Fix PUT endpoint

This commit is contained in:
Melon 2024-03-15 15:26:57 +00:00
parent 63aa76b63a
commit 3cd41fbfc7
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 27 additions and 1 deletions

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"context"
"errors" "errors"
"flag" "flag"
"github.com/1f349/gomvn" "github.com/1f349/gomvn"
"github.com/1f349/gomvn/database"
"github.com/1f349/gomvn/routes" "github.com/1f349/gomvn/routes"
exitReload "github.com/MrMelon54/exit-reload" exitReload "github.com/MrMelon54/exit-reload"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -53,6 +55,8 @@ func main() {
if err != nil { if err != nil {
log.Fatal("[GoMVN] Error: invalid database: ", err) log.Fatal("[GoMVN] Error: invalid database: ", err)
} }
mustHaveUser(db)
repoBasePath := filepath.Join(wd, "repositories") repoBasePath := filepath.Join(wd, "repositories")
err = os.MkdirAll(repoBasePath, os.ModePerm) err = os.MkdirAll(repoBasePath, os.ModePerm)
if err != nil { if err != nil {
@ -61,7 +65,7 @@ func main() {
srv := &http.Server{ srv := &http.Server{
Addr: config.Listen, Addr: config.Listen,
Handler: routes.Router(db, config.Name, repoDir, config.Repository), Handler: routes.Router(db, config.Name, repoBasePath, config.Repository),
ReadTimeout: time.Minute, ReadTimeout: time.Minute,
ReadHeaderTimeout: time.Minute, ReadHeaderTimeout: time.Minute,
WriteTimeout: time.Minute, WriteTimeout: time.Minute,
@ -82,3 +86,20 @@ func main() {
} }
}) })
} }
func mustHaveUser(db *database.Queries) error {
// user must exist
users, err := db.CountUsers(context.Background())
if err != nil {
return err
}
if users == 0 {
_, err := db.CreateUser(context.Background(), database.CreateUserParams{
Name: "admin",
Admin: true,
TokenHash: "1234",
})
return err
}
return nil
}

View File

@ -103,6 +103,11 @@ func (r *routeCtx) handlePut(rw http.ResponseWriter, req *http.Request, params h
http.Error(rw, "404 Not Found", http.StatusNotFound) http.Error(rw, "404 Not Found", http.StatusNotFound)
return return
} }
err = os.MkdirAll(filepath.Dir(p), os.ModePerm)
if err != nil {
http.Error(rw, "500 Failed to create directory", http.StatusInternalServerError)
return
}
create, err := os.Create(p) create, err := os.Create(p)
if err != nil { if err != nil {
http.Error(rw, "500 Failed to open file", http.StatusInternalServerError) http.Error(rw, "500 Failed to open file", http.StatusInternalServerError)