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 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 Render404(w http.ResponseWriter, tpl *template.Template)
- 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 Start(cfg *Config, router *Router, readTimeout, writeTimeout time.Duration)
- type Config
- type ErrorData
- type Errors
- type Globals
- type Middlewares
- type Route
- type Router
- type Templates
- type WC
Constants ¶
const ( // C001 Error Code 1 C001 = "Invalid number of arguments provided" // C002 Error Code 2 C002 = "Could not unmarshal JSON config file" // C003 Error Code 3 C003 = "App environment not provided in config file, accepted values are `production` or `development`" // C004 Error Code 4 C004 = "App port not provided in config file" // C005 Error Code 5 C005 = "Invalid JSON" )
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 Log *log.Logger
Log is used to log errors, which will print the filename and linenumber
Functions ¶
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 Render404 ¶
func Render404(w http.ResponseWriter, tpl *template.Template)
Render404 - used to render a 404 page
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 { // Env is the deployment environment Env string `json:"environment"` // 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"` // 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"` // HTTPSOnly if true will enable HTTPS server alone HTTPSOnly bool `json:"httpsOnly,omitempty"` // TemplatesBasePath is the base path where all the HTML templates are located TemplatesBasePath string `json:"templatePath,omitempty"` // Data holds the full json config file data as bytes Data []byte `json:"-"` }
Config is used for reading app's configuration from json file
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors is the custom error for webgo error handling
type Globals ¶
type Globals struct { // All the app configurations Cfg *Config // All templates, which can be accessed anywhere from the app Templates map[string]*htpl.Template // This can be used to add any app specifc data, which needs to be shared // E.g. This can be used to plug in a new DB driver, if someone does not want to use MongoDb App map[string]interface{} }
Globals struct to hold configurations which are shared with all the request handlers via context.
type Middlewares ¶
type Middlewares struct{}
Middlewares has all the default middlewares provided by webgo
func (*Middlewares) Cors ¶
func (m *Middlewares) Cors(rw http.ResponseWriter, req *http.Request)
Cors is a basic Cors middleware definition.
func (*Middlewares) CorsOptions ¶
func (m *Middlewares) CorsOptions(rw http.ResponseWriter, req *http.Request)
CorsOptions is a cors middleware just for Options request - adding this helped remove the request method check (an `if` block to check the request type) from Cors 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. Note: It does not *do* a redirect. TrailingSlash bool // HideAccessLog if enabled will not print the basic access log to console HideAccessLog bool // FallThroughPostResponse if enabled will execute all the handlers even if a response was already sent to the client FallThroughPostResponse bool // Handler 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 Handler []http.HandlerFunc G *Globals // App globals // contains filtered or unexported fields }
Route defines a route for each API
type Router ¶
type Router struct { HideAccessLog bool NotFound http.HandlerFunc // contains filtered or unexported fields }
Router is the HTTP router