server

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: MIT Imports: 24 Imported by: 0

README

Go Server Last release

Go Report Card

Branch Status Coverage
master Build Status Coveralls
develop Build Status Coveralls

HTTP Server Router with middleware

Example

import "github.com/euskadi31/go-server"

router := server.NewRouter()

router.EnableMetrics()
router.EnableCors()
router.EnableHealthCheck()

router.AddHealthCheck("my-health-check", NewMyHealthCheck())

router.Use(MyMiddleWare())

router.AddController(MyController())

panic(http.ListenAndServe(":1337", router))

License

go-server is licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Failure added in v1.4.0

func Failure(w http.ResponseWriter, status int, err ErrorMessage)

Failure response

func FailureFromError added in v1.4.0

func FailureFromError(w http.ResponseWriter, status int, err error)

FailureFromError write ErrorMessage from error

func FailureFromValidator added in v1.4.0

func FailureFromValidator(w http.ResponseWriter, result *validate.Result)

FailureFromValidator response

func InternalServerFailure added in v1.4.0

func InternalServerFailure(w http.ResponseWriter, r *http.Request, p interface{})

InternalServerFailure response

func JSON added in v1.4.0

func JSON(w http.ResponseWriter, code int, body interface{})

JSON response

func MethodNotAllowedFailure added in v1.4.0

func MethodNotAllowedFailure(w http.ResponseWriter, r *http.Request)

MethodNotAllowedFailure response

func NewErrSchemaFileFormatNotSupported added in v1.4.0

func NewErrSchemaFileFormatNotSupported(ext string) error

NewErrSchemaFileFormatNotSupported error

func NewErrSchemaNotFound added in v1.4.0

func NewErrSchemaNotFound(name string) error

NewErrSchemaNotFound error

func NotFoundFailure added in v1.4.0

func NotFoundFailure(w http.ResponseWriter, r *http.Request)

NotFoundFailure response

func ResponseStatus added in v1.4.0

func ResponseStatus(w http.ResponseWriter) int

ResponseStatus returns the HTTP response status. Remember that the status is only set by the server after WriteHeader has been called.

func ServiceUnavailableFailure added in v1.4.0

func ServiceUnavailableFailure(w http.ResponseWriter, retry time.Duration)

ServiceUnavailableFailure response

Types

type Controller

type Controller interface {
	Mount(r *Router)
}

Controller interface

type ErrSchemaFileFormatNotSupported added in v1.4.0

type ErrSchemaFileFormatNotSupported struct {
	Ext string
}

ErrSchemaFileFormatNotSupported type

func (*ErrSchemaFileFormatNotSupported) Error added in v1.4.0

type ErrSchemaNotFound added in v1.4.0

type ErrSchemaNotFound struct {
	Name string
}

ErrSchemaNotFound type

func (*ErrSchemaNotFound) Error added in v1.4.0

func (e *ErrSchemaNotFound) Error() string

type ErrorMessage added in v1.4.0

type ErrorMessage struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ErrorMessage struct

func (ErrorMessage) Error added in v1.4.0

func (e ErrorMessage) Error() string

Error implements ErrorMessageInterface

func (ErrorMessage) GetCode added in v1.4.0

func (e ErrorMessage) GetCode() int

GetCode implements ErrorMessageInterface

func (ErrorMessage) GetMessage added in v1.4.0

func (e ErrorMessage) GetMessage() string

GetMessage implements ErrorMessageInterface

type ErrorMessageInterface added in v1.4.0

type ErrorMessageInterface interface {
	GetCode() int
	GetMessage() string
	Error() string
}

ErrorMessageInterface interface

type ErrorResponse added in v1.4.0

type ErrorResponse struct {
	Error ErrorMessage `json:"error"`
}

ErrorResponse struct

type ErrorsResponse added in v1.4.0

type ErrorsResponse struct {
	Errors []error `json:"errors"`
}

ErrorsResponse struct

type HealthCheckHandler

type HealthCheckHandler func(context.Context) bool

HealthCheckHandler type

type HealthCheckResponse

type HealthCheckResponse struct {
	Status   bool            `json:"status"`
	Services map[string]bool `json:"services"`
}

