Documentation ¶
Overview ¶
Package webfmwk implements an minimalist Go web framework
Example:
package main import ( w "github.com/burgesQ/webfmwk/v2" ) // Handler func hello(c w.IContext) error { return c.JSONOk("Hello, World!") } func main() { // Echo instance s := w.InitServer(true) // Routes s.GET("/hello", hello) // start server on :4242 go func() { s.Start(":4242") }() // ctrl+c is handled internaly defer s.WaitAndStop() }
Learn more at https://github.com/burgesQ/webfmwk
Index ¶
- Constants
- func GetLogger() log.ILog
- func Shutdown(ctx context.Context)
- type AnonymousError
- type Context
- func (c Context) CheckHeader()
- func (c Context) DecodeQP(dest interface{})
- func (c *Context) FetchContent(dest interface{})
- func (c *Context) GetQueries() map[string][]string
- func (c *Context) GetQuery(key string) (string, bool)
- func (c Context) GetVar(key string) string
- func (c Context) IsPretty() bool
- func (c *Context) JSON(statusCode int, content interface{})
- func (c *Context) JSONAccepted(content interface{})
- func (c *Context) JSONBadRequest(content interface{})
- func (c *Context) JSONBlob(statusCode int, content []byte)
- func (c *Context) JSONConflict(content interface{})
- func (c *Context) JSONCreated(content interface{})
- func (c *Context) JSONInternalError(content interface{})
- func (c *Context) JSONNoContent()
- func (c *Context) JSONNotFound(content interface{})
- func (c *Context) JSONNotImplemented(content interface{})
- func (c *Context) JSONOk(content interface{})
- func (c *Context) JSONUnprocessable(content interface{})
- func (c Context) OwnRecover()
- func (c *Context) SendResponse(statusCode int, content []byte, headers ...[2]string)
- func (c *Context) SetLogger(logger log.ILog)
- func (c *Context) SetQuery(q map[string][]string)
- func (c *Context) SetRequest(r *http.Request)
- func (c *Context) SetVars(v map[string]string)
- func (c *Context) SetWriter(w *http.ResponseWriter)
- func (c Context) Validate(dest interface{})
- type ErrorHandled
- func NewBadRequest(content interface{}) ErrorHandled
- func NewConflict(content interface{}) ErrorHandled
- func NewErrorHandled(op int, content interface{}) ErrorHandled
- func NewInternal(content interface{}) ErrorHandled
- func NewNoContent() ErrorHandled
- func NewNotAcceptable(content interface{}) ErrorHandled
- func NewNotFound(content interface{}) ErrorHandled
- func NewNotImplemented(content interface{}) ErrorHandled
- func NewProcessing(content interface{}) ErrorHandled
- func NewUnauthorized(content interface{}) ErrorHandled
- func NewUnprocessable(content interface{}) ErrorHandled
- type HandlerSign
- type IContext
- type IErrorHandled
- type Route
- type Routes
- type RoutesPerPrefix
- type Server
- func (s *Server) AddMiddleware(mw mux.MiddlewareFunc)
- func (s *Server) AddRoute(r Route)
- func (s *Server) AddRoutes(r Routes)
- func (s *Server) DELETE(path string, handler HandlerSign)
- func (s *Server) ExitHandler(ctx context.Context, sig ...os.Signal)
- func (s *Server) GET(path string, handler HandlerSign)
- func (s *Server) GetContext() *context.Context
- func (s *Server) GetLauncher() *WorkerLauncher
- func (s Server) GetLogger() log.ILog
- func (s *Server) PATCH(path string, handler HandlerSign)
- func (s *Server) POST(path string, handler HandlerSign)
- func (s *Server) PUT(path string, handler HandlerSign)
- func (s *Server) RegisterDocHandler(handler http.Handler)
- func (s *Server) RouteApplier(rpp RoutesPerPrefix)
- func (s *Server) SetCustomContext(setter func(c *Context) IContext) bool
- func (s *Server) SetLogger(lg log.ILog)
- func (s *Server) SetPrefix(prefix string)
- func (s *Server) SetRouter() *mux.Router
- func (s *Server) SetWorkerParams(w WorkerConfig)
- func (s *Server) Shutdown(ctx context.Context)
- func (s *Server) Start(addr string)
- func (s *Server) StartTLS(addr string, tlsStuffs TLSConfig)
- func (s *Server) WaitAndStop()
- type TLSConfig
- type ValidationError
- type WorkerConfig
- type WorkerLauncher
Constants ¶
const ( GET = "GET" POST = "POST" PATCH = "PATCH" PUT = "PUT" DELETE = "DELETE" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AnonymousError ¶
type AnonymousError struct {
Error string `json:"error"`
}
AnonymousError struct is used to answer error
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context implement the IContext interface It hold the data used by the request
func (Context) DecodeQP ¶ added in v2.3.6
func (c Context) DecodeQP(dest interface{})
DecodeQP implement IContext
func (*Context) FetchContent ¶
func (c *Context) FetchContent(dest interface{})
FetchContent implement IContext It load payload in the dest interface{} using the system json library
func (*Context) GetQueries ¶
GetQueries implement IContext
func (*Context) JSONAccepted ¶ added in v2.5.2
func (c *Context) JSONAccepted(content interface{})
JSONAccepted implement IContext
func (*Context) JSONBadRequest ¶
func (c *Context) JSONBadRequest(content interface{})
JSONBadRequest implement IContext
func (*Context) JSONConflict ¶
func (c *Context) JSONConflict(content interface{})
JSONConflict implement IContext
func (*Context) JSONCreated ¶
func (c *Context) JSONCreated(content interface{})
JSONCreated implement IContext
func (*Context) JSONInternalError ¶
func (c *Context) JSONInternalError(content interface{})
JSONInternalError implement IContext
func (*Context) JSONNotFound ¶
func (c *Context) JSONNotFound(content interface{})
JSONNotFound implement IContext
func (*Context) JSONNotImplemented ¶
func (c *Context) JSONNotImplemented(content interface{})
JSONNotImplemented implement IContext
func (*Context) JSONUnprocessable ¶
func (c *Context) JSONUnprocessable(content interface{})
JSONUnprocessable implement IContext
func (*Context) SendResponse ¶
Send Response implement IContext
func (*Context) SetRequest ¶
SetRequest implement IContext
func (*Context) SetWriter ¶
func (c *Context) SetWriter(w *http.ResponseWriter)
SetWriter implement IContext
type ErrorHandled ¶
type ErrorHandled struct {
// contains filtered or unexported fields
}
ErrorHandled implement the IErrorHandled interface
func NewBadRequest ¶
func NewBadRequest(content interface{}) ErrorHandled
NewBadRequest produce an ErrorHandled with the status code 400
func NewConflict ¶ added in v2.4.2
func NewConflict(content interface{}) ErrorHandled
NewConflict produce an ErrorHandled with the status code 501
func NewErrorHandled ¶ added in v2.3.7
func NewErrorHandled(op int, content interface{}) ErrorHandled
NewError return a new ErrorHandled var
func NewInternal ¶
func NewInternal(content interface{}) ErrorHandled
NewUnprocessable produce an ErrorHandled with the status code 500
func NewNoContent ¶
func NewNoContent() ErrorHandled
NewNoContent produce an ErrorHandled with the status code 204
func NewNotAcceptable ¶
func NewNotAcceptable(content interface{}) ErrorHandled
NewNotAcceptable produce an ErrorHandled with the status code 406
func NewNotFound ¶
func NewNotFound(content interface{}) ErrorHandled
NewNotAcceptable produce an ErrorHandled with the status code 404
func NewNotImplemented ¶ added in v2.3.0
func NewNotImplemented(content interface{}) ErrorHandled
NewUnprocessable produce an ErrorHandled with the status code 501
func NewProcessing ¶ added in v2.3.7
func NewProcessing(content interface{}) ErrorHandled
func NewUnauthorized ¶ added in v2.3.7
func NewUnauthorized(content interface{}) ErrorHandled
func NewUnprocessable ¶
func NewUnprocessable(content interface{}) ErrorHandled
NewUnprocessable produce an ErrorHandled with the status code 422
func (ErrorHandled) Error ¶ added in v2.3.7
func (e ErrorHandled) Error() string
func (ErrorHandled) GetContent ¶
func (e ErrorHandled) GetContent() interface{}
GetContent implement the IErrorHandled interface
func (ErrorHandled) GetOPCode ¶
func (e ErrorHandled) GetOPCode() int
GetOPCode implement the IErrorHandled interface
type HandlerSign ¶
type HandlerSign func(c IContext)
HandlerSign hold the signature of the controller
type IContext ¶
type IContext interface { // SetRequest is used to save the request object SetRequest(rq *http.Request) // SetWriter is used to save the ResponseWriter obj // TODO: use io.Writer ? SetWriter(rw *http.ResponseWriter) // FetchContent extract the content from the body FetchContent(content interface{}) // Validate is used to validate a content of the content params Validate(content interface{}) // Decode load the query param in the content object DecodeQP(content interface{}) // SetVars is used to save the url vars SetVars(vars map[string]string) // GetVar return the url var parameters. Empty string for none GetVar(key string) (val string) // SetQuery save the query param object SetQuery(query map[string][]string) // GetQueries return the queries object GetQueries() map[string][]string // GetQuery fetch the query object key GetQuery(key string) (val string, ok bool) // IsPretty toggle the compact outptu mode IsPretty() bool // SetLogger set the logger of the ctx SetLogger(logger log.ILog) // CheckHeader ensure the Content-Type of the request CheckHeader() // OwnRecover is used to encapsulate the wanted panic OwnRecover() // SendResponse create & send a response according to the parameters SendResponse(op int, content []byte, headers ...[2]string) // JSONBlob answer the JSON content with the status code op JSONBlob(op int, content []byte) // JSON answer the JSON content with the status code op JSON(op int, content interface{}) // JSONNotImplemented(interface{}) // JSONNoContent() // JSONBadRequest(interface{}) // 201 JSONCreated(interface{}) // JSONUnprocessable(interface{}) // 200 JSONOk(interface{}) // 404 JSONNotFound(interface{}) // JSONConflict(interface{}) // JSONInternalError(interface{}) // JSONAccepted(interface{}) }
IContext Interface implement the context used in this project
type IErrorHandled ¶
type IErrorHandled interface { GetOPCode() int GetContent() interface{} }
IErrorHandled interface implement the panic recovering
type Route ¶
type Route struct { Verbe string `json:"verbe"` Path string `json:"path"` Name string `json:"name"` Handler HandlerSign `json:"-"` }
Route hold the data for one route
type RoutesPerPrefix ¶ added in v2.4.0
RoutesPerPrefix hold the routes and there respectiv prefix
type Server ¶
type Server struct { CORS bool // contains filtered or unexported fields }
Server is a struct holding all the necessary data / struct
func InitServer ¶
InitServer set the server struct & pre-launch the exit handler. Init the worker internal launcher. If withCtrl is set to true, the server will handle ctrl+C internall.y Please add worker to the package's WorkerLauncher to sync them.
func (*Server) AddMiddleware ¶
func (s *Server) AddMiddleware(mw mux.MiddlewareFunc)
Enamelware append a middleware to the list of middleware
func (*Server) DELETE ¶
func (s *Server) DELETE(path string, handler HandlerSign)
DELETE expose a route to the http verb DELETE
func (*Server) ExitHandler ¶
ExitHandler handle ctrl+c in intern
func (*Server) GET ¶
func (s *Server) GET(path string, handler HandlerSign)
GET expose a route to the http verb GET
func (*Server) GetContext ¶
GetContext return a pointer on the context.Context used
func (*Server) GetLauncher ¶
func (s *Server) GetLauncher() *WorkerLauncher
GetLauncher return a pointer on the util.workerLauncher used
func (*Server) PATCH ¶
func (s *Server) PATCH(path string, handler HandlerSign)
PATCH expose a route to the http verb PATCH
func (*Server) POST ¶
func (s *Server) POST(path string, handler HandlerSign)
POST expose a route to the http verb POST
func (*Server) PUT ¶
func (s *Server) PUT(path string, handler HandlerSign)
PUT expose a route to the http verb PUT
func (*Server) RegisterDocHandler ¶
RegisterDocHandler is used to save an swagger doc handler
func (*Server) RouteApplier ¶ added in v2.4.0
func (s *Server) RouteApplier(rpp RoutesPerPrefix)
RouteApplier apply the array of RoutePerPrefix
func (*Server) SetCustomContext ¶
SetCustomContext save a custom context so it can be fetched in the controller handler
func (*Server) SetRouter ¶
SetRouter create a mux.Handler router and then : register the middlewares, register the user defined routes per prefix, and return the routes handler
func (*Server) SetWorkerParams ¶ added in v2.3.3
func (s *Server) SetWorkerParams(w WorkerConfig)
SetWorkerParams merge the WorkerConfig param with the package variable workerConfig. The workerConfig is then used to spawn an http.Server
func (*Server) WaitAndStop ¶
func (s *Server) WaitAndStop()
WaitAndStop wait for all servers to terminate. Use of a sync.waitGroup to properly wait all group.
type TLSConfig ¶
type TLSConfig struct { Cert string `json:"cert"` Key string `json:"key"` Insecure bool `json:"insecure"` }
TLSConfig contain the tls config passed by the config file
type ValidationError ¶ added in v2.5.0
type ValidationError struct {
Error validator.ValidationErrorsTranslations `json:"error"`
}
Context implement the IContext interface It hold the data used by the request
type WorkerConfig ¶ added in v2.3.3
type WorkerConfig struct { // ReadTimeout is a timing constraint on the client http request imposed by the server from the moment // of initial connection up to the time the entire request body has been read. // [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response] ReadTimeout time.Duration ReadHeaderTimeout time.Duration // WriteTimeout is a time limit imposed on client connecting to the server via http from the // time the server has completed reading the request header up to the time it has finished writing the response. // [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response] WriteTimeout time.Duration MaxHeaderBytes int }
WorkerConfig hold the worker config per server instance
type WorkerLauncher ¶
type WorkerLauncher struct {
// contains filtered or unexported fields
}
WorkerLauncher hold the different workers
func CreateWorkerLauncher ¶
func CreateWorkerLauncher(wg *sync.WaitGroup, cancel context.CancelFunc) WorkerLauncher
to use as a factory as the fields are unexported
func (*WorkerLauncher) Start ¶
func (l *WorkerLauncher) Start(name string, fn func() error)
launch a worker who will be wait & kill at the same time than the others
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
package log implement the ILog interface used by the webfmwk
|
package log implement the ILog interface used by the webfmwk |
Package middleware implement some basic middleware for the webfmwk middleware provides a convenient mechanism for filtering HTTP requests entering the application.
|
Package middleware implement some basic middleware for the webfmwk middleware provides a convenient mechanism for filtering HTTP requests entering the application. |
Package testing hold some testing method used to assert the webfmwk API
|
Package testing hold some testing method used to assert the webfmwk API |