|
|
@ -15,8 +15,10 @@ type (
|
|
|
|
start func(*engine) error
|
|
|
|
start func(*engine) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RunOption defines the method to customize a Server.
|
|
|
|
RunOption func(*Server)
|
|
|
|
RunOption func(*Server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A Server is a http server.
|
|
|
|
Server struct {
|
|
|
|
Server struct {
|
|
|
|
ngin *engine
|
|
|
|
ngin *engine
|
|
|
|
opts runOptions
|
|
|
|
opts runOptions
|
|
|
@ -58,6 +60,7 @@ func NewServer(c RestConf, opts ...RunOption) (*Server, error) {
|
|
|
|
return server, nil
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddRoutes add given routes into the Server.
|
|
|
|
func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) {
|
|
|
|
func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) {
|
|
|
|
r := featuredRoutes{
|
|
|
|
r := featuredRoutes{
|
|
|
|
routes: rs,
|
|
|
|
routes: rs,
|
|
|
@ -68,28 +71,34 @@ func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) {
|
|
|
|
e.ngin.AddRoutes(r)
|
|
|
|
e.ngin.AddRoutes(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddRoute adds given route into the Server.
|
|
|
|
func (e *Server) AddRoute(r Route, opts ...RouteOption) {
|
|
|
|
func (e *Server) AddRoute(r Route, opts ...RouteOption) {
|
|
|
|
e.AddRoutes([]Route{r}, opts...)
|
|
|
|
e.AddRoutes([]Route{r}, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start starts the Server.
|
|
|
|
func (e *Server) Start() {
|
|
|
|
func (e *Server) Start() {
|
|
|
|
handleError(e.opts.start(e.ngin))
|
|
|
|
handleError(e.opts.start(e.ngin))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stop stops the Server.
|
|
|
|
func (e *Server) Stop() {
|
|
|
|
func (e *Server) Stop() {
|
|
|
|
logx.Close()
|
|
|
|
logx.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use adds the given middleware in the Server.
|
|
|
|
func (e *Server) Use(middleware Middleware) {
|
|
|
|
func (e *Server) Use(middleware Middleware) {
|
|
|
|
e.ngin.use(middleware)
|
|
|
|
e.ngin.use(middleware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ToMiddleware converts the given handler to a Middleware.
|
|
|
|
func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
|
|
|
|
func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
|
|
|
|
return func(handle http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(handle http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return handler(handle).ServeHTTP
|
|
|
|
return handler(handle).ServeHTTP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithJwt returns a func to enable jwt authentication in given route.
|
|
|
|
func WithJwt(secret string) RouteOption {
|
|
|
|
func WithJwt(secret string) RouteOption {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
validateSecret(secret)
|
|
|
|
validateSecret(secret)
|
|
|
@ -98,6 +107,8 @@ func WithJwt(secret string) RouteOption {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithJwtTransition returns a func to enable jwt authentication as well as jwt secret transition.
|
|
|
|
|
|
|
|
// Which means old and new jwt secrets work together for a peroid.
|
|
|
|
func WithJwtTransition(secret, prevSecret string) RouteOption {
|
|
|
|
func WithJwtTransition(secret, prevSecret string) RouteOption {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
// why not validate prevSecret, because prevSecret is an already used one,
|
|
|
|
// why not validate prevSecret, because prevSecret is an already used one,
|
|
|
@ -109,6 +120,7 @@ func WithJwtTransition(secret, prevSecret string) RouteOption {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithMiddlewares adds given middlewares to given routes.
|
|
|
|
func WithMiddlewares(ms []Middleware, rs ...Route) []Route {
|
|
|
|
func WithMiddlewares(ms []Middleware, rs ...Route) []Route {
|
|
|
|
for i := len(ms) - 1; i >= 0; i-- {
|
|
|
|
for i := len(ms) - 1; i >= 0; i-- {
|
|
|
|
rs = WithMiddleware(ms[i], rs...)
|
|
|
|
rs = WithMiddleware(ms[i], rs...)
|
|
|
@ -116,6 +128,7 @@ func WithMiddlewares(ms []Middleware, rs ...Route) []Route {
|
|
|
|
return rs
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithMiddleware adds given middleware to given route.
|
|
|
|
func WithMiddleware(middleware Middleware, rs ...Route) []Route {
|
|
|
|
func WithMiddleware(middleware Middleware, rs ...Route) []Route {
|
|
|
|
routes := make([]Route, len(rs))
|
|
|
|
routes := make([]Route, len(rs))
|
|
|
|
|
|
|
|
|
|
|
@ -131,24 +144,28 @@ func WithMiddleware(middleware Middleware, rs ...Route) []Route {
|
|
|
|
return routes
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithNotFoundHandler returns a RunOption with not found handler set to given handler.
|
|
|
|
func WithNotFoundHandler(handler http.Handler) RunOption {
|
|
|
|
func WithNotFoundHandler(handler http.Handler) RunOption {
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.SetNotFoundHandler(handler)
|
|
|
|
rt.SetNotFoundHandler(handler)
|
|
|
|
return WithRouter(rt)
|
|
|
|
return WithRouter(rt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithNotAllowedHandler returns a RunOption with not allowed handler set to given handler.
|
|
|
|
func WithNotAllowedHandler(handler http.Handler) RunOption {
|
|
|
|
func WithNotAllowedHandler(handler http.Handler) RunOption {
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.SetNotAllowedHandler(handler)
|
|
|
|
rt.SetNotAllowedHandler(handler)
|
|
|
|
return WithRouter(rt)
|
|
|
|
return WithRouter(rt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithPriority returns a RunOption with priority.
|
|
|
|
func WithPriority() RouteOption {
|
|
|
|
func WithPriority() RouteOption {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
r.priority = true
|
|
|
|
r.priority = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithRouter returns a RunOption that make server run with given router.
|
|
|
|
func WithRouter(router httpx.Router) RunOption {
|
|
|
|
func WithRouter(router httpx.Router) RunOption {
|
|
|
|
return func(server *Server) {
|
|
|
|
return func(server *Server) {
|
|
|
|
server.opts.start = func(srv *engine) error {
|
|
|
|
server.opts.start = func(srv *engine) error {
|
|
|
@ -157,6 +174,7 @@ func WithRouter(router httpx.Router) RunOption {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithSignature returns a RouteOption to enable signature verification.
|
|
|
|
func WithSignature(signature SignatureConf) RouteOption {
|
|
|
|
func WithSignature(signature SignatureConf) RouteOption {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
return func(r *featuredRoutes) {
|
|
|
|
r.signature.enabled = true
|
|
|
|
r.signature.enabled = true
|
|
|
@ -166,12 +184,14 @@ func WithSignature(signature SignatureConf) RouteOption {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithUnauthorizedCallback returns a RunOption that with given unauthorized callback set.
|
|
|
|
func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
|
|
|
|
func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
|
|
|
|
return func(engine *Server) {
|
|
|
|
return func(engine *Server) {
|
|
|
|
engine.ngin.SetUnauthorizedCallback(callback)
|
|
|
|
engine.ngin.SetUnauthorizedCallback(callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithUnsignedCallback returns a RunOption that with given unsigned callback set.
|
|
|
|
func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
|
|
|
|
func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
|
|
|
|
return func(engine *Server) {
|
|
|
|
return func(engine *Server) {
|
|
|
|
engine.ngin.SetUnsignedCallback(callback)
|
|
|
|
engine.ngin.SetUnsignedCallback(callback)
|
|
|
|