2018-08-03 21:29:15 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-11 16:24:53 +02:00
|
|
|
"context"
|
2019-06-11 16:30:58 +02:00
|
|
|
"crypto/tls"
|
2018-08-03 21:29:15 +02:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2019-06-11 16:24:53 +02:00
|
|
|
"time"
|
2018-08-03 21:29:15 +02:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type route struct {
|
2018-10-09 18:07:43 +02:00
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Handle interface{}
|
|
|
|
Middleware []Middleware
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
// Handle handles a request
|
2018-10-16 18:26:06 +02:00
|
|
|
type Handle = func(*Context) error
|
2018-10-09 18:07:43 +02:00
|
|
|
|
|
|
|
// ErrorHandle handles a request
|
|
|
|
type ErrorHandle func(*Context, interface{})
|
|
|
|
|
2018-11-18 16:20:13 +01:00
|
|
|
// Middleware is a function that runs before your route, it gets the next handler as a parameter
|
2018-10-09 18:07:43 +02:00
|
|
|
type Middleware func(Handle) Handle
|
2018-08-03 21:29:15 +02:00
|
|
|
|
2018-11-30 14:48:07 +01:00
|
|
|
// Reader reads input to dst, returns true if successful
|
2018-11-18 16:29:06 +01:00
|
|
|
type Reader func(c *Context, dst interface{}) (bool, error)
|
2018-10-09 19:18:09 +02:00
|
|
|
|
2018-08-03 21:29:15 +02:00
|
|
|
// Router is the router itself
|
|
|
|
type Router struct {
|
2018-10-09 18:07:43 +02:00
|
|
|
routes []route
|
2018-10-09 19:18:09 +02:00
|
|
|
Reader Reader
|
2018-10-09 18:07:43 +02:00
|
|
|
Renderer Renderer
|
|
|
|
middleware []Middleware
|
|
|
|
NotFoundHandler Handle
|
|
|
|
MethodNotAllowedHandler Handle
|
|
|
|
ErrorHandler ErrorHandle
|
2019-06-11 16:24:53 +02:00
|
|
|
server *http.Server
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Router
|
|
|
|
func New() *Router {
|
2018-10-09 19:18:09 +02:00
|
|
|
return &Router{Reader: defaultReader, NotFoundHandler: defaultNotFoundHandler, MethodNotAllowedHandler: defaultMethodNotAllowedHandler, ErrorHandler: defaultErrorHandler}
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
// Use adds a global middleware
|
|
|
|
func (r *Router) Use(m ...Middleware) {
|
|
|
|
r.middleware = append(r.middleware, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group creates a new router group with a shared prefix and set of middlewares
|
|
|
|
func (r *Router) Group(prefix string, middleware ...Middleware) *Group {
|
|
|
|
return &Group{prefix: prefix, router: r, middleware: middleware}
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:29:15 +02:00
|
|
|
// GET adds a GET route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) GET(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
r.routes = append(r.routes, route{`GET`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST adds a POST route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) POST(path string, handle interface{}, middleware ...Middleware) {
|
2018-08-03 21:29:15 +02:00
|
|
|
checkInterfaceHandle(handle)
|
2018-10-09 18:07:43 +02:00
|
|
|
r.routes = append(r.routes, route{`POST`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE adds a DELETE route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) DELETE(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
r.routes = append(r.routes, route{`DELETE`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT adds a PUT route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) PUT(path string, handle interface{}, middleware ...Middleware) {
|
2018-08-03 21:29:15 +02:00
|
|
|
checkInterfaceHandle(handle)
|
2018-10-09 18:07:43 +02:00
|
|
|
r.routes = append(r.routes, route{`PUT`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH adds a PATCH route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) PATCH(path string, handle interface{}, middleware ...Middleware) {
|
2018-08-03 21:29:15 +02:00
|
|
|
checkInterfaceHandle(handle)
|
2018-10-09 18:07:43 +02:00
|
|
|
r.routes = append(r.routes, route{`PATCH`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD adds a HEAD route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) HEAD(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
r.routes = append(r.routes, route{`HEAD`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS adds a OPTIONS route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (r *Router) OPTIONS(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
r.routes = append(r.routes, route{`OPTIONS`, path, handle, middleware})
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the web server and binds to the given address
|
|
|
|
func (r *Router) Start(addr string) error {
|
|
|
|
httpr := r.getHttpr()
|
|
|
|
|
2019-06-11 16:24:53 +02:00
|
|
|
r.server = &http.Server{Addr: addr, Handler: httpr}
|
|
|
|
return r.server.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:24:45 +02:00
|
|
|
// StartTLS starts a TLS web server using the given key, cert and config and binds to the given address
|
2019-06-11 16:30:58 +02:00
|
|
|
func (r *Router) StartTLS(addr, certFile, keyFile string, conf *tls.Config) error {
|
|
|
|
httpr := r.getHttpr()
|
|
|
|
|
|
|
|
r.server = &http.Server{Addr: addr, Handler: httpr, TLSConfig: conf}
|
|
|
|
return r.server.ListenAndServeTLS(certFile, keyFile)
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:24:53 +02:00
|
|
|
// Stop stops the web server
|
|
|
|
func (r *Router) Stop() error {
|
|
|
|
if r.server == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:24:45 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
2019-06-11 16:24:53 +02:00
|
|
|
defer cancel()
|
|
|
|
err := r.server.Shutdown(ctx)
|
|
|
|
if err == context.DeadlineExceeded {
|
|
|
|
err = r.server.Close()
|
|
|
|
}
|
|
|
|
r.server = nil
|
|
|
|
|
|
|
|
return err
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) getHttpr() *httprouter.Router {
|
|
|
|
httpr := httprouter.New()
|
|
|
|
|
|
|
|
for _, v := range r.routes {
|
2018-10-09 18:07:43 +02:00
|
|
|
handle, ok := v.Handle.(Handle)
|
|
|
|
if !ok {
|
|
|
|
handle = handlePOST(r, v.Handle)
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:26:29 +02:00
|
|
|
middleware := make([]Middleware, len(r.middleware)+len(v.Middleware))
|
|
|
|
copy(middleware, r.middleware)
|
|
|
|
copy(middleware[len(r.middleware):], v.Middleware)
|
|
|
|
|
|
|
|
httpr.Handle(v.Method, v.Path, handleReq(r, handle, middleware))
|
2018-10-09 18:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
httpr.NotFound = http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
handleReq(r, r.NotFoundHandler, r.middleware)(res, req, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
httpr.MethodNotAllowed = http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
handleReq(r, r.MethodNotAllowedHandler, r.middleware)(res, req, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
httpr.PanicHandler = func(res http.ResponseWriter, req *http.Request, err interface{}) {
|
2020-04-09 10:41:23 +02:00
|
|
|
c := NewContext(r, res, req, nil)
|
2018-10-09 18:07:43 +02:00
|
|
|
r.ErrorHandler(c, err)
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return httpr
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkInterfaceHandle(f interface{}) {
|
2018-10-09 18:07:43 +02:00
|
|
|
if _, ok := f.(Handle); ok {
|
2018-08-03 21:29:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := reflect.TypeOf(f)
|
|
|
|
|
|
|
|
if rt.Kind() != reflect.Func {
|
|
|
|
panic(`non-func handle`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rt.NumIn() != 2 {
|
|
|
|
panic(`handle should take 2 arguments`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rt.NumOut() != 1 || rt.Out(0).Name() != `error` {
|
|
|
|
panic(`handle should return only error`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rt.In(0) != reflect.TypeOf(&Context{}) {
|
|
|
|
panic(`handle should accept Context as first argument`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
func handlePOST(r *Router, f interface{}) Handle {
|
2018-08-03 21:29:15 +02:00
|
|
|
funcRv, inputRt := reflect.ValueOf(f), reflect.TypeOf(f).In(1)
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
return func(c *Context) error {
|
2018-08-03 21:29:15 +02:00
|
|
|
data := reflect.New(inputRt)
|
2018-10-09 18:07:43 +02:00
|
|
|
|
2018-11-18 16:29:06 +01:00
|
|
|
if r.Reader != nil {
|
|
|
|
ok, err := r.Reader(c, data.Interface())
|
2018-11-30 14:48:07 +01:00
|
|
|
_ = c.Request.Body.Close()
|
2018-11-18 16:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out := funcRv.Call([]reflect.Value{reflect.ValueOf(c), data.Elem()})
|
2018-10-09 18:07:43 +02:00
|
|
|
|
|
|
|
if out[0].IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return out[0].Interface().(error)
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
func handleReq(r *Router, handle Handle, m []Middleware) httprouter.Handle {
|
2018-08-03 21:29:15 +02:00
|
|
|
return func(res http.ResponseWriter, req *http.Request, param httprouter.Params) {
|
2020-04-09 10:41:23 +02:00
|
|
|
c := NewContext(r, res, req, param)
|
2018-08-03 21:29:15 +02:00
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
f := handle
|
2019-07-03 12:24:45 +02:00
|
|
|
for i := len(m) - 1; i >= 0; i-- {
|
2018-10-09 18:07:43 +02:00
|
|
|
f = m[i](f)
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:29:15 +02:00
|
|
|
err := f(c)
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
if err != nil {
|
|
|
|
r.ErrorHandler(c, err)
|
|
|
|
}
|
2018-08-03 21:29:15 +02:00
|
|
|
}
|
|
|
|
}
|