Add fiber http backend

This commit is contained in:
2024-10-27 23:15:38 +01:00
parent a74f722c58
commit 7903bb7830
5 changed files with 92 additions and 22 deletions

View File

@@ -1,8 +1,8 @@
package main
import (
"flag"
"fmt"
"github.com/gofiber/fiber/v3"
"io"
"log"
"os"
@@ -36,10 +36,6 @@ var ns NoteService
var as AssociationService
func main() {
inputFile := flag.String("if", "input", "Input file")
flag.Parse()
log.Printf("Input file: %s", *inputFile)
db = DB{
path: "data/db.db",
}
@@ -55,24 +51,41 @@ func main() {
ns = NoteService{db: &db}
as = AssociationService{db: &db}
inputData, err := os.ReadFile(*inputFile)
if err != nil {
Error.Printf("Failed reading input file: %v", err)
return
}
log.Printf("%#v", string(inputData))
app := fiber.New()
note, err := ParseNote(string(inputData))
app.Post("/note/new", CreateNote)
log.Fatal(app.Listen(":3000"))
}
func CreateNote(c fiber.Ctx) error {
data := c.Body()
res := Response{}
note, err := ParseNote(string(data))
if err != nil {
Error.Printf("Failed parsing note: %v", err)
return
res.Success = false
res.Message = err.Error()
return c.Status(400).JSON(res)
}
dbnote, ass, err := PersistNoteData(note)
if note.Player == "" {
Error.Printf("No player specified in note: %v", note)
res.Success = false
res.Message = "No player specified"
return c.Status(400).JSON(res)
}
dbnote, _, err := PersistNoteData(note)
if err != nil {
Error.Printf("Failed persisting note: %v", err)
return
res.Success = false
res.Message = err.Error()
return c.Status(500).JSON(res)
}
log.Printf("%#v", dbnote)
log.Printf("%#v", ass)
res.Data = dbnote
res.Message = "OK"
return c.Status(200).JSON(res)
}