implemented api handlers

This commit is contained in:
ston1th 2021-04-08 00:34:54 +02:00
commit 372915e24d
15 changed files with 302 additions and 73 deletions

View file

@ -2,6 +2,7 @@ package types
import (
"encoding/json"
"io"
"net/http"
"github.com/go-logr/logr"
@ -26,20 +27,19 @@ func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log l
Request: r,
Response: w,
Data: data,
logger: log,
Log: log,
}
}
// Context is the context object shared between http handlers
type Context struct {
Body []byte
Status int
Request *http.Request
Response http.ResponseWriter
Data *ContextData
logger logr.Logger
Data *ContextData
Log logr.Logger
}
// Method returns the request method
@ -70,7 +70,7 @@ func (c *Context) Var(name string) (ret string) {
}
func (c *Context) log() {
c.logger.Info("access",
c.Log.Info("access",
"addr", c.Request.RemoteAddr,
"method", c.Request.Method,
"url", c.Request.URL.Path,
@ -79,23 +79,38 @@ func (c *Context) log() {
}
func (c *Context) NotFound() {
c.Status = http.StatusNotFound
c.Response.WriteHeader(http.StatusNotFound)
c.Response.Write([]byte(`{"message":"not found","status":404}`))
c.log()
c.Err(ErrNotFound)
}
//TODO func (c *Context) MethodNotAllowed() {
// c.Status = http.StatusMethodNotAllowed
// c.Response.WriteHeader(http.StatusMethodNotAllowed)
// c.Response.Write([]byte(`{"message":"method not allowed","status":405}`))
// c.log()
//}
// OK is a empty HTTP 200 JSON response
func (c *Context) OK() {
c.Response.Write([]byte("{}"))
c.log()
}
func (c *Context) ReadBody() (b []byte, err error) {
b, err = io.ReadAll(c.Request.Body)
c.Request.Body.Close()
return
}
func (c *Context) Body(b []byte) {
c.Response.Write(b)
c.log()
}
// JSON is a json response
func (c *Context) JSON(v interface{}) {
err := json.NewEncoder(c.Response).Encode(v)
if err != nil {
c.Err(ise)
c.Err(ErrISE)
return
}
c.log()

View file

@ -19,7 +19,7 @@ type Err struct {
// Error returns the error string
func (a Err) Error() string {
return strconv.Itoa(a.status) + ": " + a.err
return `{"message":"` + a.err + `","status":` + strconv.Itoa(a.status) + `}`
}
// Status returns the HTTP status code
@ -35,11 +35,11 @@ func newError(err string, status int) Error {
}
var (
badreq = newError("bad request", http.StatusBadRequest)
invalid = newError("invalid data", http.StatusBadRequest)
forbid = newError("forbidden", http.StatusForbidden)
nf = newError("resource not found", http.StatusNotFound)
mna = newError("method not allowed", http.StatusMethodNotAllowed)
exists = newError("resource already exists", http.StatusConflict)
ise = newError("internal server error", http.StatusInternalServerError)
ErrBadreq = newError("bad request", http.StatusBadRequest)
ErrInvalid = newError("invalid data", http.StatusBadRequest)
ErrForbidden = newError("forbidden", http.StatusForbidden)
ErrNotFound = newError("not found", http.StatusNotFound)
ErrMNA = newError("method not allowed", http.StatusMethodNotAllowed)
ErrExists = newError("resource already exists", http.StatusConflict)
ErrISE = newError("internal server error", http.StatusInternalServerError)
)