|
|
@ -29,12 +29,12 @@ type (
|
|
|
|
// Be aware that later RunOption might overwrite previous one that write the same option.
|
|
|
|
// Be aware that later RunOption might overwrite previous one that write the same option.
|
|
|
|
// The process will exit if error occurs.
|
|
|
|
// The process will exit if error occurs.
|
|
|
|
func MustNewServer(c RestConf, opts ...RunOption) *Server {
|
|
|
|
func MustNewServer(c RestConf, opts ...RunOption) *Server {
|
|
|
|
engine, err := NewServer(c, opts...)
|
|
|
|
server, err := NewServer(c, opts...)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return engine
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewServer returns a server with given config of c and options defined in opts.
|
|
|
|
// NewServer returns a server with given config of c and options defined in opts.
|
|
|
@ -61,36 +61,36 @@ func NewServer(c RestConf, opts ...RunOption) (*Server, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddRoutes add given routes into the Server.
|
|
|
|
// AddRoutes add given routes into the Server.
|
|
|
|
func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) {
|
|
|
|
func (s *Server) AddRoutes(rs []Route, opts ...RouteOption) {
|
|
|
|
r := featuredRoutes{
|
|
|
|
r := featuredRoutes{
|
|
|
|
routes: rs,
|
|
|
|
routes: rs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&r)
|
|
|
|
opt(&r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.ngin.AddRoutes(r)
|
|
|
|
s.ngin.AddRoutes(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddRoute adds given route into the Server.
|
|
|
|
// AddRoute adds given route into the Server.
|
|
|
|
func (e *Server) AddRoute(r Route, opts ...RouteOption) {
|
|
|
|
func (s *Server) AddRoute(r Route, opts ...RouteOption) {
|
|
|
|
e.AddRoutes([]Route{r}, opts...)
|
|
|
|
s.AddRoutes([]Route{r}, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start starts the Server.
|
|
|
|
// Start starts the Server.
|
|
|
|
// Graceful shutdown is enabled by default.
|
|
|
|
// Graceful shutdown is enabled by default.
|
|
|
|
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
|
|
|
|
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
|
|
|
|
func (e *Server) Start() {
|
|
|
|
func (s *Server) Start() {
|
|
|
|
handleError(e.opts.start(e.ngin))
|
|
|
|
handleError(s.opts.start(s.ngin))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stop stops the Server.
|
|
|
|
// Stop stops the Server.
|
|
|
|
func (e *Server) Stop() {
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
logx.Close()
|
|
|
|
logx.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Use adds the given middleware in the Server.
|
|
|
|
// Use adds the given middleware in the Server.
|
|
|
|
func (e *Server) Use(middleware Middleware) {
|
|
|
|
func (s *Server) Use(middleware Middleware) {
|
|
|
|
e.ngin.use(middleware)
|
|
|
|
s.ngin.use(middleware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ToMiddleware converts the given handler to a Middleware.
|
|
|
|
// ToMiddleware converts the given handler to a Middleware.
|
|
|
|