TTime/backend/cmd/main.go
2024-02-28 11:29:32 +01:00

62 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"fmt"
"ttime/internal/config"
"ttime/internal/database"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3"
)
// The button state as represented in memory
type ButtonState struct {
PressCount int `json:"pressCount"`
}
// This is what a handler with a receiver looks like
// Keep in mind that concurrent state access is not (usually) safe
// And will in practice be guarded by a mutex
func (b *ButtonState) pressHandlerGet(c *fiber.Ctx) error {
fmt.Println("Get request received")
return c.JSON(b)
}
func (b *ButtonState) pressHandlerPost(c *fiber.Ctx) error {
fmt.Println("Post request received")
b.PressCount++
return c.JSON(b)
}
func main() {
conf, err := config.ReadConfigFromFile("config.toml")
if err != nil {
conf = config.NewConfig()
conf.WriteConfigToFile("config.toml")
}
// Pretty print the current config
str, _ := json.MarshalIndent(conf, "", " ")
fmt.Println(string(str))
database.DbConnect(conf.DbPath)
app := fiber.New()
app.Static("/", "./static")
b := &ButtonState{PressCount: 0}
app.Get("/api/button", b.pressHandlerGet)
app.Post("/api/button", b.pressHandlerPost)
app.Post("/api/project", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "Project created",
})
})
err = app.Listen(fmt.Sprintf(":%d", conf.Port))
if err != nil {
fmt.Println("Error starting server: ", err)
}
}