Implement initial login functionality

This commit is contained in:
Tom Wiesing 2022-12-05 16:14:54 +01:00
parent a3bd0db78c
commit 3aa79b0d23
No known key found for this signature in database
36 changed files with 908 additions and 70 deletions

View file

@ -28,7 +28,7 @@ func (ei ErrInterceptor) Intercept(w http.ResponseWriter, r *http.Request, err e
res = ei.Fallback
}
res.ServerHTTP(w, r)
res.ServeHTTP(w, r)
return true
}
@ -65,13 +65,13 @@ var (
)
var (
textInterceptor = StatusInterceptor("text/plain", func(code int, text string) ([]byte, error) {
TextInterceptor = StatusInterceptor("text/plain", func(code int, text string) ([]byte, error) {
return []byte(text), nil
})
jsonInterceptor = StatusInterceptor("application/json", func(code int, text string) ([]byte, error) {
JSONInterceptor = StatusInterceptor("application/json", func(code int, text string) ([]byte, error) {
return json.Marshal(map[string]any{"status": text, "code": code})
})
htmlInterceptor = StatusInterceptor("text/html", func(code int, text string) ([]byte, error) {
HTMLInterceptor = StatusInterceptor("text/html", func(code int, text string) ([]byte, error) {
return []byte(`<!DOCTYPE HTML><title>` + text + `</title>` + text), nil
})
)

View file

@ -5,6 +5,25 @@ import (
"net/http"
)
// WriteHTML writes a html response of type T to w.
// If an error occured, writes an error response instead.
func WriteHTML[T any](result T, err error, template *template.Template, templateName string, w http.ResponseWriter, r *http.Request) {
// intercept any errors
if HTMLInterceptor.Intercept(w, r, err) {
return
}
// write out the response as html
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if templateName != "" {
template.ExecuteTemplate(w, templateName, result)
} else {
template.Execute(w, result)
}
}
type HTMLHandler[T any] struct {
Handler func(r *http.Request) (T, error)
@ -16,19 +35,5 @@ type HTMLHandler[T any] struct {
func (h HTMLHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// call the function
result, err := h.Handler(r)
// intercept any errors
if htmlInterceptor.Intercept(w, r, err) {
return
}
// write out the response as json
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if h.TemplateName != "" {
h.Template.ExecuteTemplate(w, h.TemplateName, result)
} else {
h.Template.Execute(w, result)
}
WriteHTML(result, err, h.Template, h.TemplateName, w, r)
}

View file

@ -11,7 +11,7 @@ type Response struct {
StatusCode int // defaults to [http.StatusOK]
}
func (response Response) ServerHTTP(w http.ResponseWriter, r *http.Request) {
func (response Response) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if response.ContentType == "" {
response.ContentType = "text/plain"
}

View file

@ -10,17 +10,11 @@ func JSON[T any](f func(r *http.Request) (T, error)) JSONHandler[T] {
return JSONHandler[T](f)
}
// JSONHandler implements [http.Handler] by returning values as json to the caller.
// In case of an error, a generic "internal server error" message is returned.
type JSONHandler[T any] func(r *http.Request) (T, error)
// ServeHTTP calls j(r) and returns json
func (j JSONHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// call the function
result, err := j(r)
// WriteJSON writes a JSON response of type T to w.
// If an error occured, writes an error response instead.
func WriteJSON[T any](result T, err error, w http.ResponseWriter, r *http.Request) {
// handle any errors
if jsonInterceptor.Intercept(w, r, err) {
if JSONInterceptor.Intercept(w, r, err) {
return
}
@ -29,3 +23,13 @@ func (j JSONHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
// JSONHandler implements [http.Handler] by returning values as json to the caller.
// In case of an error, a generic "internal server error" message is returned.
type JSONHandler[T any] func(r *http.Request) (T, error)
// ServeHTTP calls j(r) and returns json
func (j JSONHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
result, err := j(r)
WriteJSON(result, err, w, r)
}

View file

@ -13,7 +13,7 @@ func (rh RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url, code, err := rh(r)
// intercept the errors
if textInterceptor.Intercept(w, r, err) {
if TextInterceptor.Intercept(w, r, err) {
return
}