HealthCheckResponse struct

type Router

type Router struct {
	*mux.Router
	// contains filtered or unexported fields
}

Router struct

func NewRouter

func NewRouter() *Router

NewRouter constructor

func (*Router) AddController

func (r *Router) AddController(controller Controller)

AddController to Router

func (*Router) AddHealthCheck

func (r *Router) AddHealthCheck(name string, handle HealthCheckHandler) error

AddHealthCheck handler

func (*Router) AddPrefixRoute

func (r *Router) AddPrefixRoute(prefix string, handler http.Handler) *mux.Route

AddPrefixRoute to Router

func (*Router) AddPrefixRouteFunc

func (r *Router) AddPrefixRouteFunc(prefix string, handler http.HandlerFunc) *mux.Route

AddPrefixRouteFunc to Router

func (*Router) AddRoute

func (r *Router) AddRoute(path string, handler http.Handler) *mux.Route

AddRoute to Router

func (*Router) AddRouteFunc

func (r *Router) AddRouteFunc(path string, handler http.HandlerFunc) *mux.Route

AddRouteFunc to Router

func (*Router) EnableCors

func (r *Router) EnableCors()

EnableCors for all endpoint

func (*Router) EnableCorsWithOptions

func (r *Router) EnableCorsWithOptions(options cors.Options)

EnableCorsWithOptions for all endpoint

func (*Router) EnableHealthCheck

func (r *Router) EnableHealthCheck()

EnableHealthCheck endpoint

func (*Router) EnableMetrics

func (r *Router) EnableMetrics()

EnableMetrics endpoint

func (*Router) EnableRecovery

func (r *Router) EnableRecovery()

EnableRecovery for all endpoint

func (*Router) SetNotFound

func (r *Router) SetNotFound(handler http.Handler)

SetNotFound handler

func (*Router) SetNotFoundFunc

func (r *Router) SetNotFoundFunc(handler http.HandlerFunc)

SetNotFoundFunc handler

func (*Router) Use added in v1.4.0

func (r *Router) Use(middleware ...alice.Constructor)

Use middleware

type SchemaValidator added in v1.4.0

type SchemaValidator interface {
	SchemaValidator() *spec.Schema
}

SchemaValidator interface experimental

type Validator added in v1.4.0

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

Validator struct

func NewValidator added in v1.4.0

func NewValidator() *Validator

NewValidator constructor

func (*Validator) AddSchemFromObject added in v1.4.0

func (v *Validator) AddSchemFromObject(object SchemaValidator) error

AddSchemFromObject experimental

func (*Validator) AddSchemFromObjectName added in v1.4.0

func (v *Validator) AddSchemFromObjectName(name string, object SchemaValidator) error

AddSchemFromObjectName experimental

func (*Validator) AddSchema added in v1.4.0

func (v *Validator) AddSchema(name string, schema *spec.Schema) error

AddSchema by name

func (*Validator) AddSchemaFromFile added in v1.4.0

func (v *Validator) AddSchemaFromFile(name string, filename string) error

AddSchemaFromFile name

func (*Validator) AddSchemaFromJSON added in v1.4.0

func (v *Validator) AddSchemaFromJSON(name string, content json.RawMessage) error

AddSchemaFromJSON string

func (*Validator) AddSchemaFromReader added in v1.4.0

func (v *Validator) AddSchemaFromReader(name string, format string, reader io.Reader) error

AddSchemaFromReader func

func (*Validator) AddSchemaFromYAML added in v1.4.0

func (v *Validator) AddSchemaFromYAML(name string, content []byte) error

AddSchemaFromYAML string

func (Validator) Validate added in v1.4.0

func (v Validator) Validate(name string, data interface{}) *validate.Result

Validate data

type ValidatorError added in v1.4.0

type ValidatorError struct {
	Code    int32         `json:"code,omitempty"`
	Name    string        `json:"name,omitempty"`
	In      string        `json:"in,omitempty"`
	Value   interface{}   `json:"value,omitempty"`
	Message string        `json:"message"`
	Values  []interface{} `json:"values,omitempty"`
}

ValidatorError struct

func (ValidatorError) Error added in v1.4.0

func (e ValidatorError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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