1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 07:52:53 +00:00

Add more comments

This commit is contained in:
Fenny 2020-03-16 15:43:16 +01:00
parent 305c60e5e2
commit 66632ccda9

View File

@ -111,7 +111,9 @@ func releaseConn(conn *Conn) {
poolConn.Put(conn)
}
// Accepts : https://fiber.wiki/context#accepts
// Checks, if the specified extensions or content types are acceptable.
//
// https://fiber.wiki/context#accepts
func (ctx *Ctx) Accepts(offers ...string) (offer string) {
if len(offers) == 0 {
return ""
@ -149,7 +151,9 @@ func (ctx *Ctx) Accepts(offers ...string) (offer string) {
return ""
}
// AcceptsCharsets : https://fiber.wiki/context#acceptscharsets
// Checks, if the specified charset is acceptable.
//
// https://fiber.wiki/context#accepts
func (ctx *Ctx) AcceptsCharsets(offers ...string) (offer string) {
if len(offers) == 0 {
return ""
@ -175,7 +179,9 @@ func (ctx *Ctx) AcceptsCharsets(offers ...string) (offer string) {
return ""
}
// AcceptsEncodings : https://fiber.wiki/context#acceptsencodings
// Checks, if the specified encoding is acceptable.
//
// https://fiber.wiki/context#accepts
func (ctx *Ctx) AcceptsEncodings(offers ...string) (offer string) {
if len(offers) == 0 {
return ""
@ -201,7 +207,9 @@ func (ctx *Ctx) AcceptsEncodings(offers ...string) (offer string) {
return ""
}
// AcceptsLanguages : https://fiber.wiki/context#acceptslanguages
// Checks, if the specified language is acceptable.
//
// https://fiber.wiki/context#accepts
func (ctx *Ctx) AcceptsLanguages(offers ...string) (offer string) {
if len(offers) == 0 {
return ""
@ -226,7 +234,10 @@ func (ctx *Ctx) AcceptsLanguages(offers ...string) (offer string) {
return ""
}
// Append : https://fiber.wiki/context#append
// Appends the specified value to the HTTP response header field.
// If the header is not already set, it creates the header with the specified value.
//
// https://fiber.wiki/context#append
func (ctx *Ctx) Append(field string, values ...string) {
if len(values) == 0 {
return
@ -242,7 +253,9 @@ func (ctx *Ctx) Append(field string, values ...string) {
ctx.Set(field, h)
}
// Attachment : https://fiber.wiki/context#attachment
// Sets the HTTP response Content-Disposition header field to attachment.
//
// https://fiber.wiki/context#attachment
func (ctx *Ctx) Attachment(name ...string) {
if len(name) > 0 {
filename := filepath.Base(name[0])
@ -253,12 +266,17 @@ func (ctx *Ctx) Attachment(name ...string) {
ctx.Set(HeaderContentDisposition, "attachment")
}
// BaseURL : https://fiber.wiki/context#baseurl
// Returns base URL (protocol + host) as a string.
//
// https://fiber.wiki/context#baseurl
func (ctx *Ctx) BaseURL() string {
return ctx.Protocol() + "://" + ctx.Hostname()
}
// Body : https://fiber.wiki/context#body
// Contains the raw body submitted in a POST request.
// If a key is provided, it returns the form value
//
// https://fiber.wiki/context#body
func (ctx *Ctx) Body(key ...string) string {
// Return request body
if len(key) == 0 {
@ -271,7 +289,11 @@ func (ctx *Ctx) Body(key ...string) string {
return ""
}
// BodyParser : https://fiber.wiki/context#bodyparser
// Binds the request body to a struct.
// BodyParser supports decoding the following content types based on the Content-Type header:
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
//
// https://fiber.wiki/context#bodyparser
func (ctx *Ctx) BodyParser(out interface{}) error {
// TODO : Query Params
ctype := getString(ctx.Fasthttp.Request.Header.ContentType())
@ -303,7 +325,9 @@ func (ctx *Ctx) BodyParser(out interface{}) error {
return fmt.Errorf("BodyParser: cannot parse content-type: %v", ctype)
}
// ClearCookie : https://fiber.wiki/context#clearcookie
// Expire a client cookie (or all cookies if left empty)
//
// https://fiber.wiki/context#clearcookie
func (ctx *Ctx) ClearCookie(key ...string) {
if len(key) > 0 {
for i := range key {
@ -318,7 +342,8 @@ func (ctx *Ctx) ClearCookie(key ...string) {
})
}
// Compress : https://fiber.wiki/context#compress
// This function is deprecated since v1.8.2!
// Please us github.com/gofiber/compression
func (ctx *Ctx) Compress(enable ...bool) {
log.Println("Warning: c.Compress() is deprecated since v1.8.2, please use github.com/gofiber/compression instead.")
ctx.compress = true
@ -327,7 +352,9 @@ func (ctx *Ctx) Compress(enable ...bool) {
}
}
// Cookie : https://fiber.wiki/context#cookie
// Set cookie by passing a cookie struct
//
// https://fiber.wiki/context#cookie
func (ctx *Ctx) Cookie(cookie *Cookie) {
fcookie := &fasthttp.Cookie{}
fcookie.SetKey(cookie.Name)
@ -340,7 +367,9 @@ func (ctx *Ctx) Cookie(cookie *Cookie) {
ctx.Fasthttp.Response.Header.SetCookie(fcookie)
}
// Cookies : https://fiber.wiki/context#cookies
// Get cookie value by key
//
// https://fiber.wiki/context#cookies
func (ctx *Ctx) Cookies(key ...string) (value string) {
if len(key) == 0 {
return ctx.Get(HeaderCookie)
@ -348,6 +377,11 @@ func (ctx *Ctx) Cookies(key ...string) (value string) {
return getString(ctx.Fasthttp.Request.Header.Cookie(key[0]))
}
// Transfers the file from path as an attachment.
// Typically, browsers will prompt the user for download.
// By default, the Content-Disposition header filename= parameter is the filepath (this typically appears in the browser dialog).
// Override this default with the filename parameter.
//
// Download : https://fiber.wiki/context#download
func (ctx *Ctx) Download(file string, name ...string) {
filename := filepath.Base(file)
@ -360,12 +394,17 @@ func (ctx *Ctx) Download(file string, name ...string) {
ctx.SendFile(file)
}
// Error returns err that is passed via Next(err)
// This contains the error information that thrown by a panic or passed via the Next(err) method.
//
// https://fiber.wiki/context#error
func (ctx *Ctx) Error() error {
return ctx.err
}
// Format : https://fiber.wiki/context#format
// Performs content-negotiation on the Accept HTTP header. It uses Accepts to select a proper format.
// If the header is not specified or there is no proper format, text/plain is used.
//
// https://fiber.wiki/context#format
func (ctx *Ctx) Format(body interface{}) {
var b string
accept := ctx.Accepts("html", "json")
@ -390,22 +429,31 @@ func (ctx *Ctx) Format(body interface{}) {
}
}
// FormFile : https://fiber.wiki/context#formfile
// MultipartForm files can be retrieved by name, the first file from the given key is returned.
//
// https://fiber.wiki/context#formfile
func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
return ctx.Fasthttp.FormFile(key)
}
// FormValue : https://fiber.wiki/context#formvalue
// MultipartForm values can be retrieved by name, the first value from the given key is returned.
//
// https://fiber.wiki/context#formvalue
func (ctx *Ctx) FormValue(key string) (value string) {
return getString(ctx.Fasthttp.FormValue(key))
}
// Fresh : https://fiber.wiki/context#fresh
// Not implemented yet, pull requests are welcome!
//
// https://fiber.wiki/context#fresh
func (ctx *Ctx) Fresh() bool {
return false
}
// Get : https://fiber.wiki/context#get
// Returns the HTTP request header specified by field.
// Field names are case-insensitive
//
// https://fiber.wiki/context#get
func (ctx *Ctx) Get(key string) (value string) {
if key == "referrer" {
key = "referer"
@ -413,17 +461,23 @@ func (ctx *Ctx) Get(key string) (value string) {
return getString(ctx.Fasthttp.Request.Header.Peek(key))
}
// Hostname : https://fiber.wiki/context#hostname
// Contains the hostname derived from the Host HTTP header.
//
// https://fiber.wiki/context#hostname
func (ctx *Ctx) Hostname() string {
return getString(ctx.Fasthttp.URI().Host())
}
// IP : https://fiber.wiki/context#Ip
// Returns the remote IP address of the request.
//
// https://fiber.wiki/context#Ip
func (ctx *Ctx) IP() string {
return ctx.Fasthttp.RemoteIP().String()
}
// IPs : https://fiber.wiki/context#ips
// Returns an string slice of IP addresses specified in the X-Forwarded-For request header.
//
// https://fiber.wiki/context#ips
func (ctx *Ctx) IPs() []string {
ips := strings.Split(ctx.Get(HeaderXForwardedFor), ",")
for i := range ips {
@ -432,7 +486,10 @@ func (ctx *Ctx) IPs() []string {
return ips
}
// Is : https://fiber.wiki/context#is
// Returns the matching content type,
// if the incoming requests Content-Type HTTP header field matches the MIME type specified by the type parameter.
//
// https://fiber.wiki/context#is
func (ctx *Ctx) Is(extension string) (match bool) {
if extension[0] != '.' {
extension = "." + extension
@ -449,7 +506,10 @@ func (ctx *Ctx) Is(extension string) (match bool) {
return
}
// JSON : https://fiber.wiki/context#json
// Converts any interface or string to JSON using Jsoniter.
// This method also sets the content header to application/json.
//
// https://fiber.wiki/context#json
func (ctx *Ctx) JSON(json interface{}) error {
ctx.Fasthttp.Response.Header.SetContentType(MIMEApplicationJSON)
raw, err := jsoniter.Marshal(&json)
@ -462,7 +522,11 @@ func (ctx *Ctx) JSON(json interface{}) error {
return nil
}
// JSONP : https://fiber.wiki/context#jsonp
// Sends a JSON response with JSONP support.
// This method is identical to JSON, except that it opts-in to JSONP callback support.
// By default, the callback name is simply callback.
//
// https://fiber.wiki/context#jsonp
func (ctx *Ctx) JSONP(json interface{}, callback ...string) error {
raw, err := jsoniter.Marshal(&json)
if err != nil {
@ -482,7 +546,9 @@ func (ctx *Ctx) JSONP(json interface{}, callback ...string) error {
return nil
}
// Links : https://fiber.wiki/context#links
// Joins the links followed by the property to populate the responses Link HTTP header field.
//
// https://fiber.wiki/context#links
func (ctx *Ctx) Links(link ...string) {
h := ""
for i, l := range link {
@ -499,7 +565,10 @@ func (ctx *Ctx) Links(link ...string) {
}
}
// Locals : https://fiber.wiki/context#locals
// You can pass interface{} values under string keys scoped to the request
// and therefore available to all routes that match the request.
//
// https://fiber.wiki/context#locals
func (ctx *Ctx) Locals(key string, value ...interface{}) (val interface{}) {
if len(value) == 0 {
return ctx.Fasthttp.UserValue(key)
@ -508,12 +577,16 @@ func (ctx *Ctx) Locals(key string, value ...interface{}) (val interface{}) {
return value[0]
}
// Location : https://fiber.wiki/context#location
// Sets the response Location HTTP header to the specified path parameter.
//
// https://fiber.wiki/context#location
func (ctx *Ctx) Location(path string) {
ctx.Set(HeaderLocation, path)
}
// Method : https://fiber.wiki/context#method
// Contains a string corresponding to the HTTP method of the request: GET, POST, PUT and so on.
//
// https://fiber.wiki/context#method
func (ctx *Ctx) Method(override ...string) string {
if len(override) > 0 {
ctx.method = override[0]
@ -521,12 +594,18 @@ func (ctx *Ctx) Method(override ...string) string {
return ctx.method
}
// MultipartForm : https://fiber.wiki/context#multipartform
// Access multipart form entries, you can parse the binary with MultipartForm().
// This returns a map[string][]string, so given a key the value will be a string slice.
//
// https://fiber.wiki/context#multipartform
func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
return ctx.Fasthttp.MultipartForm()
}
// Next : https://fiber.wiki/context#next
// Next executes the next method in the stack that matches the current route.
// You can pass an optional error for custom error handling.
//
// https://fiber.wiki/context#next
func (ctx *Ctx) Next(err ...error) {
ctx.route = nil
ctx.values = nil
@ -536,12 +615,17 @@ func (ctx *Ctx) Next(err ...error) {
ctx.app.nextRoute(ctx)
}
// OriginalURL : https://fiber.wiki/context#originalurl
// Contains the original request URL.
//
// https://fiber.wiki/context#originalurl
func (ctx *Ctx) OriginalURL() string {
return getString(ctx.Fasthttp.Request.Header.RequestURI())
}
// Params : https://fiber.wiki/context#params
// Used to get the route parameters.
// Defaults to empty string "", if the param doesn't exist.
//
// https://fiber.wiki/context#params
func (ctx *Ctx) Params(key string) (value string) {
if ctx.route.Params == nil {
return
@ -844,7 +928,9 @@ func (ctx *Ctx) Vary(fields ...string) {
ctx.Set(HeaderVary, h)
}
// Write : https://fiber.wiki/context#write
// Appends any input to the HTTP body response.
//
// https://fiber.wiki/context#write
func (ctx *Ctx) Write(bodies ...interface{}) {
for i := range bodies {
switch body := bodies[i].(type) {
@ -858,7 +944,10 @@ func (ctx *Ctx) Write(bodies ...interface{}) {
}
}
// XHR : https://fiber.wiki/context#xhr
// A Boolean property, that is true, if the requests X-Requested-With header field is XMLHttpRequest,
// indicating that the request was issued by a client library (such as jQuery).
//
// https://fiber.wiki/context#xhr
func (ctx *Ctx) XHR() bool {
return ctx.Get(HeaderXRequestedWith) == "XMLHttpRequest"
}