Documentation
¶
Index ¶
- type Config
- type Context
- type Endpoint
- type Handler
- type Server
- func (s *Server) AttachLogger(logger logger.Logger)
- func (s *Server) Register(name string, handler Handler)
- func (s *Server) RemoveErrorHandler(status int)
- func (s *Server) Run(addr string) error
- func (s *Server) RunWithShutdown(addr string, shutdownTimeout time.Duration) error
- func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (s *Server) SetErrorHandler(status int, handler Handler)
- func (s *Server) UseConfig(config Config)
- func (s *Server) UseMiddleware(handler Handler)
- func (s *Server) UseMiddlewares(handlers ...Handler)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
Config is a map that matches endpoints to Service.
func (*Config) Add ¶
Add links an endpoint to a Service by name. If the path does not end with a slash and an asterisk ("/*"), only requests that match the path EXACTLY will be handled by the Service. Paths ending with "/*" is considered as a prefix.
For example:
"/api/echo" can be handled by "/api/echo" or "/api/*", but not "/api" or "/".
If multiple prefixes exist, the prefix that matches the most will be the handler.
For example:
"/api/foo/bar" will be handled by "/api/foo/*" but not "/api/*".
The Service name of an endpoint should be as specific as possible and should not contain asterisk (*).
type Context ¶
type Context struct { // Request is the pointer to the http.Request. Request *http.Request // StatusCode holds the status code of the response. StatusCode int // Response holds the response body. Response []byte // Header holds HTTP headers in the response. Header http.Header // Data is a map that holds data of any type for value exchange between middlewares and main handler. Data map[string]interface{} // Logger is the current logger used in this context. Logger logger.Logger // contains filtered or unexported fields }
Context is the context of a request.
func (*Context) GetServiceName ¶
GetServiceName gets Service name of the request.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct for HTTP router.
func (*Server) AttachLogger ¶
func (s *Server) AttachLogger(logger logger.Logger)
AttachLogger attaches logger to the Server.
func (*Server) Register ¶
Register registers a service.
Service is the configuration of one Service. The key is the identifier of the Service, separated by dots, with parent Service before sub Service. For example: "foo.bar.baz".
The value is the handler function of a Service.
A Service can be handled by a more generic Service name (the request of which can be forwarded to other Service). For example: "foo.bar" can handle "foo.bar.baz" requests. But "foo.bar.baz" cannot handle "foo.bar".
An asterisk (*) means a Service handler for all Service, if there is no other Service that are more specific.
func (*Server) RemoveErrorHandler ¶
RemoveErrorHandler removes error handler of the given HTTP status code.
func (*Server) RunWithShutdown ¶
RunWithShutdown starts the server with the current Config. It catches a SIGINT or SIGTERM as shutdown signal.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP serves HTTP requests.
func (*Server) SetErrorHandler ¶
SetErrorHandler sets error handler of the given HTTP status code.
func (*Server) UseMiddleware ¶
UseMiddleware registers a middleware.
func (*Server) UseMiddlewares ¶
UseMiddlewares registers middlewares.