alien

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2019 License: MIT Imports: 5 Imported by: 8

README

Alien Coverage Status Build Status GoDoc Go Report Card

Alien is a lightweight http router( multiplexer) for Go( Golang ), made for humans who don't like magic.

Documentation docs

Features

  • fast ( see the benchmarks, or run them yourself)
  • lightweight ( just a single file read all of it in less than a minute)
  • safe( designed with concurrency in mind)
  • middleware support.
  • routes groups
  • no external dependency( only the standard library )

Motivation

I wanted a simple, fast, and lightweight router that has no unnecessary overhead using the standard library only, following good practices and well tested code( Over 90% coverage)

Installation

go get github.com/gernest/alien

Usage

normal static routes


package main

import (
	"log"
	"net/http"

	"github.com/gernest/alien"
)

func main() {
	m := alien.New()
	m.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})
	log.Fatal(http.ListenAndServe(":8090", m))
}

visiting your localhost at path / will print hello world

named params


package main

import (
	"log"
	"net/http"

	"github.com/gernest/alien"
)

func main() {
	m := alien.New()
	m.Get("/hello/:name", func(w http.ResponseWriter, r *http.Request) {
		p := alien.GetParams(r)
		w.Write([]byte(p.Get("name")))
	})
	log.Fatal(http.ListenAndServe(":8090", m))
}

visiting your localhost at path /hello/tanzania will print tanzania

catch all params

package main

import (
	"log"
	"net/http"

	"github.com/gernest/alien"
)

func main() {
	m := alien.New()
	m.Get("/hello/*name", func(w http.ResponseWriter, r *http.Request) {
		p := alien.GetParams(r)
		w.Write([]byte(p.Get("name")))
	})
	log.Fatal(http.ListenAndServe(":8090", m))
}

visiting your localhost at path /hello/my/margicl/sheeplike/ship will print my/margical/sheeplike/ship

middlewares

Middlewares are anything that satisfy the interface func(http.Handler)http.Handler . Meaning you have thousands of middlewares at your disposal, you can use middlewares from many golang http frameworks on alien(most support the interface).


package main

import (
	"log"
	"net/http"

	"github.com/gernest/alien"
)

func middleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello middlware"))
	})
}

func main() {
	m := alien.New()
	m.Use(middleware)
	m.Get("/", func(_ http.ResponseWriter, _ *http.Request) {
	})
	log.Fatal(http.ListenAndServe(":8090", m))
}

visiting your localhost at path / will print hello middleware

groups

You can group routes

package main

import (
	"log"
	"net/http"

	"github.com/gernest/alien"
)

func main() {
	m := alien.New()
	g := m.Group("/home")
	m.Use(middleware)
	g.Get("/alone", func(w http.ResponseWriter, _ *http.Request) {
		w.Write([]byte("home alone"))
	})
	log.Fatal(http.ListenAndServe(":8090", m))
}

visiting your localhost at path /home/alone will print home alone

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork the repository and submit a pull request for whatever change you want to be added to this project.

If you have any questions, just open an issue.

Author

Geofrey Ernest @gernesti on twitter

Licence

MIT see LICENSE

Documentation

Overview

Package alien is a lightweight http router from outer space.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseParams

func ParseParams(matched, pattern string) (result string, err error)

ParseParams parses params found in mateched from pattern. There are two kinds of params, one to capture a segment which starts with : and a nother to capture everything( a.k.a catch all) whis starts with *.

For instance

pattern:="/hello/:name"
matched:="/hello/world"

Will result into name:world. this function captures the named params and theri coreesponding values, returning them in a comma separated string of a key:value nature. please see the tests for more details.

Types

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux is a http multiplexer that allows matching of http requests to the registered http handlers.

Mux supports named parameters in urls like

/hello/:name

will match

/hello/world

where by inside the request passed to the handler, the param with key name and value world will be passed.

Mux supports catch all parameters too

/hello/*whatever

will match

/hello/world
/hello/world/tanzania
/hello/world/afica/tanzania.png

where by inside the request passed to the handler, the param with key whatever will be set and value will be

world
world/tanzania
world/afica/tanzania.png

If you dont specify a name in a catch all route, then the default name "catch" will be ussed.

func New

func New() *Mux

New returns a new *Mux instance with default handler for mismatched routes.

func (*Mux) AddRoute

func (m *Mux) AddRoute(method, pattern string, h func(http.ResponseWriter, *http.Request)) error

AddRoute registers h with pattern and method. If there is a path prefix created via the Group method) it will be set.

func (*Mux) Connect

func (m *Mux) Connect(path string, h func(http.ResponseWriter, *http.Request)) error

Connect registers h with pattern and method CONNECT.

func (*Mux) Delete

func (m *Mux) Delete(path string, h func(http.ResponseWriter, *http.Request)) error

Delete registers h with pattern and method DELETE.

func (*Mux) Get

func (m *Mux) Get(pattern string, h func(http.ResponseWriter, *http.Request)) error

Get registers h with pattern and method GET.

func (*Mux) Group

func (m *Mux) Group(pattern string) *Mux

Group creates a path prefix group for pattern, all routes registered using the returned Mux will only match if the request path starts with pattern. For instance .

m:=New()
home:=m.Group("/home")
home.Get("/alone",myHandler)

will match

/home/alone

func (*Mux) Head

func (m *Mux) Head(path string, h func(http.ResponseWriter, *http.Request)) error

Head registers h with pattern and method HEAD.

func (*Mux) NotFoundHandler

func (m *Mux) NotFoundHandler(h http.Handler)

NotFoundHandler is executed when the request route is not found.

func (*Mux) Options

func (m *Mux) Options(path string, h func(http.ResponseWriter, *http.Request)) error

Options registers h with pattern and method OPTIONS.

func (*Mux) Patch

func (m *Mux) Patch(path string, h func(http.ResponseWriter, *http.Request)) error

Patch registers h with pattern and method PATCH.

func (*Mux) Post

func (m *Mux) Post(path string, h func(http.ResponseWriter, *http.Request)) error

Post registers h with pattern and method POST.

func (*Mux) Put

func (m *Mux) Put(path string, h func(http.ResponseWriter, *http.Request)) error

Put registers h with pattern and method PUT.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. It muliplexes http requests against registered handlers.

func (*Mux) Trace

func (m *Mux) Trace(path string, h func(http.ResponseWriter, *http.Request)) error

Trace registers h with pattern and method TRACE.

func (*Mux) Use

func (m *Mux) Use(middleware ...func(http.Handler) http.Handler)

Use assigns midlewares to the current *Mux. All routes registered by the *Mux after this call will have the middlewares assigned to them.

type Params

type Params map[string]string

Params stores route params.

func GetParams

func GetParams(r *http.Request) Params

GetParams returrns route params stored in r.

func (Params) Get

func (p Params) Get(key string) string

Get returns value associated with key.

func (Params) Load

func (p Params) Load(src string)

Load loads params found in src into p.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL