Documentation ¶
Overview ¶
Package httpservice provides simple http framework for generic http service. In general you will include instance of Service structure into your own specific service to reduce amount of written code.
Typical simplified usage (in tandem with httpserver package):
srv := httpservice.New(httpservice.WithLogger(logger)) srv.UseMiddleware(middleware.Recover) srv.UseMiddleware(middleware.Log) srv.GET("/", "index", func (ctx *context.Context) error { // ... Handle request }) // ... Define other routes httpserver.New(httpserver.WithHandler(srv)).Run(ctx)
Index ¶
- Constants
- type Config
- type ConfigService
- type HandlerFunc
- type MiddlewareFunc
- type Option
- type Service
- func (service *Service) DELETE(path string, action string, handler HandlerFunc)
- func (service *Service) Debug() bool
- func (service *Service) GET(path string, action string, handler HandlerFunc)
- func (service *Service) Logger() log.Logger
- func (service *Service) Name() string
- func (service *Service) OPTIONS(path string, action string, handler HandlerFunc)
- func (service *Service) POST(path string, action string, handler HandlerFunc)
- func (service *Service) PUT(path string, action string, handler HandlerFunc)
- func (service *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (service *Service) UseDecoder(dec decoder.Decoder)
- func (service *Service) UseEncoder(enc encoder.Encoder)
- func (service *Service) UseMiddleware(fn MiddlewareFunc)
- func (service *Service) UseMiddlewareBefore(fn MiddlewareFunc)
Constants ¶
const DefName = "http-service"
DefName is the default service's name.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config structure with service's configuration. Shouldn't be created manually.
type ConfigService ¶
ConfigService interface used to obtain configuration from somewhere into some specific structure.
type HandlerFunc ¶
HandlerFunc is a function that will handle each request under some route with all used middlewares. If it returns an error, it will be written into the log and the internal server response will be sent to the client.
type MiddlewareFunc ¶
type MiddlewareFunc func(HandlerFunc) HandlerFunc
MiddlewareFunc is a function that will wrap request handler to provide some shared functionality.
type Option ¶
Option function that is fed to New and MustNew. Obtain them using 'With' functions down below.
func WithConfig ¶
func WithConfig(service ConfigService, key string) Option
WithConfig option retrieves configuration from provided configuration service.
Example JSON configuration with all possible fields (if some are not present, defaults will be used):
{ "name": "service-name", "debug": true }
func WithLogger ¶
WithLogger option applies provided logger. Default: standard logger with name equal to the service's one.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service structure that provides http service functionality. Don't create manually, use the functions down below instead.
func (*Service) DELETE ¶
func (service *Service) DELETE(path string, action string, handler HandlerFunc)
DELETE method applies handler for all DELETE requests coming to provided path. Action is an arbitrary string that identifies this specific handler (used in logs for example).
func (*Service) GET ¶
func (service *Service) GET(path string, action string, handler HandlerFunc)
GET method applies handler for all GET requests coming to provided path. Action is an arbitrary string that identifies this specific handler (used in logs for example).
func (*Service) OPTIONS ¶
func (service *Service) OPTIONS(path string, action string, handler HandlerFunc)
OPTIONS method applies handler for all OPTIONS requests coming to provided path. Action is an arbitrary string that identifies this specific handler (used in logs for example).
func (*Service) POST ¶
func (service *Service) POST(path string, action string, handler HandlerFunc)
POST method applies handler for all POST requests coming to provided path. Action is an arbitrary string that identifies this specific handler (used in logs for example).
func (*Service) PUT ¶
func (service *Service) PUT(path string, action string, handler HandlerFunc)
PUT method applies handler for all PUT requests coming to provided path. Action is an arbitrary string that identifies this specific handler (used in logs for example).
func (*Service) ServeHTTP ¶
func (service *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP method implements http.Handler interface.
func (*Service) UseDecoder ¶
UseDecoder method applies provided decoder that will be used for each route defined after that. Example:
srv.UseDecoder(dec1) srv.GET(...) // will use decoder dec1 srv.GET(...) // will use decoder dec1 too srv.UseDecoder(dec2) srv.GET(...) // will use decoder dec2
func (*Service) UseEncoder ¶
UseEncoder method applies provided encoder that will be used for each route defined after that. Example:
srv.UseEncoder(enc1) srv.GET(...) // will use encoder enc1 srv.GET(...) // will use encoder enc1 too srv.UseEncoder(enc2) srv.GET(...) // will use encoder enc2
func (*Service) UseMiddleware ¶
func (service *Service) UseMiddleware(fn MiddlewareFunc)
UseMiddleware method applies provided middleware function that will wrap all handlers. If you call this method several times, each subsequent middleware will be applied after the previous ones.
func (*Service) UseMiddlewareBefore ¶
func (service *Service) UseMiddlewareBefore(fn MiddlewareFunc)
UseMiddlewareBefore method applies provided middleware function that will wrap all handlers. If you call this method several times, each subsequent middleware will be applied before the previous ones.
Directories ¶
Path | Synopsis |
---|---|
Package context provides context object for data that belongs to an individual http request.
|
Package context provides context object for data that belongs to an individual http request. |
Package decoder provides request decoding functionality for the context.
|
Package decoder provides request decoding functionality for the context. |
Package encoder provides response encoding functionality for the context package.
|
Package encoder provides response encoding functionality for the context package. |
Package middleware provides commonly-used middleware functions for the httpservice package.
|
Package middleware provides commonly-used middleware functions for the httpservice package. |
Package response provides wrapper around http.ResponseWriter used inside context object, also it provides some commonly-used response payloads.
|
Package response provides wrapper around http.ResponseWriter used inside context object, also it provides some commonly-used response payloads. |