2021-03-18 02:14:06 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/logging"
|
2021-03-21 20:38:37 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
2021-03-18 02:14:06 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/templates"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HMNRouter struct {
|
|
|
|
HttpRouter *httprouter.Router
|
2021-03-21 20:38:37 +00:00
|
|
|
Wrappers []HMNHandlerWrapper
|
2021-03-18 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HMNRouter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
r.HttpRouter.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HMNRouter) Handle(method, route string, handler HMNHandler) {
|
2021-03-21 20:38:37 +00:00
|
|
|
for i := len(r.Wrappers) - 1; i >= 0; i-- {
|
|
|
|
handler = r.Wrappers[i](handler)
|
|
|
|
}
|
2021-03-18 02:14:06 +00:00
|
|
|
r.HttpRouter.Handle(method, route, handleHmnHandler(route, handler))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HMNRouter) GET(route string, handler HMNHandler) {
|
|
|
|
r.Handle(http.MethodGet, route, handler)
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
// TODO: POST, etc.
|
|
|
|
|
2021-03-18 02:14:06 +00:00
|
|
|
func (r *HMNRouter) ServeFiles(path string, root http.FileSystem) {
|
|
|
|
r.HttpRouter.ServeFiles(path, root)
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
func (r *HMNRouter) WithWrappers(wrappers ...HMNHandlerWrapper) *HMNRouter {
|
|
|
|
result := *r
|
|
|
|
result.Wrappers = append(result.Wrappers, wrappers...)
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:14:06 +00:00
|
|
|
type HMNHandler func(c *RequestContext, p httprouter.Params)
|
2021-03-21 20:38:37 +00:00
|
|
|
type HMNHandlerWrapper func(h HMNHandler) HMNHandler
|
2021-03-18 02:14:06 +00:00
|
|
|
|
|
|
|
type RequestContext struct {
|
|
|
|
StatusCode int
|
2021-03-21 20:38:37 +00:00
|
|
|
Body *bytes.Buffer
|
|
|
|
Logger *zerolog.Logger
|
|
|
|
Req *http.Request
|
|
|
|
Errors []error
|
|
|
|
|
|
|
|
rw http.ResponseWriter
|
2021-03-18 02:14:06 +00:00
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
currentProject *models.Project
|
2021-03-18 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRequestContext(rw http.ResponseWriter, req *http.Request, route string) *RequestContext {
|
2021-03-21 20:38:37 +00:00
|
|
|
logger := logging.With().Str("route", route).Logger()
|
|
|
|
|
2021-03-18 02:14:06 +00:00
|
|
|
return &RequestContext{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: new(bytes.Buffer),
|
2021-03-21 20:38:37 +00:00
|
|
|
Logger: &logger,
|
|
|
|
Req: req,
|
2021-03-18 02:14:06 +00:00
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
rw: rw,
|
2021-03-18 02:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RequestContext) Context() context.Context {
|
2021-03-21 20:38:37 +00:00
|
|
|
return c.Req.Context()
|
2021-03-18 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RequestContext) URL() *url.URL {
|
2021-03-21 20:38:37 +00:00
|
|
|
return c.Req.URL
|
2021-03-18 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RequestContext) Headers() http.Header {
|
|
|
|
return c.rw.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RequestContext) WriteTemplate(name string, data interface{}) error {
|
|
|
|
return templates.Templates[name].Execute(c.Body, data)
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
func (c *RequestContext) AddErrors(errs ...error) {
|
|
|
|
c.Errors = append(c.Errors, errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RequestContext) AbortWithErrors(status int, errs ...error) {
|
|
|
|
c.StatusCode = status
|
|
|
|
c.AddErrors(errs...)
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:14:06 +00:00
|
|
|
func handleHmnHandler(route string, h HMNHandler) httprouter.Handle {
|
|
|
|
return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
c := newRequestContext(rw, r, route)
|
2021-03-21 20:38:37 +00:00
|
|
|
defer func() {
|
|
|
|
/*
|
|
|
|
This panic recovery is the last resort. If you want to render
|
|
|
|
an error page or something, make it a request wrapper.
|
|
|
|
*/
|
|
|
|
if recovered := recover(); recovered != nil {
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
logging.LogPanicValue(c.Logger, recovered, "request panicked and was not handled")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-03-18 02:14:06 +00:00
|
|
|
h(c, p)
|
|
|
|
|
|
|
|
rw.WriteHeader(c.StatusCode)
|
|
|
|
io.Copy(rw, c.Body)
|
|
|
|
}
|
|
|
|
}
|