mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-12-22 15:50:44 +00:00
occam's razor moment
This commit is contained in:
parent
e0d72ef13b
commit
9d79f5e878
7 changed files with 43 additions and 18 deletions
|
@ -51,7 +51,7 @@ func API() http.Handler {
|
|||
render.JSON(w, r, resp)
|
||||
}),
|
||||
))
|
||||
//api.Use(setDBMiddleware)
|
||||
|
||||
api.With(setDBMiddleware).Post("/create", NewUser)
|
||||
api.With(setDBMiddleware).Post("/login", Login)
|
||||
api.Post("/logout", Logout)
|
||||
|
|
|
@ -38,7 +38,7 @@ type accountTest struct {
|
|||
}
|
||||
|
||||
func makeTestToken(username string) (tokenString string, err error) {
|
||||
claim := map[string]interface{}{"username": username, "admin": false}
|
||||
claim := map[string]any{"username": username, "admin": false}
|
||||
|
||||
jwtauth.SetExpiry(claim, time.Now().Add(time.Minute))
|
||||
|
||||
|
@ -73,8 +73,10 @@ func TestAPI(t *testing.T) {
|
|||
defer ts.Close()
|
||||
|
||||
tester := Init(ts.URL)
|
||||
// test mode
|
||||
mode = "test"
|
||||
db, err, ok := db.InitDb(config.DB, mode)
|
||||
|
||||
db, err, ok := db.InitDb(config.DB)
|
||||
if err != nil && !ok {
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
@ -95,7 +97,9 @@ func TestAPI(t *testing.T) {
|
|||
|
||||
func Init(url string) accountTest {
|
||||
user := autoUname()
|
||||
user = user + "-testUser"
|
||||
|
||||
user += "-testUser"
|
||||
|
||||
token, err := makeTestToken(user)
|
||||
if err != nil {
|
||||
return accountTest{}
|
||||
|
@ -310,8 +314,20 @@ func (a *accountTest) TestUpload(t *testing.T) {
|
|||
}
|
||||
|
||||
func (a *accountTest) CleanUp(db *gorm.DB) {
|
||||
var user internal.User
|
||||
var req internal.ZoneRequest
|
||||
var (
|
||||
user internal.User
|
||||
req internal.ZoneRequest
|
||||
)
|
||||
|
||||
db.Where("username = ?", a.username).Delete(&user)
|
||||
|
||||
db.Where("user = ?", a.username).Delete(&req)
|
||||
|
||||
if err := os.Remove("pomme-test.sqlite"); err != nil {
|
||||
l := newResponder(Response[any]{
|
||||
Message: "unable to clean up test DB",
|
||||
Err: err.Error(),
|
||||
})
|
||||
l.writeLogEntry()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,14 +29,12 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||
var result internal.User
|
||||
|
||||
if _, err := r.Cookie("jwt"); err == nil {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
resp := internal.Response{
|
||||
Message: "Already logged in",
|
||||
}
|
||||
render.JSON(w, r, resp)
|
||||
logger := newResponder(Response[any]{
|
||||
Message: "already logged in",
|
||||
Status: http.StatusOK,
|
||||
})
|
||||
logger.apiError(w, r)
|
||||
logger.writeLogEntry()
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ import (
|
|||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var mode string
|
||||
|
||||
// setDBMiddleware is the http Handler func for the GORM middleware with context.
|
||||
func setDBMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -20,6 +22,7 @@ func setDBMiddleware(next http.Handler) http.Handler {
|
|||
pommeDB *gorm.DB
|
||||
ok bool
|
||||
)
|
||||
|
||||
c, err := internal.ReadConfig()
|
||||
if err != nil {
|
||||
logger := newResponder(Response[any]{
|
||||
|
@ -29,7 +32,7 @@ func setDBMiddleware(next http.Handler) http.Handler {
|
|||
logger.writeLogEntry()
|
||||
}
|
||||
|
||||
pommeDB, err, ok = db.InitDb(c.DB)
|
||||
pommeDB, err, ok = db.InitDb(c.DB, mode)
|
||||
|
||||
if err != nil && !ok {
|
||||
logger := newResponder(Response[any]{
|
||||
|
|
|
@ -15,6 +15,8 @@ import (
|
|||
|
||||
// NewUser takes a POST request and user form and creates a user in the database.
|
||||
func NewUser(w http.ResponseWriter, r *http.Request) {
|
||||
var result internal.User
|
||||
|
||||
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
|
||||
if !ok {
|
||||
logger := newResponder(Response[any]{
|
||||
|
@ -26,8 +28,6 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var result internal.User
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
logger := newResponder(Response[any]{
|
||||
|
@ -55,6 +55,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
|
|||
Status: http.StatusInternalServerError,
|
||||
})
|
||||
logger.apiError(w, r)
|
||||
logger.writeLogEntry()
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -67,6 +68,7 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
|
|||
Status: http.StatusInternalServerError,
|
||||
})
|
||||
logger.apiError(w, r)
|
||||
logger.writeLogEntry()
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ import (
|
|||
// @Router /api/upload [post]
|
||||
func ReceiveFile(w http.ResponseWriter, r *http.Request) {
|
||||
var result internal.User
|
||||
|
||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1*1024*1024) // approx 1 mb max upload
|
||||
|
|
|
@ -9,8 +9,13 @@ import (
|
|||
)
|
||||
|
||||
// InitDb is the init function for the database.
|
||||
func InitDb(path string) (db *gorm.DB, err error, ok bool) {
|
||||
func InitDb(path, mode string) (db *gorm.DB, err error, ok bool) {
|
||||
ok = true
|
||||
|
||||
if mode == "test" {
|
||||
path = "pomme-test.sqlite"
|
||||
}
|
||||
|
||||
db, err = gorm.Open(sqlite.Open(path), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue