more work done

This commit is contained in:
ston1th 2021-03-06 16:51:36 +01:00
commit 6fda229a8e
23 changed files with 384 additions and 177 deletions

View file

@ -2,7 +2,6 @@ package types
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-logr/logr"
@ -16,7 +15,7 @@ func NewContext(w http.ResponseWriter, r *http.Request, log logr.Logger) *Contex
Status: http.StatusOK,
Request: r,
Response: w,
log: log,
logger: log,
}
}
@ -27,7 +26,7 @@ type Context struct {
Request *http.Request
Response http.ResponseWriter
log logr.Logger
logger logr.Logger
}
// Method returns the request method
@ -58,7 +57,7 @@ func (c *Context) Var(name string) (ret string) {
}
func (c *Context) log() {
c.log.Info("access",
c.logger.Info("access",
"addr", c.Request.RemoteAddr,
"method", c.Request.Method,
"url", c.Request.URL,

45
pkg/api/types/error.go Normal file
View file

@ -0,0 +1,45 @@
package types
import (
"net/http"
"strconv"
)
// Error interface is a custom api error
type Error interface {
Error() string
Status() int
}
// Err implements the Error interface
type Err struct {
err string
status int
}
// Error returns the error string
func (a Err) Error() string {
return strconv.Itoa(a.status) + ": " + a.err
}
// Status returns the HTTP status code
func (a Err) Status() int {
return a.status
}
func newError(err string, status int) Error {
return Err{
err: err,
status: status,
}
}
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)
)

View file

@ -2,6 +2,6 @@ package types
type Route struct {
Path string
Handler http.HandlerFunc
Handler CtxHandler
Methods []string
}