AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kong/context/context.go

92 lines
1.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package context
import "net/http"
type kv struct {
key string
value interface{}
}
type keysValues []kv
func (kv keysValues) get(key string) (interface{}, bool) {
for _, p := range kv {
if p.key == key {
return p.value, true
}
}
return nil, false
}
type Context struct {
Request *http.Request
Response http.ResponseWriter
rrType RendererType
params keysValues
data keysValues
requestID string
Status int
}
func (c *Context) reset() *Context {
c.Request = nil
c.Response = nil
c.params = c.params[:0]
c.data = c.data[:0]
c.requestID = "?"
c.Status = 0
return c
}
func (c *Context) SetHeader(key, value string) {
c.Response.Header().Set(key, value)
}
func (c *Context) SetHeaderContentType(ct string) {
c.SetHeader("Content-Type", ct)
}
func (c *Context) SetHeaderWWWAuthenticate() {
c.SetHeader("WWW-Authenticate", `Basic realm="Authorization Required"`)
}
func (c *Context) AddParam(name, value string) {
c.params = append(c.params, kv{key: name, value: value})
}
func (c *Context) Param(name string) string {
value, ok := c.params.get(name)
if !ok {
return ""
}
return value.(string)
}
func (c *Context) AddData(key string, value interface{}) {
c.data = append(c.data, kv{key: key, value: value})
}
func (c *Context) Data(key string) (interface{}, bool) {
return c.data.get(key)
}
func (c *Context) SetRequestID(rID string) {
c.requestID = rID
}
func (c *Context) RequestID() string {
return c.requestID
}
/*
######################################################################################################## @(°_°)@ #######
*/