2024-02-12 12:40:49 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-08 10:25:22 +01:00
|
|
|
"os"
|
2024-03-08 10:16:56 +01:00
|
|
|
_ "ttime/docs"
|
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-03-08 10:25:22 +01:00
|
|
|
"github.com/BurntSushi/toml"
|
2024-02-28 11:29:32 +01:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-03-18 23:38:50 +01:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2024-03-08 10:16:56 +01:00
|
|
|
"github.com/gofiber/swagger"
|
2024-03-06 12:51:46 +01:00
|
|
|
|
|
|
|
jwtware "github.com/gofiber/contrib/jwt"
|
2024-02-12 12:40:49 +01:00
|
|
|
)
|
|
|
|
|
2024-03-08 10:16:56 +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-19 00:27:31 +01:00
|
|
|
//@securityDefinitions.apikey bererToken
|
|
|
|
//@in header
|
|
|
|
//@name Authorization
|
|
|
|
|
2024-03-08 10:16:56 +01:00
|
|
|
// @host localhost:8080
|
|
|
|
// @BasePath /api
|
|
|
|
|
|
|
|
// @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() {
|
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-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)
|
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)
|
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
|
|
|
}
|
|
|
|
|
2024-03-18 22:07:02 +01:00
|
|
|
if err = db.MigrateSampleData(); err != nil {
|
|
|
|
fmt.Println("Error migrating sample data: ", err)
|
2024-03-18 23:04:08 +01:00
|
|
|
os.Exit(1)
|
2024-03-18 22:07:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 02:38:26 +01:00
|
|
|
// 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-18 23:38:50 +01:00
|
|
|
server.Use(logger.New())
|
|
|
|
|
2024-03-18 22:07:02 +01:00
|
|
|
// Mounts the swagger documentation, this is available at /swagger/index.html
|
2024-03-08 10:16:56 +01:00
|
|
|
server.Get("/swagger/*", swagger.HandlerDefault)
|
|
|
|
|
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-06 12:51:46 +01:00
|
|
|
// Register our unprotected routes
|
2024-03-02 02:38:26 +01:00
|
|
|
server.Post("/api/register", gs.Register)
|
2024-03-06 12:51:46 +01:00
|
|
|
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")},
|
|
|
|
}))
|
|
|
|
|
2024-03-18 20:56:00 +01:00
|
|
|
// Protected routes (require a valid JWT bearer token authentication header)
|
2024-03-19 00:41:30 +01:00
|
|
|
server.Post("/api/submitWeeklyReport", gs.SubmitWeeklyReport)
|
2024-03-14 21:25:14 +01:00
|
|
|
server.Get("/api/getUserProjects", gs.GetUserProjects)
|
2024-03-06 12:51:46 +01:00
|
|
|
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-20 14:16:47 +01:00
|
|
|
server.Delete("api/project/:projectID", gs.DeleteProject) // WIP
|
2024-03-20 13:03:43 +01:00
|
|
|
server.Post("/api/project", gs.CreateProject) // WIP
|
2024-03-18 16:42:35 +01:00
|
|
|
server.Get("/api/project/:projectId", gs.GetProject)
|
2024-03-20 13:03:43 +01:00
|
|
|
server.Get("/api/project/getAllUsers", gs.GetAllUsersProject)
|
2024-03-17 23:17:06 +01:00
|
|
|
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
|
2024-03-18 14:47:15 +01:00
|
|
|
server.Post("/api/signReport", gs.SignReport)
|
|
|
|
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
2024-03-20 12:50:04 +01:00
|
|
|
server.Put("/api/changeUserName", gs.ChangeUserName)
|
2024-03-18 14:47:15 +01:00
|
|
|
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
2024-03-19 00:27:31 +01:00
|
|
|
server.Get("/api/users/all", gs.ListAllUsers)
|
2024-03-20 16:05:50 +01:00
|
|
|
server.Get("/api/getWeeklyReportsUser/:projectName", gs.GetWeeklyReportsUserHandler)
|
2024-03-20 17:50:51 +01:00
|
|
|
server.Get("/api/checkIfProjectManager/:projectName", gs.IsProjectManagerHandler)
|
2024-03-19 23:08:14 +01:00
|
|
|
server.Post("/api/ProjectRoleChange", gs.ProjectRoleChange)
|
2024-03-20 00:35:37 +01:00
|
|
|
server.Get("/api/getUsersProject/:projectName", gs.ListAllUsersProject)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|