En cours de développement

master
losyme 11 months ago
parent 5c80d9d3f1
commit f0fed86a38
  1. 2
      Taskfile.yml
  2. 114
      context/context.go
  3. 43
      context/pool.go
  4. 26
      context/render.go

@ -6,6 +6,8 @@ version: '3'
tasks:
update:
cmds:
- go get -u forge.chapril.org/losyme/errors
- go get -u forge.chapril.org/losyme/util
- go mod tidy
lint:
cmds:

@ -0,0 +1,114 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package context
import "net/http"
const (
_error = "__ERROR"
_recoverData = "__RECOVER_DATA"
_requestID = "__REQUEST_ID"
)
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
Status int
params keysValues
data keysValues
}
func (c *Context) reset() *Context {
c.Request = nil
c.Response = nil
c.Status = 0
c.params = c.params[:0]
c.data = c.data[:0]
return c
}
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) SetError(err error) {
c.AddData(_error, err)
}
func (c *Context) Error() error {
err, ok := c.Data(_error)
if ok {
return err.(error)
}
return nil
}
func (c *Context) SetRecoverData(data interface{}) {
c.AddData(_recoverData, data)
}
func (c *Context) RecoverData() interface{} {
data, ok := c.Data(_recoverData)
if ok {
return data
}
return nil
}
func (c *Context) SetRequestID(rid string) {
c.AddData(_requestID, rid)
}
func (c *Context) RequestID() string {
rid, ok := c.Data(_requestID)
if ok {
return rid.(string)
}
return "?"
}
/*
######################################################################################################## @(°_°)@ #######
*/

@ -0,0 +1,43 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package context
import (
"net/http"
"sync"
)
type Pool struct {
sp *sync.Pool
}
func NewPool() *Pool {
return &Pool{
sp: &sync.Pool{
New: func() interface{} {
return new(Context)
},
},
}
}
func (p *Pool) Get(rw http.ResponseWriter, req *http.Request) *Context {
c := p.sp.Get().(*Context)
c.Request = req
c.Response = rw
return c
}
func (p *Pool) Put(c *Context) {
p.sp.Put(c.reset())
}
/*
######################################################################################################## @(°_°)@ #######
*/

@ -0,0 +1,26 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package context
import "forge.chapril.org/losyme/kong/render"
func (c *Context) Render(status int, rr render.Render) error {
locked, err := rr.Render(c.Response, status)
if locked {
c.Status = status
}
return err
}
func (c *Context) JSON(status int, data interface{}) error {
return c.Render(status, render.JSON{Data: data})
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save