web

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 14 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMiddlewareKey added in v0.4.0

func GetMiddlewareKey(group, name string) string

GetMiddlewareKey returns a unique key for middleware

Types

type HandlerManager added in v0.4.0

type HandlerManager struct {
	// contains filtered or unexported fields
}

HandlerManager is a manager about middleware new function and shutdown function, and auto calls those functions when needed; it tries to keep the middleware are immutable.

If you want to get middleware information in your application, and you know that your middleware is immutable, or you can control it, you can store it in the cache for reuse.

func NewHandlerManager added in v0.4.0

func NewHandlerManager() *HandlerManager

NewHandlerManager creates a new middleware manager, initialize common useful middlewares.

func (*HandlerManager) Get added in v0.4.0

func (*HandlerManager) GetMiddleware added in v0.4.0

func (m *HandlerManager) GetMiddleware(key string) (handler.Middleware, bool)

GetMiddleware returns a middleware instance by key. Should not change the middleware's option value and keep middleware run immutable.

func (*HandlerManager) Register added in v0.4.0

func (m *HandlerManager) Register(name string, handler handler.MiddlewareNewFunc)

Register a middleware new function.

func (*HandlerManager) RegisterMiddleware added in v0.4.0

func (m *HandlerManager) RegisterMiddleware(key string, mid handler.Middleware)

RegisterMiddleware register a middleware instance. Should call it after Register. Keep the key unique.

func (*HandlerManager) Shutdown added in v0.4.0

func (m *HandlerManager) Shutdown(ctx context.Context) error

Shutdown a handler if handler base on file,net such as a need to release resource

type Option

type Option func(s *ServerOptions)

Option the function to apply a configuration option

func WithConfiguration added in v0.1.0

func WithConfiguration(cfg *conf.Configuration) Option

WithConfiguration set up the configuration of the web server by a configuration instance

func WithGracefulStop added in v0.1.0

func WithGracefulStop() Option

WithGracefulStop indicate use graceful stop

func WithMiddlewareApplyFunc added in v0.4.0

func WithMiddlewareApplyFunc(name string, handlerFunc handler.MiddlewareApplyFunc) Option

WithMiddlewareApplyFunc provide a simple way to inject middleware by gin.HandlerFunc.

Notice: the middleware usual attach `c.Next()` or `c.Abort` to indicator whether exits the method. example:

	RegisterMiddleware("test", func(cfg *conf.Configuration){
     // use cfg to init
	    return func(c *gin.Context) {
	        // ....process
	        c.Next() or c.Abort() or c.AbortWithStatus(500)
	    }
	})

func WithMiddlewareNewFunc added in v0.4.0

func WithMiddlewareNewFunc(name string, newFunc handler.MiddlewareNewFunc) Option

WithMiddlewareNewFunc provide a simple way to inject middleware by MiddlewareNewFunc.

type Router

type Router struct {
	*gin.Engine
	Groups []*RouterGroup
	// contains filtered or unexported fields
}

Router is base on Gin.

func NewRouter

func NewRouter(options *ServerOptions) *Router

func (*Router) Apply

func (r *Router) Apply(cnf *conf.Configuration) (err error)

Apply implements the conf.Configurable interface.

RouterGroups and Middlewares must init by order, so we use array-type in configuration.

func (*Router) FindGroup added in v0.0.3

func (r *Router) FindGroup(basePath string) *RouterGroup

FindGroup return a specified router group by an url format base path.

parameter basePath is map to configuration:

routerGroups:
- group:
  basePath: "/auth"

type RouterGroup added in v0.0.3

type RouterGroup struct {
	Group *gin.RouterGroup

	Router *Router
	// contains filtered or unexported fields
}

RouterGroup is a wrapper for gin.RouterGroup.

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Server

New create a web server

func (*Server) Apply

func (s *Server) Apply(cfg *conf.Configuration) error

Apply implement conf.Configuration

func (*Server) HandlerManager added in v0.0.3

func (s *Server) HandlerManager() *HandlerManager

HandlerManager return server's handler manager,it's convenient to process handler

func (*Server) ListenAndServe added in v0.0.3

func (s *Server) ListenAndServe() (err error)

ListenAndServe Starts Http Server

return

http.ErrServerClosed or other error

func (*Server) Router

func (s *Server) Router() *Router

func (*Server) Run

func (s *Server) Run() error

Run builtin run the server.

you can process whole yourself

func (*Server) ServerOptions added in v0.0.3

func (s *Server) ServerOptions() ServerOptions

ServerOptions return a setting used by web server

func (*Server) Start added in v0.1.0

func (s *Server) Start(ctx context.Context) error

func (*Server) Stop added in v0.0.3

func (s *Server) Stop(ctx context.Context) error

Stop http server and clear resource

type ServerOptions added in v0.0.3

type ServerOptions struct {
	Addr string    `json:"addr" yaml:"addr"`
	TLS  *conf.TLS `json:"tls" yaml:"tls"`
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL