Initial status page
This commit is contained in:
parent
a3511b1bfc
commit
a1f35b97d3
17 changed files with 618 additions and 83 deletions
24
pkg/httpx/basic.go
Normal file
24
pkg/httpx/basic.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package httpx
|
||||
|
||||
import "net/http"
|
||||
|
||||
var basicUnauthorized = []byte("Unauthorized")
|
||||
|
||||
// BasicAuth returns a new [http.Handler] that requires any credentials to pass the check function
|
||||
func BasicAuth(handler http.Handler, realm string, check func(username, password string) bool) http.Handler {
|
||||
var authenticateHeader = `Basic realm="` + realm + `"`
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// if the basic authentication passes
|
||||
// we can just use the handler!
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if ok && check(user, pass) {
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// http authentication did not pass
|
||||
w.Header().Add("WWW-Authenticate", authenticateHeader)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write(basicUnauthorized)
|
||||
})
|
||||
}
|
||||
41
pkg/httpx/html.go
Normal file
41
pkg/httpx/html.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package httpx
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HTMLHandler[T any] struct {
|
||||
Handler func(r *http.Request) (T, error)
|
||||
Template *template.Template // called with T
|
||||
}
|
||||
|
||||
var htmlInternalServerErr = []byte(`<!DOCTYPE HTML><title>Internal Server Error</title>Internal Server Error`)
|
||||
var htmlNotFound = []byte(`<!DOCTYPE HTML><title>Not Found</title>Not Found`)
|
||||
|
||||
// ServeHTTP calls j(r) and returns json
|
||||
func (h HTMLHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// call the function
|
||||
result, err := h.Handler(r)
|
||||
|
||||
// entity not found
|
||||
if err == ErrNotFound {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write(htmlNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// handle other errors
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write(htmlInternalServerErr)
|
||||
return
|
||||
}
|
||||
|
||||
// write out the response as json
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
h.Template.Execute(w, result)
|
||||
}
|
||||
6
pkg/httpx/httpx.go
Normal file
6
pkg/httpx/httpx.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package httpx
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrNotFound should be returned from any httpx error to indicate that the item was not found
|
||||
var ErrNotFound = errors.New("httpx: Error 404")
|
||||
45
pkg/httpx/json.go
Normal file
45
pkg/httpx/json.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var jsonInternalServerErr = []byte(`{"status":"internal server error"}`)
|
||||
var jsonNotFound = []byte(`{"status":"not found"}`)
|
||||
|
||||
// JSON creates a new JSONHandler
|
||||
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)
|
||||
|
||||
// entity not found
|
||||
if err == ErrNotFound {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write(jsonNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// handle other errors
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write(jsonInternalServerErr)
|
||||
return
|
||||
}
|
||||
|
||||
// write out the response as json
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue