Documentation
¶
Overview ¶
Package webgo is a lightweight framework for building web apps. It has a multiplexer, middleware plugging mechanism & context management of its own. The primary goal of webgo is to get out of the developer's way as much as possible. i.e. it does not enforce you to build your app in any particular pattern, instead just helps you get all the trivial things done faster and easier.
e.g. 1. Getting named URI parameters. 2. Multiplexer for regex matching of URI and such. 3. Inject special app level configurations or any such objects to the request context as required.
Index ¶
- Constants
- Variables
- func GetError(r *http.Request) error
- func GlobalLoggerConfig(stdout io.Writer, stderr io.Writer, cfgs ...logCfg)
- func OriginalResponseWriter(rw http.ResponseWriter) http.ResponseWriter
- func R200(w http.ResponseWriter, data interface{})
- func R201(w http.ResponseWriter, data interface{})
- func R204(w http.ResponseWriter)
- func R302(w http.ResponseWriter, data interface{})
- func R400(w http.ResponseWriter, data interface{})
- func R403(w http.ResponseWriter, data interface{})
- func R404(w http.ResponseWriter, data interface{})
- func R406(w http.ResponseWriter, data interface{})
- func R451(w http.ResponseWriter, data interface{})
- func R500(w http.ResponseWriter, data interface{})
- func Render(w http.ResponseWriter, data interface{}, rCode int, tpl *template.Template)
- func ResponseStatus(rw http.ResponseWriter) int
- func Send(w http.ResponseWriter, contentType string, data interface{}, rCode int)
- func SendError(w http.ResponseWriter, data interface{}, rCode int)
- func SendHeader(w http.ResponseWriter, rCode int)
- func SendResponse(w http.ResponseWriter, data interface{}, rCode int)
- func SetError(r *http.Request, err error)
- type Config
- type ContextPayload
- type ErrorData
- type Logger
- type Middleware
- type Route
- type Router
- func (rtr *Router) ServeHTTP(rw http.ResponseWriter, r *http.Request)
- func (router *Router) Shutdown() error
- func (router *Router) ShutdownHTTPS() error
- func (router *Router) Start()
- func (router *Router) StartHTTPS()
- func (rtr *Router) Use(flist ...Middleware)
- func (rtr *Router) UseOnSpecialHandlers(flist ...Middleware)
Constants ¶
const ( // LogCfgDisableDebug is used to disable debug logs LogCfgDisableDebug = logCfg("disable-debug") // LogCfgDisableInfo is used to disable info logs LogCfgDisableInfo = logCfg("disable-info") // LogCfgDisableWarn is used to disable warning logs LogCfgDisableWarn = logCfg("disable-warn") // LogCfgDisableError is used to disable error logs LogCfgDisableError = logCfg("disable-err") // LogCfgDisableFatal is used to disable fatal logs LogCfgDisableFatal = logCfg("disable-fatal") )
const ( // HeaderContentType is the key for mentioning the response header content type HeaderContentType = "Content-Type" // JSONContentType is the MIME type when the response is JSON JSONContentType = "application/json" // HTMLContentType is the MIME type when the response is HTML HTMLContentType = "text/html; charset=UTF-8" // ErrInternalServer to send when there's an internal server error ErrInternalServer = "Internal server error" )
Variables ¶
var ( // ErrInvalidPort is the error returned when the port number provided in the config file is invalid ErrInvalidPort = errors.New("Port number not provided or is invalid (should be between 0 - 65535)") )
Functions ¶
func GlobalLoggerConfig ¶
GlobalLoggerConfig is used to configure the global/default logger of webgo IMPORTANT: This is not concurrent safe
func OriginalResponseWriter ¶ added in v5.3.1
func OriginalResponseWriter(rw http.ResponseWriter) http.ResponseWriter
OriginalResponseWriter returns the Go response writer stored within the webgo custom response writer
func R400 ¶
func R400(w http.ResponseWriter, data interface{})
R400 - Invalid request, any incorrect/erraneous value in the request body
func R406 ¶
func R406(w http.ResponseWriter, data interface{})
R406 - Unacceptable header. For any error related to values set in header
func R451 ¶
func R451(w http.ResponseWriter, data interface{})
R451 - Resource taken down because of a legal request
func Render ¶
func Render(w http.ResponseWriter, data interface{}, rCode int, tpl *template.Template)
Render is used for rendering templates (HTML)
func ResponseStatus ¶
func ResponseStatus(rw http.ResponseWriter) int
ResponseStatus returns the response status code. It works only if the http.ResponseWriter is not wrapped in another response writer before calling ResponseStatus
func Send ¶
func Send(w http.ResponseWriter, contentType string, data interface{}, rCode int)
Send sends a completely custom response without wrapping in the `{data: <data>, status: <int>` struct
func SendError ¶
func SendError(w http.ResponseWriter, data interface{}, rCode int)
SendError is used to respond to any request with an error
func SendHeader ¶
func SendHeader(w http.ResponseWriter, rCode int)
SendHeader is used to send only a response header, i.e no response body
func SendResponse ¶
func SendResponse(w http.ResponseWriter, data interface{}, rCode int)
SendResponse is used to respond to any request (JSON response) based on the code, data etc.
Types ¶
type Config ¶
type Config struct { // Host is the host on which the server is listening Host string `json:"host,omitempty"` // Port is the port number where the server has to listen for the HTTP requests Port string `json:"port,omitempty"` // CertFile is the TLS/SSL certificate file path, required for HTTPS CertFile string `json:"certFile,omitempty"` // KeyFile is the filepath of private key of the certificate KeyFile string `json:"keyFile,omitempty"` // HTTPSPort is the port number where the server has to listen for the HTTP requests HTTPSPort string `json:"httpsPort,omitempty"` // ReadTimeout is the maximum duration for which the server would read a request ReadTimeout time.Duration `json:"readTimeout,omitempty"` // WriteTimeout is the maximum duration for which the server would try to respond WriteTimeout time.Duration `json:"writeTimeout,omitempty"` // InsecureSkipVerify is the HTTP certificate verification InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"` // ShutdownTimeout is the duration in which graceful shutdown is completed ShutdownTimeout time.Duration }
Config is used for reading app's configuration from json file
type ContextPayload ¶
ContextPayload is the WebgoContext. A new instance of ContextPayload is injected inside every request's context object
func Context ¶
func Context(r *http.Request) *ContextPayload
Context returns the ContextPayload injected inside the HTTP request context
func (*ContextPayload) Error ¶ added in v5.2.0
func (cp *ContextPayload) Error() error
Error returns the error set within the context
func (*ContextPayload) Params ¶
func (cp *ContextPayload) Params() map[string]string
Params returns the URI parameters of the respective route
func (*ContextPayload) SetError ¶ added in v5.2.0
func (cp *ContextPayload) SetError(err error)
SetError sets the err within the context
type Logger ¶
type Logger interface { Debug(data ...interface{}) Info(data ...interface{}) Warn(data ...interface{}) Error(data ...interface{}) Fatal(data ...interface{}) }
Logger defines all the logging methods to be implemented
var LOGHANDLER Logger
LOGHANDLER is a global variable which webgo uses to log messages
type Middleware ¶
type Middleware func(http.ResponseWriter, *http.Request, http.HandlerFunc)
Middleware is the signature of WebGo's middleware
type Route ¶
type Route struct { // Name is unique identifier for the route Name string // Method is the HTTP request method/type Method string // Pattern is the URI pattern to match Pattern string // TrailingSlash if set to true, the URI will be matched with or without // a trailing slash. IMPORTANT: It does not redirect. TrailingSlash bool // FallThroughPostResponse if enabled will execute all the handlers even if a response was already sent to the client FallThroughPostResponse bool // Handlers is a slice of http.HandlerFunc which can be middlewares or anything else. Though only 1 of them will be allowed to respond to client. // subsequent writes from the following handlers will be ignored Handlers []http.HandlerFunc // contains filtered or unexported fields }
Route defines a route for each API
type Router ¶
type Router struct { // NotFound is the generic handler for 404 resource not found response NotFound http.HandlerFunc // NotImplemented is the generic handler for 501 method not implemented NotImplemented http.HandlerFunc // contains filtered or unexported fields }
Router is the HTTP router
func NewRouter ¶
NewRouter initializes & returns a new router instance with all the configurations and routes set
func (*Router) ShutdownHTTPS ¶
ShutdownHTTPS gracefully shuts down HTTPS server
func (*Router) Start ¶
func (router *Router) Start()
Start starts the HTTP server with the appropriate configurations
func (*Router) StartHTTPS ¶
func (router *Router) StartHTTPS()
StartHTTPS starts the server with HTTPS enabled
func (*Router) UseOnSpecialHandlers ¶
func (rtr *Router) UseOnSpecialHandlers(flist ...Middleware)
UseOnSpecialHandlers adds middleware to the 2 special handlers of webgo
Directories
¶
Path | Synopsis |
---|---|
middleware
|
|
accesslog
Package accesslogs provides a simple straight forward access log middleware.
|
Package accesslogs provides a simple straight forward access log middleware. |
cors
Package cors sets the appropriate CORS(https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) response headers, and lets you customize.
|
Package cors sets the appropriate CORS(https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) response headers, and lets you customize. |