Documentation
¶
Index ¶
- Constants
- Variables
- type App
- func (a *App) Close() error
- func (a *App) Disable(key string)
- func (a *App) Disabled(key string) bool
- func (a *App) Enable(key string)
- func (a *App) Enabled(key string) bool
- func (a *App) GetX(key string) interface{}
- func (a *App) IsTrustProxyEnabled() bool
- func (a *App) Listen(addr string) error
- func (a *App) LoadTemplates(fs fs.FS, config *TemplateConfig) (err error)
- func (a *App) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (a *App) SetX(key string, value interface{})
- func (a *App) Settings() map[string]interface{}
- type Body
- type DownloadOption
- type Error
- type Handler
- type JSONError
- type NextFunc
- type ParamHandler
- type Path
- 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 Route
- func (r *Route) All(path string, handlers ...Handler) *Route
- func (r *Route) Delete(path string, handlers ...Handler) *Route
- func (r *Route) Get(path string, handlers ...Handler) *Route
- func (r *Route) Head(path string, handlers ...Handler)
- func (r *Route) Options(path string, handlers ...Handler)
- func (r *Route) Param(param string, handler ParamHandler)
- func (r *Route) Patch(path string, handlers ...Handler)
- func (r *Route) Path() string
- func (r *Route) Post(path string, handlers ...Handler)
- func (r *Route) Put(path string, handlers ...Handler)
- func (r *Route) Router(path string) *Route
- func (r *Route) Static(root fs.FS, path string)
- func (r *Route) Use(middleware ...Handler) *Route
- 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 { *Route // default Route // 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. Equivalent to:
const app = express()
func (*App) IsTrustProxyEnabled ¶
func (*App) Listen ¶
Listen starts an HTTP server and listens on the given address. Equivalent to:
app.listen(3000, () => {})
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
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 Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path is a type that represents a path in the application.
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 ¶ added in v1.0.3
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 ¶ added in v1.0.3
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 ¶ added in v1.0.3
Set sets the value of specified HTTP request header field (case-insensitive match)
func (*Request) SetContext ¶ added in v1.0.1
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 ¶ added in v1.0.1
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route is a type that represents a route in the application. It is equivalent to an express.Router instance.
func (*Route) Param ¶
func (r *Route) Param(param string, handler ParamHandler)
Param calls the given handler when the route param matches the given param.
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