server

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2018 License: MIT Imports: 24 Imported by: 21

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

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

Failure response

func FailureFromError

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

FailureFromError write ErrorMessage from error

func FailureFromValidator

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

FailureFromValidator response

func InternalServerFailure

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

InternalServerFailure response

func JSON

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

JSON response

func MethodNotAllowedFailure

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

MethodNotAllowedFailure response

func NewErrSchemaFileFormatNotSupported

func NewErrSchemaFileFormatNotSupported(ext string) error

NewErrSchemaFileFormatNotSupported error

func NewErrSchemaNotFound

func NewErrSchemaNotFound(name string) error

NewErrSchemaNotFound error

func NotFoundFailure

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

NotFoundFailure response

func ResponseStatus

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

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

ServiceUnavailableFailure response

Types

type Controller

type Controller interface {
	Mount(r *Router)
}

Controller interface

type ErrSchemaFileFormatNotSupported

type ErrSchemaFileFormatNotSupported struct {
	Ext string
}

ErrSchemaFileFormatNotSupported type

func (*ErrSchemaFileFormatNotSupported) Error

type ErrSchemaNotFound

type ErrSchemaNotFound struct {
	Name string
}

ErrSchemaNotFound type

func (*ErrSchemaNotFound) Error

func (e *ErrSchemaNotFound) Error() string

type ErrorMessage

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

ErrorMessage struct

func (ErrorMessage) Error

func (e ErrorMessage) Error() string

Error implements ErrorMessageInterface

func (ErrorMessage) GetCode

func (e ErrorMessage) GetCode() int

GetCode implements ErrorMessageInterface

func (ErrorMessage) GetMessage

func (e ErrorMessage) GetMessage() string

GetMessage implements ErrorMessageInterface

type ErrorMessageInterface

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

ErrorMessageInterface interface

type ErrorResponse

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

ErrorResponse struct

type ErrorsResponse

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

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

Use middleware

type SchemaValidator

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

SchemaValidator interface experimental

type Validator

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

Validator struct

func NewValidator

func NewValidator() *Validator

NewValidator constructor

func (*Validator) AddSchemFromObject

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

AddSchemFromObject experimental

func (*Validator) AddSchemFromObjectName

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

AddSchemFromObjectName experimental

func (*Validator) AddSchema

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

AddSchema by name

func (*Validator) AddSchemaFromFile

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

AddSchemaFromFile name

func (*Validator) AddSchemaFromJSON

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

AddSchemaFromJSON string

func (*Validator) AddSchemaFromReader

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

AddSchemaFromReader func

func (*Validator) AddSchemaFromYAML

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

AddSchemaFromYAML string

func (Validator) Validate

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

Validate data

type ValidatorError

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

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