TTime/backend/main.go

147 lines
4.9 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/projects"
"ttime/internal/handlers/reports"
"ttime/internal/handlers/users"
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/fiber/v2/middleware/logger"
"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
2024-03-29 18:42:53 +01:00
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @description Use the JWT token provided by the login endpoint to authenticate requests. **Prefix the token with "Bearer ".**
// @host localhost:8080
// @BasePath /api
2024-03-29 18:42:53 +01:00
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
2024-03-23 15:22:58 +01:00
/**
Main function for starting the server and initializing configurations.
Reads configuration from file, pretty prints it, connects to the database,
migrates it, and sets up routes for the server.
*/
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)
2024-03-18 23:04:08 +01:00
os.Exit(1)
2024-03-17 14:38:20 +01:00
}
// Migrate sample data, should not be used in production
if err = db.MigrateSampleData(); err != nil {
fmt.Println("Error migrating sample data: ", err)
2024-03-18 23:04:08 +01:00
os.Exit(1)
}
// Create the server
2024-02-29 20:02:13 +01:00
server := fiber.New()
2024-02-12 12:40:49 +01:00
// We want some logs
server.Use(logger.New())
// Sets up db middleware, accessed as Local "db" key
server.Use(database.DbMiddleware(&db))
// Mounts the swagger documentation, this is available at /swagger/index.html
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
// Create a group for our API
api := server.Group("/api")
// Register our unprotected routes
api.Post("/register", users.Register)
api.Post("/login", users.Login)
// Every route from here on will require a valid
// JWT bearer token authentication in the header
server.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
}))
// All user related routes
// userGroup := api.Group("/user") // Not currently in use
2024-03-29 15:36:42 +01:00
api.Get("/users/all", users.ListAllUsers)
api.Get("/project/getAllUsers", users.GetAllUsersProject)
2024-04-04 19:54:31 +02:00
api.Get("/username", users.GetUserName)
api.Post("/login", users.Login)
api.Post("/register", users.Register)
api.Post("/loginrenew", users.LoginRenew)
api.Post("/promoteToAdmin", users.PromoteToAdmin)
2024-03-29 15:36:42 +01:00
api.Put("/changeUserName", users.ChangeUserName)
api.Delete("/userdelete/:username", users.UserDelete) // Perhaps just use POST to avoid headaches
// All project related routes
// projectGroup := api.Group("/project") // Not currently in use
2024-04-02 13:08:55 +02:00
api.Get("/getProjectTimes/:projectName", projects.GetProjectTimesHandler)
api.Get("/getUserProjects/:username", projects.GetUserProjects)
api.Get("/project/:projectId", projects.GetProject)
api.Get("/checkIfProjectManager/:projectName", projects.IsProjectManagerHandler)
api.Get("/getUsersProject/:projectName", projects.ListAllUsersProject)
2024-03-29 15:36:42 +01:00
api.Post("/project", projects.CreateProject)
api.Post("/ProjectRoleChange", projects.ProjectRoleChange)
2024-04-03 15:53:15 +02:00
api.Put("/promoteToPm/:projectName", projects.PromoteToPm)
api.Put("/addUserToProject/:projectName", projects.AddUserToProjectHandler)
api.Delete("/removeUserFromProject/:projectName", projects.RemoveUserFromProject)
api.Delete("/removeProject/:projectName", projects.RemoveProject)
2024-03-29 15:36:42 +01:00
api.Delete("/project/:projectID", projects.DeleteProject)
2024-04-09 17:39:10 +02:00
// All report related routes
// reportGroup := api.Group("/report") // Not currently in use
api.Get("/getWeeklyReport", reports.GetWeeklyReport)
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
api.Get("/getAllWeeklyReports/:projectName", reports.GetAllWeeklyReports)
2024-03-29 15:36:42 +01:00
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
api.Put("/signReport/:reportId", reports.SignReport)
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
2024-04-09 17:39:10 +02:00
api.Put("/unsignReport/:reportId", reports.UnsignReport)
api.Delete("/deleteReport/:reportId", reports.DeleteReport)
2024-03-20 00:35:37 +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
}
}