You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rest/ngin.go

171 lines
3.3 KiB
Go

package rest
4 years ago
import (
"log"
"net/http"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/rest/handler"
"github.com/tal-tech/go-zero/rest/httpx"
4 years ago
)
type (
runOptions struct {
start func(*engine) error
4 years ago
}
RunOption func(*Server)
4 years ago
Server struct {
ngin *engine
4 years ago
opts runOptions
}
)
func MustNewServer(c RestConf, opts ...RunOption) *Server {
engine, err := NewServer(c, opts...)
4 years ago
if err != nil {
log.Fatal(err)
}
return engine
}
func NewServer(c RestConf, opts ...RunOption) (*Server, error) {
4 years ago
if err := c.SetUp(); err != nil {
return nil, err
}
server := &Server{
ngin: newEngine(c),
4 years ago
opts: runOptions{
start: func(srv *engine) error {
4 years ago
return srv.Start()
},
},
}
for _, opt := range opts {
opt(server)
4 years ago
}
return server, nil
4 years ago
}
func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) {
4 years ago
r := featuredRoutes{
routes: rs,
}
for _, opt := range opts {
opt(&r)
}
e.ngin.AddRoutes(r)
4 years ago
}
func (e *Server) AddRoute(r Route, opts ...RouteOption) {
4 years ago
e.AddRoutes([]Route{r}, opts...)
}
func (e *Server) Start() {
handleError(e.opts.start(e.ngin))
4 years ago
}
func (e *Server) Stop() {
4 years ago
logx.Close()
}
func (e *Server) Use(middleware Middleware) {
e.ngin.use(middleware)
4 years ago
}
func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
return func(handle http.HandlerFunc) http.HandlerFunc {
return handler(handle).ServeHTTP
}
}
func WithJwt(secret string) RouteOption {
return func(r *featuredRoutes) {
validateSecret(secret)
r.jwt.enabled = true
r.jwt.secret = secret
}
}
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
}
}
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
}
func WithPriority() RouteOption {
return func(r *featuredRoutes) {
r.priority = true
}
}
func WithRouter(router httpx.Router) RunOption {
return func(server *Server) {
server.opts.start = func(srv *engine) error {
4 years ago
return srv.StartWithRouter(router)
}
}
}
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
}
}
4 years ago
func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption {
return func(engine *Server) {
engine.ngin.SetUnauthorizedCallback(callback)
4 years ago
}
}
4 years ago
func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
return func(engine *Server) {
engine.ngin.SetUnsignedCallback(callback)
4 years ago
}
}
func handleError(err error) {
// ErrServerClosed means the server is closed manually
if err == nil || 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")
}
}