From f372ded63c0cca567bfcbd38f5b9a1dbf0e579d3 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 31 Jul 2020 11:45:16 +0800 Subject: [PATCH] rename ngin to rest --- example/graceful/dns/api/config/config.go | 2 +- example/graceful/dns/api/graceful.go | 2 +- example/graceful/dns/api/handler/routes.go | 2 +- example/graceful/etcd/api/config/config.go | 2 +- example/graceful/etcd/api/graceful.go | 2 +- example/graceful/etcd/api/handler/routes.go | 2 +- example/http/breaker/server.go | 2 +- example/http/demo/main.go | 2 +- example/http/post/main.go | 2 +- example/http/shedding/main.go | 2 +- example/http/signature/server/server.go | 2 +- example/jwt/user/user.go | 4 +- example/load/simulate/server/server.go | 2 +- example/tracing/edge/main.go | 2 +- rest/config.go | 4 +- rest/internal/server.go | 40 --------------- rest/internal/starter.go | 35 +++++++++++++ rest/ngin.go | 52 +++++++++---------- rest/server.go | 56 ++++++++++----------- rest/types.go | 6 ++- tools/goctl/api/demo/config/config.go | 2 +- tools/goctl/api/demo/demo.go | 2 +- tools/goctl/api/demo/handler/handlers.go | 2 +- tools/goctl/api/gogen/genconfig.go | 2 +- tools/goctl/api/gogen/genmain.go | 2 +- 25 files changed, 113 insertions(+), 120 deletions(-) delete mode 100644 rest/internal/server.go diff --git a/example/graceful/dns/api/config/config.go b/example/graceful/dns/api/config/config.go index daf38faa..4ba55209 100644 --- a/example/graceful/dns/api/config/config.go +++ b/example/graceful/dns/api/config/config.go @@ -6,6 +6,6 @@ import ( ) type Config struct { - rest.RtConf + rest.RestConf Rpc rpcx.RpcClientConf } diff --git a/example/graceful/dns/api/graceful.go b/example/graceful/dns/api/graceful.go index 70ed8f20..16ed7e70 100644 --- a/example/graceful/dns/api/graceful.go +++ b/example/graceful/dns/api/graceful.go @@ -24,7 +24,7 @@ func main() { Client: client, } - engine := rest.MustNewEngine(c.RtConf) + engine := rest.MustNewServer(c.RestConf) defer engine.Stop() handler.RegisterHandlers(engine, ctx) diff --git a/example/graceful/dns/api/handler/routes.go b/example/graceful/dns/api/handler/routes.go index 45f95d02..56bda696 100644 --- a/example/graceful/dns/api/handler/routes.go +++ b/example/graceful/dns/api/handler/routes.go @@ -8,7 +8,7 @@ import ( "zero/rest" ) -func RegisterHandlers(engine *rest.Engine, ctx *svc.ServiceContext) { +func RegisterHandlers(engine *rest.Server, ctx *svc.ServiceContext) { engine.AddRoutes([]rest.Route{ { Method: http.MethodGet, diff --git a/example/graceful/etcd/api/config/config.go b/example/graceful/etcd/api/config/config.go index daf38faa..4ba55209 100644 --- a/example/graceful/etcd/api/config/config.go +++ b/example/graceful/etcd/api/config/config.go @@ -6,6 +6,6 @@ import ( ) type Config struct { - rest.RtConf + rest.RestConf Rpc rpcx.RpcClientConf } diff --git a/example/graceful/etcd/api/graceful.go b/example/graceful/etcd/api/graceful.go index 7553e4e5..23775072 100644 --- a/example/graceful/etcd/api/graceful.go +++ b/example/graceful/etcd/api/graceful.go @@ -24,7 +24,7 @@ func main() { Client: client, } - engine := rest.MustNewEngine(c.RtConf) + engine := rest.MustNewServer(c.RestConf) defer engine.Stop() handler.RegisterHandlers(engine, ctx) diff --git a/example/graceful/etcd/api/handler/routes.go b/example/graceful/etcd/api/handler/routes.go index e067a308..a82ffad8 100644 --- a/example/graceful/etcd/api/handler/routes.go +++ b/example/graceful/etcd/api/handler/routes.go @@ -8,7 +8,7 @@ import ( "zero/rest" ) -func RegisterHandlers(engine *rest.Engine, ctx *svc.ServiceContext) { +func RegisterHandlers(engine *rest.Server, ctx *svc.ServiceContext) { engine.AddRoutes([]rest.Route{ { Method: http.MethodGet, diff --git a/example/http/breaker/server.go b/example/http/breaker/server.go index cd2e4ebe..43e782f6 100644 --- a/example/http/breaker/server.go +++ b/example/http/breaker/server.go @@ -15,7 +15,7 @@ import ( func main() { logx.Disable() stat.SetReporter(nil) - server := rest.MustNewEngine(rest.RtConf{ + server := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Name: "breaker", Log: logx.LogConf{ diff --git a/example/http/demo/main.go b/example/http/demo/main.go index fbc4d70a..93300bb0 100644 --- a/example/http/demo/main.go +++ b/example/http/demo/main.go @@ -47,7 +47,7 @@ func handle(w http.ResponseWriter, r *http.Request) { func main() { flag.Parse() - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Mode: "console", diff --git a/example/http/post/main.go b/example/http/post/main.go index 3e41efb0..2e807061 100644 --- a/example/http/post/main.go +++ b/example/http/post/main.go @@ -37,7 +37,7 @@ func handlePost(w http.ResponseWriter, r *http.Request) { func main() { flag.Parse() - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Mode: "console", diff --git a/example/http/shedding/main.go b/example/http/shedding/main.go index 01ad3c9d..efd2fa67 100644 --- a/example/http/shedding/main.go +++ b/example/http/shedding/main.go @@ -42,7 +42,7 @@ func main() { flag.Parse() logx.Disable() - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Mode: "console", diff --git a/example/http/signature/server/server.go b/example/http/signature/server/server.go index 753261c3..358cddcf 100644 --- a/example/http/signature/server/server.go +++ b/example/http/signature/server/server.go @@ -31,7 +31,7 @@ func handle(w http.ResponseWriter, r *http.Request) { func main() { flag.Parse() - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Path: "logs", diff --git a/example/jwt/user/user.go b/example/jwt/user/user.go index 87d2cc24..737d10f9 100644 --- a/example/jwt/user/user.go +++ b/example/jwt/user/user.go @@ -20,7 +20,7 @@ const jwtUserField = "user" type ( Config struct { - rest.RtConf + rest.RestConf AccessSecret string AccessExpire int64 `json:",default=1209600"` // 2 weeks RefreshSecret string @@ -79,7 +79,7 @@ func main() { var c Config conf.MustLoad("user.json", &c) - engine, err := rest.NewEngine(c.RtConf) + engine, err := rest.NewServer(c.RestConf) if err != nil { log.Fatal(err) } diff --git a/example/load/simulate/server/server.go b/example/load/simulate/server/server.go index cbb05879..f7318c25 100644 --- a/example/load/simulate/server/server.go +++ b/example/load/simulate/server/server.go @@ -25,7 +25,7 @@ func main() { }() logx.Disable() - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Mode: "console", diff --git a/example/tracing/edge/main.go b/example/tracing/edge/main.go index 87e4f4ec..2f497c58 100644 --- a/example/tracing/edge/main.go +++ b/example/tracing/edge/main.go @@ -42,7 +42,7 @@ func main() { var c rpcx.RpcClientConf conf.MustLoad(*configFile, &c) client = rpcx.MustNewClient(c) - engine := rest.MustNewEngine(rest.RtConf{ + engine := rest.MustNewServer(rest.RestConf{ ServiceConf: service.ServiceConf{ Log: logx.LogConf{ Mode: "console", diff --git a/rest/config.go b/rest/config.go index dc9e9fa8..c45d5ea4 100644 --- a/rest/config.go +++ b/rest/config.go @@ -21,10 +21,10 @@ type ( // why not name it as Conf, because we need to consider usage like: // type Config struct { // rpcx.RpcConf - // rest.RtConf + // rest.RestConf // } // if with the name Conf, there will be two Conf inside Config. - RtConf struct { + RestConf struct { service.ServiceConf Host string `json:",default=0.0.0.0"` Port int diff --git a/rest/internal/server.go b/rest/internal/server.go deleted file mode 100644 index 3b72741d..00000000 --- a/rest/internal/server.go +++ /dev/null @@ -1,40 +0,0 @@ -package internal - -import ( - "crypto/tls" - "fmt" - "net/http" -) - -func StartHttp(host string, port int, handler http.Handler) error { - addr := fmt.Sprintf("%s:%d", host, port) - server := buildHttpServer(addr, handler) - return StartServer(server) -} - -func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler) error { - addr := fmt.Sprintf("%s:%d", host, port) - if server, err := buildHttpsServer(addr, handler, certFile, keyFile); err != nil { - return err - } else { - return StartServer(server) - } -} - -func buildHttpServer(addr string, handler http.Handler) *http.Server { - return &http.Server{Addr: addr, Handler: handler} -} - -func buildHttpsServer(addr string, handler http.Handler, certFile, keyFile string) (*http.Server, error) { - cert, err := tls.LoadX509KeyPair(certFile, keyFile) - if err != nil { - return nil, err - } - - config := tls.Config{Certificates: []tls.Certificate{cert}} - return &http.Server{ - Addr: addr, - Handler: handler, - TLSConfig: &config, - }, nil -} diff --git a/rest/internal/starter.go b/rest/internal/starter.go index 7f0b9703..e0c6df65 100644 --- a/rest/internal/starter.go +++ b/rest/internal/starter.go @@ -2,11 +2,28 @@ package internal import ( "context" + "crypto/tls" + "fmt" "net/http" "zero/core/proc" ) +func StartHttp(host string, port int, handler http.Handler) error { + addr := fmt.Sprintf("%s:%d", host, port) + server := buildHttpServer(addr, handler) + return StartServer(server) +} + +func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler) error { + addr := fmt.Sprintf("%s:%d", host, port) + if server, err := buildHttpsServer(addr, handler, certFile, keyFile); err != nil { + return err + } else { + return StartServer(server) + } +} + func StartServer(srv *http.Server) error { proc.AddWrapUpListener(func() { srv.Shutdown(context.Background()) @@ -14,3 +31,21 @@ func StartServer(srv *http.Server) error { return srv.ListenAndServe() } + +func buildHttpServer(addr string, handler http.Handler) *http.Server { + return &http.Server{Addr: addr, Handler: handler} +} + +func buildHttpsServer(addr string, handler http.Handler, certFile, keyFile string) (*http.Server, error) { + cert, err := tls.LoadX509KeyPair(certFile, keyFile) + if err != nil { + return nil, err + } + + config := tls.Config{Certificates: []tls.Certificate{cert}} + return &http.Server{ + Addr: addr, + Handler: handler, + TLSConfig: &config, + }, nil +} diff --git a/rest/ngin.go b/rest/ngin.go index 393493a7..b70089ea 100644 --- a/rest/ngin.go +++ b/rest/ngin.go @@ -11,19 +11,19 @@ import ( type ( runOptions struct { - start func(*server) error + start func(*engine) error } - RunOption func(*Engine) + RunOption func(*Server) - Engine struct { - srv *server + Server struct { + ngin *engine opts runOptions } ) -func MustNewEngine(c RtConf, opts ...RunOption) *Engine { - engine, err := NewEngine(c, opts...) +func MustNewServer(c RestConf, opts ...RunOption) *Server { + engine, err := NewServer(c, opts...) if err != nil { log.Fatal(err) } @@ -31,51 +31,51 @@ func MustNewEngine(c RtConf, opts ...RunOption) *Engine { return engine } -func NewEngine(c RtConf, opts ...RunOption) (*Engine, error) { +func NewServer(c RestConf, opts ...RunOption) (*Server, error) { if err := c.SetUp(); err != nil { return nil, err } - engine := &Engine{ - srv: newServer(c), + server := &Server{ + ngin: newEngine(c), opts: runOptions{ - start: func(srv *server) error { + start: func(srv *engine) error { return srv.Start() }, }, } for _, opt := range opts { - opt(engine) + opt(server) } - return engine, nil + return server, nil } -func (e *Engine) AddRoutes(rs []Route, opts ...RouteOption) { +func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) { r := featuredRoutes{ routes: rs, } for _, opt := range opts { opt(&r) } - e.srv.AddRoutes(r) + e.ngin.AddRoutes(r) } -func (e *Engine) AddRoute(r Route, opts ...RouteOption) { +func (e *Server) AddRoute(r Route, opts ...RouteOption) { e.AddRoutes([]Route{r}, opts...) } -func (e *Engine) Start() { - handleError(e.opts.start(e.srv)) +func (e *Server) Start() { + handleError(e.opts.start(e.ngin)) } -func (e *Engine) Stop() { +func (e *Server) Stop() { logx.Close() } -func (e *Engine) Use(middleware Middleware) { - e.srv.use(middleware) +func (e *Server) Use(middleware Middleware) { + e.ngin.use(middleware) } func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware { @@ -125,8 +125,8 @@ func WithPriority() RouteOption { } func WithRouter(router router.Router) RunOption { - return func(engine *Engine) { - engine.opts.start = func(srv *server) error { + return func(server *Server) { + server.opts.start = func(srv *engine) error { return srv.StartWithRouter(router) } } @@ -142,14 +142,14 @@ func WithSignature(signature SignatureConf) RouteOption { } func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption { - return func(engine *Engine) { - engine.srv.SetUnauthorizedCallback(callback) + return func(engine *Server) { + engine.ngin.SetUnauthorizedCallback(callback) } } func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption { - return func(engine *Engine) { - engine.srv.SetUnsignedCallback(callback) + return func(engine *Server) { + engine.ngin.SetUnsignedCallback(callback) } } diff --git a/rest/server.go b/rest/server.go index e3f038d6..8818c5fc 100644 --- a/rest/server.go +++ b/rest/server.go @@ -21,22 +21,18 @@ const topCpuUsage = 1000 var ErrSignatureConfig = errors.New("bad config for Signature") -type ( - Middleware func(next http.HandlerFunc) http.HandlerFunc - - server struct { - conf RtConf - routes []featuredRoutes - unauthorizedCallback handler.UnauthorizedCallback - unsignedCallback handler.UnsignedCallback - middlewares []Middleware - shedder load.Shedder - priorityShedder load.Shedder - } -) - -func newServer(c RtConf) *server { - srv := &server{ +type engine struct { + conf RestConf + routes []featuredRoutes + unauthorizedCallback handler.UnauthorizedCallback + unsignedCallback handler.UnsignedCallback + middlewares []Middleware + shedder load.Shedder + priorityShedder load.Shedder +} + +func newEngine(c RestConf) *engine { + srv := &engine{ conf: c, } if c.CpuThreshold > 0 { @@ -48,23 +44,23 @@ func newServer(c RtConf) *server { return srv } -func (s *server) AddRoutes(r featuredRoutes) { +func (s *engine) AddRoutes(r featuredRoutes) { s.routes = append(s.routes, r) } -func (s *server) SetUnauthorizedCallback(callback handler.UnauthorizedCallback) { +func (s *engine) SetUnauthorizedCallback(callback handler.UnauthorizedCallback) { s.unauthorizedCallback = callback } -func (s *server) SetUnsignedCallback(callback handler.UnsignedCallback) { +func (s *engine) SetUnsignedCallback(callback handler.UnsignedCallback) { s.unsignedCallback = callback } -func (s *server) Start() error { +func (s *engine) Start() error { return s.StartWithRouter(router.NewPatRouter()) } -func (s *server) StartWithRouter(router router.Router) error { +func (s *engine) StartWithRouter(router router.Router) error { if err := s.bindRoutes(router); err != nil { return err } @@ -72,7 +68,7 @@ func (s *server) StartWithRouter(router router.Router) error { return internal.StartHttp(s.conf.Host, s.conf.Port, router) } -func (s *server) appendAuthHandler(fr featuredRoutes, chain alice.Chain, +func (s *engine) appendAuthHandler(fr featuredRoutes, chain alice.Chain, verifier func(alice.Chain) alice.Chain) alice.Chain { if fr.jwt.enabled { if len(fr.jwt.prevSecret) == 0 { @@ -88,7 +84,7 @@ func (s *server) appendAuthHandler(fr featuredRoutes, chain alice.Chain, return verifier(chain) } -func (s *server) bindFeaturedRoutes(router router.Router, fr featuredRoutes, metrics *stat.Metrics) error { +func (s *engine) bindFeaturedRoutes(router router.Router, fr featuredRoutes, metrics *stat.Metrics) error { verifier, err := s.signatureVerifier(fr.signature) if err != nil { return err @@ -103,7 +99,7 @@ func (s *server) bindFeaturedRoutes(router router.Router, fr featuredRoutes, met return nil } -func (s *server) bindRoute(fr featuredRoutes, router router.Router, metrics *stat.Metrics, +func (s *engine) bindRoute(fr featuredRoutes, router router.Router, metrics *stat.Metrics, route Route, verifier func(chain alice.Chain) alice.Chain) error { chain := alice.New( handler.TracingHandler, @@ -128,7 +124,7 @@ func (s *server) bindRoute(fr featuredRoutes, router router.Router, metrics *sta return router.Handle(route.Method, route.Path, handle) } -func (s *server) bindRoutes(router router.Router) error { +func (s *engine) bindRoutes(router router.Router) error { metrics := s.createMetrics() for _, fr := range s.routes { @@ -140,7 +136,7 @@ func (s *server) bindRoutes(router router.Router) error { return nil } -func (s *server) createMetrics() *stat.Metrics { +func (s *engine) createMetrics() *stat.Metrics { var metrics *stat.Metrics if len(s.conf.Name) > 0 { @@ -152,7 +148,7 @@ func (s *server) createMetrics() *stat.Metrics { return metrics } -func (s *server) getLogHandler() func(http.Handler) http.Handler { +func (s *engine) getLogHandler() func(http.Handler) http.Handler { if s.conf.Verbose { return handler.DetailedLogHandler } else { @@ -160,14 +156,14 @@ func (s *server) getLogHandler() func(http.Handler) http.Handler { } } -func (s *server) getShedder(priority bool) load.Shedder { +func (s *engine) getShedder(priority bool) load.Shedder { if priority && s.priorityShedder != nil { return s.priorityShedder } return s.shedder } -func (s *server) signatureVerifier(signature signatureSetting) (func(chain alice.Chain) alice.Chain, error) { +func (s *engine) signatureVerifier(signature signatureSetting) (func(chain alice.Chain) alice.Chain, error) { if !signature.enabled { return func(chain alice.Chain) alice.Chain { return chain @@ -207,7 +203,7 @@ func (s *server) signatureVerifier(signature signatureSetting) (func(chain alice }, nil } -func (s *server) use(middleware Middleware) { +func (s *engine) use(middleware Middleware) { s.middlewares = append(s.middlewares, middleware) } diff --git a/rest/types.go b/rest/types.go index 14602d36..81418831 100644 --- a/rest/types.go +++ b/rest/types.go @@ -3,12 +3,16 @@ package rest import "net/http" type ( + Middleware func(next http.HandlerFunc) http.HandlerFunc + Route struct { Method string Path string Handler http.HandlerFunc } + RouteOption func(r *featuredRoutes) + jwtSetting struct { enabled bool secret string @@ -26,6 +30,4 @@ type ( signature signatureSetting routes []Route } - - RouteOption func(r *featuredRoutes) ) diff --git a/tools/goctl/api/demo/config/config.go b/tools/goctl/api/demo/config/config.go index ab02995e..5b258216 100644 --- a/tools/goctl/api/demo/config/config.go +++ b/tools/goctl/api/demo/config/config.go @@ -3,5 +3,5 @@ package config import "zero/rest" type Config struct { - rest.RtConf + rest.RestConf } diff --git a/tools/goctl/api/demo/demo.go b/tools/goctl/api/demo/demo.go index c7a9543e..1ef5f194 100644 --- a/tools/goctl/api/demo/demo.go +++ b/tools/goctl/api/demo/demo.go @@ -17,7 +17,7 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - engine := rest.MustNewEngine(c.RtConf) + engine := rest.MustNewServer(c.RestConf) defer engine.Stop() handler.RegisterHandlers(engine) diff --git a/tools/goctl/api/demo/handler/handlers.go b/tools/goctl/api/demo/handler/handlers.go index 04220fea..0f37a57b 100644 --- a/tools/goctl/api/demo/handler/handlers.go +++ b/tools/goctl/api/demo/handler/handlers.go @@ -6,7 +6,7 @@ import ( "zero/rest" ) -func RegisterHandlers(engine *rest.Engine) { +func RegisterHandlers(engine *rest.Server) { engine.AddRoutes([]rest.Route{ { Method: http.MethodGet, diff --git a/tools/goctl/api/gogen/genconfig.go b/tools/goctl/api/gogen/genconfig.go index 129f3d02..d72b44bf 100644 --- a/tools/goctl/api/gogen/genconfig.go +++ b/tools/goctl/api/gogen/genconfig.go @@ -18,7 +18,7 @@ import ( ) type Config struct { - rest.RtConf + rest.RestConf } ` ) diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 750a5b75..cfc29fa1 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -30,7 +30,7 @@ func main() { ctx := svc.NewServiceContext(c) - engine := rest.MustNewEngine(c.RtConf) + engine := rest.MustNewEngine(c.RestConf) defer engine.Stop() handler.RegisterHandlers(engine, ctx)