TTime/backend/main.go

89 lines
2.6 KiB
Go
Raw Normal View History

2024-02-12 12:40:49 +01:00
package main
import (
"fmt"
2024-03-08 10:25:22 +01:00
"os"
_ "ttime/docs"
"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-03-08 10:25:22 +01:00
"github.com/BurntSushi/toml"
2024-02-28 11:29:32 +01:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
jwtware "github.com/gofiber/contrib/jwt"
2024-02-12 12:40:49 +01:00
)
// @title TTime API
// @version 0.0.1
// @description This is the API for TTime, a time tracking application.
// @license.name AGPL
// @license.url https://www.gnu.org/licenses/agpl-3.0.html
// @host localhost:8080
// @BasePath /api
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
2024-02-12 12:40:49 +01:00
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-03-08 10:25:22 +01:00
// Pretty print the current config with toml
2024-03-08 10:32:02 +01:00
_ = toml.NewEncoder(os.Stdout).Encode(conf)
2024-03-08 10:25:22 +01:00
fmt.Printf("Starting server on http://localhost:%d\n", conf.Port)
fmt.Printf("For documentation, go to http://localhost:%d/swagger/index.html\n", conf.Port)
// Connect to the database
db := database.DbConnect(conf.DbPath)
2024-03-17 14:38:20 +01:00
// Migrate the database
if err = db.Migrate(); err != nil {
fmt.Println("Error migrating database: ", err)
}
// 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
server.Get("/swagger/*", swagger.HandlerDefault)
// 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 unprotected routes
server.Post("/api/register", gs.Register)
server.Post("/api/login", gs.Login)
// Every route from here on will require a valid JWT
server.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
}))
// Protected routes (require a valid JWT bearer token authentication header)
2024-03-16 22:47:19 +01:00
server.Post("/api/submitReport", gs.SubmitWeeklyReport)
2024-03-14 21:25:14 +01:00
server.Get("/api/getUserProjects", gs.GetUserProjects)
server.Post("/api/loginrenew", gs.LoginRenew)
2024-03-16 22:47:19 +01:00
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
2024-03-13 17:52:37 +01:00
server.Post("/api/project", gs.CreateProject)
server.Get("/api/project/:projectId", gs.GetProject)
2024-03-17 23:17:06 +01:00
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
server.Post("/api/signReport", gs.SignReport)
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
// 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
}
}