TTime/backend/cmd/main.go

50 lines
1.3 KiB
Go
Raw Normal View History

2024-02-12 12:40:49 +01:00
package main
import (
2024-02-13 09:19:25 +01:00
"encoding/json"
2024-02-12 12:40:49 +01:00
"fmt"
"ttime/internal/config"
2024-02-13 09:20:49 +01:00
"ttime/internal/database"
"ttime/internal/handlers"
2024-02-12 12:40:49 +01:00
2024-02-28 11:29:32 +01:00
"github.com/gofiber/fiber/v2"
2024-02-12 12:40:49 +01:00
_ "github.com/mattn/go-sqlite3"
)
func main() {
conf, err := config.ReadConfigFromFile("config.toml")
if err != nil {
conf = config.NewConfig()
2024-03-02 04:29:50 +01:00
_ = conf.WriteConfigToFile("config.toml")
}
2024-02-28 08:39:42 +01:00
// Pretty print the current config
str, _ := json.MarshalIndent(conf, "", " ")
fmt.Println(string(str))
// Connect to the database
db := database.DbConnect(conf.DbPath)
// Get our global state
gs := handlers.NewGlobalState(db)
// Create the server
2024-02-29 20:02:13 +01:00
server := fiber.New()
2024-02-12 12:40:49 +01:00
// Mount our static files (Beware of the security implications of this!)
// This will likely be replaced by an embedded filesystem in the future
2024-02-29 20:02:13 +01:00
server.Static("/", "./static")
2024-02-28 08:39:42 +01:00
// Register our handlers
server.Post("/api/register", gs.Register)
2024-03-06 10:14:54 +01:00
server.Delete("/api/userdelete", gs.UserDelete) // Perhaps just use POST to avoid headaches
2024-02-28 11:29:32 +01:00
2024-03-06 09:41:36 +01:00
// Register handlers for example button count
server.Get("/api/button", gs.GetButtonCount)
server.Post("/api/button", gs.IncrementButtonCount)
// Announce the port we are listening on and start the server
2024-02-29 20:02:13 +01:00
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
2024-02-12 12:40:49 +01:00
if err != nil {
2024-02-28 11:29:32 +01:00
fmt.Println("Error starting server: ", err)
2024-02-12 12:40:49 +01:00
}
}