httpservice

package
v0.0.0-...-93b1e75 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
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

type ConfigService interface {
	GetByKey(string, interface{}) error
	IsNoSuchKeyError(error) bool
}

ConfigService interface used to obtain configuration from somewhere into some specific structure.

type HandlerFunc

type HandlerFunc func(ctx *context.Context) error

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

type Option func(*Config) error

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 WithDebug

func WithDebug(debug bool) Option

WithDebug option sets debug mode. Default: false.

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger option applies provided logger. Default: standard logger with name equal to the service's one.

func WithName

func WithName(name string) Option

WithName option applies provided service name. Default: "http-service".

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 MustNew

func MustNew(opts ...Option) *Service

MustNew function creates new service with provided options and panics on any error.

func New

func New(opts ...Option) (*Service, error)

New function creates new service with provided options.

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) Debug

func (service *Service) Debug() bool

Debug method retrieves service's debug flag.

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) Logger

func (service *Service) Logger() log.Logger

Logger method retrieves service's logger object.

func (*Service) Name

func (service *Service) Name() string

Name method retrieves service's name.

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

func (service *Service) UseDecoder(dec decoder.Decoder)

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

func (service *Service) UseEncoder(enc encoder.Encoder)

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.

Jump to

Keyboard shortcuts

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