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"
|
2024-02-28 03:21:13 +01:00
|
|
|
"ttime/internal/config"
|
2024-02-13 09:20:49 +01:00
|
|
|
"ttime/internal/database"
|
2024-03-02 02:38:26 +01:00
|
|
|
"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() {
|
2024-02-28 03:21:13 +01:00
|
|
|
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 03:21:13 +01:00
|
|
|
}
|
|
|
|
|
2024-02-28 08:39:42 +01:00
|
|
|
// Pretty print the current config
|
|
|
|
str, _ := json.MarshalIndent(conf, "", " ")
|
|
|
|
fmt.Println(string(str))
|
2024-02-28 03:21:13 +01:00
|
|
|
|
2024-03-02 02:38:26 +01:00
|
|
|
// 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
|
|
|
|
2024-03-02 02:38:26 +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
|
|
|
|
2024-03-02 02:38:26 +01:00
|
|
|
// Register our handlers
|
|
|
|
server.Post("/api/register", gs.Register)
|
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)
|
|
|
|
|
2024-03-02 02:38:26 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|