This commit is contained in:
grumbulon 2023-02-24 22:36:53 -05:00
parent 3df52fd618
commit d168b52184
3 changed files with 5177 additions and 1 deletions

5124
frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -8,16 +8,23 @@ import (
"time"
"dns.froth.zone/pomme/internal"
"dns.froth.zone/pomme/internal/logger"
"github.com/go-chi/render"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
var l = logger.NewResponder()
// NewUser takes a POST request and user form and creates a user in the database.
func NewUser(w http.ResponseWriter, r *http.Request) {
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
if !ok {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": "unable to connect to DB"})
re := logger.Response[string, int]{
Message: "internal server error",
Status: http.StatusInternalServerError,
}
l.NewMessage(w, r, re)
}
var result internal.User

45
internal/logger/logger.go Normal file
View file

@ -0,0 +1,45 @@
package logger
import (
"fmt"
"net/http"
"github.com/go-chi/render"
)
type HTTPResponder interface {
comparable
string | int | bool
}
type Response[T string, R int] struct {
Message, Realm, Err T
Status R
}
func NewResponder[T string, R int]() Response[T, R] {
return Response[T, R]{}
}
func (resp *Response[T, R]) NewMessage(w http.ResponseWriter, r *http.Request, e Response[T, R]) {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch e.Realm {
default:
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`realm="%s"`, e.Realm))
fallthrough
case "":
w.Header().Add("API Error", e.Message)
}
w.WriteHeader(e.Status)
v := map[string]any{
"message": message,
"status": status,
}
render.JSON(w, r, v)
return
}