httpserver
A high performance simple configurable Go HTTP server that is compatible with http.Handler
package main
import "github.com/cihangir/httpserver"
func middleware(next http.Handler) http.Handler {
fn := func(rw http.ResponseWriter, req *http.Request) {
// do first
next.ServeHTTP(rw, req)
// do then
}
return http.HandlerFunc(fn)
}
func main() {
s := httpserver.New(middleware)
s.Get("/1", helloer)
s.Get("/2", httpserver.NewHandler(
http.HandlerFunc(helloer),
middleware2,
middleware3,
))
s.ListenAndServe(addr)
}
func helloer(rw http.ResponseWriter, req *http.Request) {
io.WriteString(rw, "Hello, World!")
}
##Middlewares
Middleware are just http.HandlerFunc's
func middlewareCreator(next http.Handler) http.Handler {
fn := func(rw http.ResponseWriter, req *http.Request) {
p := "my middleware"
io.WriteString(rw, p)
next.ServeHTTP(rw, req)
io.WriteString(rw, p)
}
return http.HandlerFunc(fn)
}
Execution of the handlers are not bi-directional, you can wrap all handler with the first middleware. Following is the order of execution;
//0 START
//1 START
//2 START
//2 END
//1 END
//0 END
Features
Simple: Compatible with stdlib's handler structure
Pluggable: You can have global and handler based middlewares
Performant: It uses julienschmidt/httprouter as router
Install
Install the package with:
go get github.com/cihangir/httpserver
Import it with:
import "github.com/cihangir/httpserver"
Usage
func NewHandler
func NewHandler(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler
NewHandler creates a new http handler with optional middlewares
type Server
type Server struct {
// Router holds a performant router for requests
Router *httprouter.Router
// Middlewares holds global middlewares for the server
Middlewares []func(next http.Handler) http.Handler
}
Server is a high performance simple configurable Go HTTP server
func New
func New(middlewares ...func(http.Handler) http.Handler) *Server
New creates a server
func (*Server) Get
func (s *Server) Get(p string, handler http.Handler)
Get is a shortcut for Server.Handle("GET", p, handle)
func (*Server) Handle
func (s *Server) Handle(method, p string, h http.Handler)
Handle passes handlers to the router
func (*Server) Head
func (s *Server) Head(p string, handler http.Handler)
Head is a shortcut for Server.Handle("HEAD", p, handle)
func (*Server) ListenAndServe
func (s *Server) ListenAndServe(addr string) error
ListenAndServe serves the server with default http mux
func (*Server) Options
func (s *Server) Options(p string, handler http.Handler)
Options is a shortcut for Server.Handle("OPTIONS", p, handle)
func (*Server) Post
func (s *Server) Post(p string, handler http.Handler)
Post is a shortcut for Server.Handle("POST", p, handle)
func (*Server) ServeHTTP
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements Handler interface