123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package rest
- import (
- "crypto/tls"
- "errors"
- "net/http"
- "path"
- "time"
- "github.com/wuntsong-org/go-zero-plus/core/logx"
- "github.com/wuntsong-org/go-zero-plus/rest/chain"
- "github.com/wuntsong-org/go-zero-plus/rest/handler"
- "github.com/wuntsong-org/go-zero-plus/rest/httpx"
- "github.com/wuntsong-org/go-zero-plus/rest/internal"
- "github.com/wuntsong-org/go-zero-plus/rest/internal/cors"
- "github.com/wuntsong-org/go-zero-plus/rest/router"
- )
- type (
- // RunOption defines the method to customize a Server.
- RunOption func(*Server)
- // StartOption defines the method to customize http server.
- StartOption = internal.StartOption
- // A Server is a http server.
- Server struct {
- ngin *engine
- router httpx.Router
- }
- )
- // MustNewServer returns a server with given config of c and options defined in opts.
- // Be aware that later RunOption might overwrite previous one that write the same option.
- // The process will exit if error occurs.
- func MustNewServer(c RestConf, opts ...RunOption) *Server {
- server, err := NewServer(c, opts...)
- if err != nil {
- logx.Must(err)
- }
- return server
- }
- // NewServer returns a server with given config of c and options defined in opts.
- // Be aware that later RunOption might overwrite previous one that write the same option.
- func NewServer(c RestConf, opts ...RunOption) (*Server, error) {
- if err := c.SetUp(); err != nil {
- return nil, err
- }
- server := &Server{
- ngin: newEngine(c),
- router: router.NewRouter(),
- }
- opts = append([]RunOption{WithNotFoundHandler(nil)}, opts...)
- for _, opt := range opts {
- opt(server)
- }
- return server, nil
- }
- // AddRoutes add given routes into the Server.
- func (s *Server) AddRoutes(rs []Route, opts ...RouteOption) {
- r := featuredRoutes{
- routes: rs,
- }
- for _, opt := range opts {
- opt(&r)
- }
- s.ngin.addRoutes(r)
- }
- // AddRoute adds given route into the Server.
- func (s *Server) AddRoute(r Route, opts ...RouteOption) {
- s.AddRoutes([]Route{r}, opts...)
- }
- // PrintRoutes prints the added routes to stdout.
- func (s *Server) PrintRoutes() {
- s.ngin.print()
- }
- // Routes returns the HTTP routers that registered in the server.
- func (s *Server) Routes() []Route {
- var routes []Route
- for _, r := range s.ngin.routes {
- routes = append(routes, r.routes...)
- }
- return routes
- }
- // ServeHTTP is for test purpose, allow developer to do a unit test with
- // all defined router without starting an HTTP Server.
- //
- // For example:
- //
- // server := MustNewServer(...)
- // server.addRoute(...) // router a
- // server.addRoute(...) // router b
- // server.addRoute(...) // router c
- //
- // r, _ := http.NewRequest(...)
- // w := httptest.NewRecorder(...)
- // server.ServeHTTP(w, r)
- // // verify the response
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- s.ngin.bindRoutes(s.router)
- s.router.ServeHTTP(w, r)
- }
- // Start starts the Server.
- // Graceful shutdown is enabled by default.
- // Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
- func (s *Server) Start() {
- handleError(s.ngin.start(nil, s.router))
- }
- func (s *Server) StartAsGoRoutine() *http.Server {
- ch := make(chan *http.Server, 1)
- go func() {
- defer func() {
- recover()
- }()
- handleError(s.ngin.start(ch, s.router))
- }()
- return <-ch
- }
- // StartWithOpts starts the Server.
- // Graceful shutdown is enabled by default.
- // Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
- func (s *Server) StartWithOpts(opts ...StartOption) {
- handleError(s.ngin.start(nil, s.router, opts...))
- }
- func (s *Server) StartAsGoRoutineWithOpts(opts ...StartOption) *http.Server {
- ch := make(chan *http.Server, 1)
- go func() {
- defer func() {
- recover()
- }()
- handleError(s.ngin.start(ch, s.router, opts...))
- }()
- return <-ch
- }
- // Stop stops the Server.
- func (s *Server) Stop() {
- logx.Close()
- }
- // Use adds the given middleware in the Server.
- func (s *Server) Use(middleware Middleware) {
- s.ngin.use(middleware)
- }
- // ToMiddleware converts the given handler to a Middleware.
- func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
- return func(handle http.HandlerFunc) http.HandlerFunc {
- return handler(handle).ServeHTTP
- }
- }
- // WithChain returns a RunOption that uses the given chain to replace the default chain.
- // JWT auth middleware and the middlewares that added by svr.Use() will be appended.
- func WithChain(chn chain.Chain) RunOption {
- return func(svr *Server) {
- svr.ngin.chain = chn
- }
- }
- // WithCors returns a func to enable CORS for given origin, or default to all origins (*).
- func WithCors(origin ...string) RunOption {
- return func(server *Server) {
- server.router.SetNotAllowedHandler(cors.NotAllowedHandler(nil, origin...))
- server.router = newCorsRouter(server.router, nil, origin...)
- }
- }
- // WithCustomCors returns a func to enable CORS for given origin, or default to all origins (*),
- // fn lets caller customizing the response.
- func WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter),
- origin ...string) RunOption {
- return func(server *Server) {
- server.router.SetNotAllowedHandler(cors.NotAllowedHandler(notAllowedFn, origin...))
- server.router = newCorsRouter(server.router, middlewareFn, origin...)
- }
- }
- // WithJwt returns a func to enable jwt authentication in given route.
- func WithJwt(secret string) RouteOption {
- return func(r *featuredRoutes) {
- validateSecret(secret)
- r.jwt.enabled = true
- r.jwt.secret = secret
- }
- }
- // 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 period.
- func WithJwtTransition(secret, prevSecret string) RouteOption {
- return func(r *featuredRoutes) {
- // why not validate prevSecret, because prevSecret is an already used one,
- // even it not meet our requirement, we still need to allow the transition.
- validateSecret(secret)
- r.jwt.enabled = true
- r.jwt.secret = secret
- r.jwt.prevSecret = prevSecret
- }
- }
- // WithMaxBytes returns a RouteOption to set maxBytes with the given value.
- func WithMaxBytes(maxBytes int64) RouteOption {
- return func(r *featuredRoutes) {
- r.maxBytes = maxBytes
- }
- }
- // WithMiddlewares adds given middlewares to given routes.
- func WithMiddlewares(ms []Middleware, rs ...Route) []Route {
- for i := len(ms) - 1; i >= 0; i-- {
- rs = WithMiddleware(ms[i], rs...)
- }
- return rs
- }
- // WithMiddleware adds given middleware to given route.
- func WithMiddleware(middleware Middleware, rs ...Route) []Route {
- routes := make([]Route, len(rs))
- for i := range rs {
- route := rs[i]
- routes[i] = Route{
- Method: route.Method,
- Path: route.Path,
- Handler: middleware(route.Handler),
- }
- }
- return routes
- }
- // WithNotFoundHandler returns a RunOption with not found handler set to given handler.
- func WithNotFoundHandler(handler http.Handler) RunOption {
- return func(server *Server) {
- notFoundHandler := server.ngin.notFoundHandler(handler)
- server.router.SetNotFoundHandler(notFoundHandler)
- }
- }
- // WithNotAllowedHandler returns a RunOption with not allowed handler set to given handler.
- func WithNotAllowedHandler(handler http.Handler) RunOption {
- return func(server *Server) {
- server.router.SetNotAllowedHandler(handler)
- }
- }
- func WithOptionsHandler(handler http.Handler) RunOption {
- return func(server *Server) {
- server.router.SetOptionsHandler(handler)
- }
- }
- func WithGlobalMiddleware(middleware httpx.MiddlewareFunc) RunOption {
- return func(server *Server) {
- server.router.SetMiddleware(middleware)
- }
- }
- // WithPrefix adds group as a prefix to the route paths.
- func WithPrefix(group string) RouteOption {
- return func(r *featuredRoutes) {
- var routes []Route
- for _, rt := range r.routes {
- p := path.Join(group, rt.Path)
- routes = append(routes, Route{
- Method: rt.Method,
- Path: p,
- Handler: rt.Handler,
- })
- }
- r.routes = routes
- }
- }
- // WithPriority returns a RunOption with priority.
- func WithPriority() RouteOption {
- return func(r *featuredRoutes) {
- r.priority = true
- }
- }
- // WithRouter returns a RunOption that make server run with given router.
- func WithRouter(router httpx.Router) RunOption {
- return func(server *Server) {
- server.router = router
- }
- }
- // WithSignature returns a RouteOption to enable signature verification.
- func WithSignature(signature SignatureConf) RouteOption {
- return func(r *featuredRoutes) {
- r.signature.enabled = true
- r.signature.Strict = signature.Strict
- r.signature.Expiry = signature.Expiry
- r.signature.PrivateKeys = signature.PrivateKeys
- }
- }
- // WithTimeout returns a RouteOption to set timeout with given value.
- func WithTimeout(timeout time.Duration) RouteOption {
- return func(r *featuredRoutes) {
- r.timeout = timeout
- }
- }
- // WithTLSConfig returns a RunOption that with given tls config.
- func WithTLSConfig(cfg *tls.Config) RunOption {
- return func(svr *Server) {
- svr.ngin.setTlsConfig(cfg)
- }
- }
- // WithUnauthorizedCallback returns a RunOption that with given unauthorized callback set.
- func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
- return func(svr *Server) {
- svr.ngin.setUnauthorizedCallback(callback)
- }
- }
- // WithUnsignedCallback returns a RunOption that with given unsigned callback set.
- func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
- return func(svr *Server) {
- svr.ngin.setUnsignedCallback(callback)
- }
- }
- func handleError(err error) {
- // ErrServerClosed means the server is closed manually
- if err == nil || errors.Is(err, http.ErrServerClosed) {
- return
- }
- logx.Error(err)
- panic(err)
- }
- func validateSecret(secret string) {
- if len(secret) < 8 {
- panic("secret's length can't be less than 8")
- }
- }
- type corsRouter struct {
- httpx.Router
- middleware Middleware
- }
- func newCorsRouter(router httpx.Router, headerFn func(http.Header), origins ...string) httpx.Router {
- return &corsRouter{
- Router: router,
- middleware: cors.Middleware(headerFn, origins...),
- }
- }
- func (c *corsRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- c.middleware(c.Router.ServeHTTP)(w, r)
- }
|