Documentation
¶
Index ¶
- Constants
- Variables
- type App
- func (r App) All(path string, handlers ...Handler) *route
- func (a *App) Close() error
- func (r App) Delete(path string, handlers ...Handler) *route
- func (r App) Get(path string, handlers ...Handler) *route
- func (a *App) GetSetting(key string) interface{}
- func (r App) Head(path string, handlers ...Handler) *route
- func (a *App) IsSettingDisabled(key string) bool
- func (a *App) IsSettingEnabled(key string) bool
- func (a *App) IsTrustProxyEnabled() bool
- func (a *App) Listen(addr string) error
- func (a *App) LoadTemplates(fs fs.FS, config *TemplateConfig) (err error)
- func (r App) NewRouter(path string) *route
- func (r App) Options(path string, handlers ...Handler) *route
- func (r App) Param(param string, handler ParamHandler) *route
- func (r App) Patch(path string, handlers ...Handler) *route
- func (r App) Path() string
- func (r App) Post(path string, handlers ...Handler) *route
- func (r App) Put(path string, handlers ...Handler) *route
- func (a *App) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (a *App) SetSetting(key string, value interface{})
- func (a *App) Settings() map[string]interface{}
- func (r App) Static(root fs.FS, path string) *route
- func (r App) Use(middleware ...Handler) *route
- type Body
- type DownloadOption
- type Error
- type Handler
- type JSONError
- type NextFunc
- type ParamHandler
- type Range
- type Request
- func (req *Request) Context() coreContext.Context
- func (req *Request) Cookie(name string) (value string, exists bool)
- func (req *Request) Get(key string) string
- func (req *Request) GetParam(name string) string
- func (req *Request) Is(mime string) bool
- func (req *Request) QueryParam(name string) string
- func (req *Request) Range(size int64) ([]Range, error)
- func (req *Request) Set(key string, value string)
- func (req *Request) SetContext(ctx coreContext.Context)
- type Response
- func (r *Response) Append(key, value string) *Response
- func (r *Response) Attachment(filename string) *Response
- func (r *Response) ClearCookie(name string) *Response
- func (r *Response) Cookie(cookie *http.Cookie) *Response
- func (r *Response) Download(filepath, filename string, options *DownloadOption, cb func(error))
- func (r *Response) Get(key string) string
- func (r *Response) JSON(v interface{}) *Response
- func (r *Response) Location(path string) *Response
- func (r *Response) Redirect(path string, status ...int) *Response
- func (r *Response) Render(name string, data interface{}) *Response
- func (r *Response) Send(body interface{}) *Response
- func (r *Response) SendFile(filePath, fileName string, options *DownloadOption, cb func(error))
- func (r *Response) SendStatus(statusCode int) *Response
- func (r *Response) Set(key string, value string) *Response
- func (r *Response) SignedCookie(cookie *http.Cookie, secret string) *Response
- func (r *Response) Status(code int) *Response
- func (r *Response) StatusCode() int
- func (r *Response) Type(filename string) *Response
- func (r *Response) Vary(field string) *Response
- type TemplateConfig
Constants ¶
const ( GET allowedMethod = 1 << iota POST PUT DELETE PATCH OPTIONS HEAD )
Variables ¶
var ( ErrDirPath = errors.New("specified path is a directory") ErrDotfilesDeny = errors.New("serving dotfiles is not allowed") )
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the main type for the coco framework.
func NewApp ¶
func NewApp() (app *App)
NewApp creates a new App instance with a default Route at the root path "/" and a default settings instance with default values.
func (*App) GetSetting ¶
GetSetting retrieves a custom setting by its key.
func (*App) IsSettingDisabled ¶
IsSettingDisabled checks if a setting is false.
func (*App) IsSettingEnabled ¶
IsSettingEnabled checks if a setting is true.
func (*App) IsTrustProxyEnabled ¶
func (*App) Listen ¶
Listen starts an HTTP server and listens on the given address. addr should be in format :PORT ie :8000
func (*App) LoadTemplates ¶
func (a *App) LoadTemplates(fs fs.FS, config *TemplateConfig) (err error)
LoadTemplates loads templates from an fs.FS with a given config
func (App) Param ¶
func (r App) Param(param string, handler ParamHandler) *route
func (*App) SetSetting ¶
SetSetting sets a custom setting with a key and value.
type Body ¶
type Body struct {
// contains filtered or unexported fields
}
func (*Body) FormData ¶
FormData returns the body form data, expects request sent with `x-www-form-urlencoded` header or
type DownloadOption ¶
type JSONError ¶
JSONError is an error type that is returned when the request body is not a valid JSON.
type NextFunc ¶
NextFunc is a function that is called to pass execution to the next handler in the chain.
type ParamHandler ¶
ParamHandler is a function that is called when a parameter is found in the route path
type Request ¶
type Request struct { BaseURL string // HostName contains the hostname derived from the Host HTTP header. HostName string // Ip contains the remote IP address of the request. Ip string // Ips contains the remote IP addresses from the X-Forwarded-For header. Ips []string // Protocol contains the request protocol string: "http" or "https" Protocol string // Secure is a boolean that is true if the request protocol is "https" Secure bool // Subdomains is a slice of subdomain strings. Subdomains []string // Xhr is a boolean that is true if the request's X-Requested-With header // field is "XMLHttpRequest". Xhr bool // OriginalURL is the original URL requested by the client. OriginalURL *url.URL // Cookies contains the cookies sent by the request. Cookies map[string]string // Body contains the body of the request. Body // Query contains the parsed query string from the URL. Query map[string]string // Params contains the Route parameters. Params map[string]string // SignedCookies contains the signed cookies sent by the request. SignedCookies map[string]string // Stale is a boolean that is true if the request is stale, false otherwise. Stale bool // Fresh is a boolean that is true if the request is fresh, false otherwise. Fresh bool // Method contains a string corresponding to the HTTP method of the request: // GET, POST, PUT, and so on. Method string // Path contains a string corresponding to the path of the request. Path string // contains filtered or unexported fields }
func (*Request) Context ¶
func (req *Request) Context() coreContext.Context
func (*Request) Get ¶
Get returns the value of specified HTTP request header field (case-insensitive match)
func (*Request) GetParam ¶
GetParam returns the value of param `name` when present or `defaultValue`.
func (*Request) Is ¶
Is returns true if the incoming request’s “Content-Type” HTTP header field matches the given mime type.
func (*Request) QueryParam ¶
QueryParam returns the value of query parameter `name` when present or `defaultValue`.
func (*Request) Range ¶
Range returns the first range found in the request’s “Range” header field. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
func (*Request) Set ¶
Set sets the value of specified HTTP request header field (case-insensitive match)
func (*Request) SetContext ¶
func (req *Request) SetContext(ctx coreContext.Context)
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func (*Response) Append ¶
Append sets the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value.
func (*Response) Attachment ¶
Attachment sets the Content-Disposition header to “attachment”. If a filename is given, then the Content-Type header is set based on the filename’s extension.
func (*Response) ClearCookie ¶
ClearCookie clears the cookie by setting the MaxAge to -1
func (*Response) Download ¶
func (r *Response) Download(filepath, filename string, options *DownloadOption, cb func(error))
Download transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension.
func (*Response) Location ¶
Location sets the response Location HTTP header to the specified path parameter.
func (*Response) Redirect ¶
Redirect redirects to the URL derived from the specified path, with specified status.
func (*Response) SendFile ¶
func (r *Response) SendFile(filePath, fileName string, options *DownloadOption, cb func(error))
SendFile transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension.
func (*Response) SendStatus ¶
SendStatus sends the HTTP response status code.
func (*Response) SignedCookie ¶
SignedCookie SecureCookie sets a signed cookie
func (*Response) StatusCode ¶
type TemplateConfig ¶
type TemplateConfig struct { // The file extension of the templates. // Defaults to ".html". Ext string // The directory where the includes are stored. // Defaults to "includes". IncludesDir string // The name used for layout templates :- templates that wrap other contents. // Defaults to "layouts". Layout string }
TemplateConfig is a configuration for loading templates from an fs.FS