remove dependencies
This commit is contained in:
parent
b808ce30cb
commit
1656b91e3b
107 changed files with 567 additions and 7772 deletions
|
|
@ -8,7 +8,6 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/db"
|
||||
)
|
||||
|
|
@ -22,23 +21,23 @@ func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log l
|
|||
h := w.Header()
|
||||
h.Set("Content-Type", "application/json")
|
||||
return &Context{
|
||||
Code: http.StatusOK,
|
||||
Request: r,
|
||||
Response: w,
|
||||
Data: data,
|
||||
Log: log,
|
||||
ip: clientip(r.RemoteAddr),
|
||||
code: http.StatusOK,
|
||||
r: r,
|
||||
w: w,
|
||||
data: data,
|
||||
Log: log,
|
||||
ip: clientip(r.RemoteAddr),
|
||||
}
|
||||
}
|
||||
|
||||
// Context is the context object shared between http handlers
|
||||
type Context struct {
|
||||
Code int
|
||||
code int
|
||||
|
||||
Request *http.Request
|
||||
Response http.ResponseWriter
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
|
||||
Data *ContextData
|
||||
data *ContextData
|
||||
Log logr.Logger
|
||||
|
||||
ip string
|
||||
|
|
@ -46,28 +45,27 @@ type Context struct {
|
|||
|
||||
// Method returns the request method
|
||||
func (c *Context) Method() string {
|
||||
return c.Request.Method
|
||||
return c.r.Method
|
||||
}
|
||||
|
||||
// Path returns the request URL path
|
||||
func (c *Context) Path() string {
|
||||
return c.Request.URL.Path
|
||||
return c.r.URL.Path
|
||||
}
|
||||
|
||||
// Header returns the given http header
|
||||
func (c *Context) Header(name string) string {
|
||||
return c.Request.Header.Get(name)
|
||||
return c.r.Header.Get(name)
|
||||
}
|
||||
|
||||
// SetHeader sets the given http header
|
||||
func (c *Context) SetHeader(name, value string) {
|
||||
h := c.Response.Header()
|
||||
h.Set(name, value)
|
||||
c.w.Header().Set(name, value)
|
||||
}
|
||||
|
||||
// Form returns the given form value
|
||||
func (c *Context) Form(name string) string {
|
||||
return c.Request.FormValue(name)
|
||||
return c.r.FormValue(name)
|
||||
}
|
||||
|
||||
func (c *Context) ClientIP() string {
|
||||
|
|
@ -76,15 +74,16 @@ func (c *Context) ClientIP() string {
|
|||
|
||||
// Var returns the given url variable
|
||||
func (c *Context) Var(name string) string {
|
||||
return mux.Vars(c.Request)[name]
|
||||
return c.r.PathValue(name)
|
||||
}
|
||||
|
||||
func (c *Context) log() {
|
||||
r := c.r
|
||||
c.Log.Info("access",
|
||||
"addr", c.Request.RemoteAddr,
|
||||
"method", c.Request.Method,
|
||||
"path", c.Request.URL.Path,
|
||||
"code", c.Code,
|
||||
"addr", r.RemoteAddr,
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"code", c.code,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -92,25 +91,33 @@ func (c *Context) NotFound() {
|
|||
c.Err(ErrNotFound)
|
||||
}
|
||||
|
||||
func (c *Context) DB() *db.DB {
|
||||
return c.data.DB
|
||||
}
|
||||
|
||||
func (c *Context) Approver() *Approver {
|
||||
return c.data.Approver
|
||||
}
|
||||
|
||||
// OK is a empty HTTP 200 JSON response
|
||||
func (c *Context) OK() {
|
||||
c.log()
|
||||
}
|
||||
|
||||
func (c *Context) ReadBody() (b []byte, err error) {
|
||||
b, err = io.ReadAll(c.Request.Body)
|
||||
c.Request.Body.Close()
|
||||
b, err = io.ReadAll(c.r.Body)
|
||||
c.r.Body.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Context) Body(b []byte) {
|
||||
c.Response.Write(b)
|
||||
c.w.Write(b)
|
||||
c.log()
|
||||
}
|
||||
|
||||
// JSON is a json response
|
||||
func (c *Context) JSON(v any) {
|
||||
err := json.NewEncoder(c.Response).Encode(v)
|
||||
err := json.NewEncoder(c.w).Encode(v)
|
||||
if err != nil {
|
||||
c.Err(ErrISE)
|
||||
return
|
||||
|
|
@ -119,8 +126,8 @@ func (c *Context) JSON(v any) {
|
|||
}
|
||||
|
||||
func (c *Context) Redirect(url string, code int) {
|
||||
c.Code = code
|
||||
http.Redirect(c.Response, c.Request, url, code)
|
||||
c.code = code
|
||||
http.Redirect(c.w, c.r, url, code)
|
||||
c.log()
|
||||
}
|
||||
|
||||
|
|
@ -131,8 +138,8 @@ func (c *Context) Err(err Error) {
|
|||
|
||||
// HTTPErr is a http.Error wrapper
|
||||
func (c *Context) HTTPErr(err string, code int) {
|
||||
c.Code = code
|
||||
http.Error(c.Response, err, code)
|
||||
c.code = code
|
||||
http.Error(c.w, err, code)
|
||||
c.log()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue