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-29 14:37:22 +01:00
|
|
|
"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"
|
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-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 ".**
|
2024-03-19 00:27:31 +01:00
|
|
|
|
2024-03-08 10:16:56 +01:00
|
|
|
// @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-08 10:16:56 +01:00
|
|
|
|
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-29 14:37:22 +01:00
|
|
|
|
2024-03-17 14:38:20 +01:00
|
|
|
// Migrate the database
|
2024-04-14 09:21:34 +02:00
|
|
|
if err = database.Migrate(db); err != nil {
|
2024-03-17 14:38:20 +01:00
|
|
|
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-29 14:37:22 +01:00
|
|
|
// Migrate sample data, should not be used in production
|
2024-04-14 09:21:34 +02:00
|
|
|
if err = database.MigrateSampleData(db); err != nil {
|
2024-03-18 22:07:02 +01:00
|
|
|
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
|
|
|
// Create the server
|
2024-02-29 20:02:13 +01:00
|
|
|
server := fiber.New()
|
2024-02-12 12:40:49 +01:00
|
|
|
|
2024-03-29 14:37:22 +01:00
|
|
|
// We want some logs
|
2024-03-18 23:38:50 +01:00
|
|
|
server.Use(logger.New())
|
|
|
|
|
2024-03-29 14:37:22 +01:00
|
|
|
// Sets up db middleware, accessed as Local "db" key
|
|
|
|
server.Use(database.DbMiddleware(&db))
|
|
|
|
|
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-29 14:37:22 +01:00
|
|
|
// Create a group for our API
|
|
|
|
api := server.Group("/api")
|
|
|
|
|
2024-03-06 12:51:46 +01:00
|
|
|
// Register our unprotected routes
|
2024-03-29 14:37:22 +01:00
|
|
|
api.Post("/register", users.Register)
|
|
|
|
api.Post("/login", users.Login)
|
2024-03-06 12:51:46 +01:00
|
|
|
|
2024-03-29 14:37:22 +01:00
|
|
|
// Every route from here on will require a valid
|
|
|
|
// JWT bearer token authentication in the header
|
2024-03-06 12:51:46 +01:00
|
|
|
server.Use(jwtware.New(jwtware.Config{
|
|
|
|
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
|
|
|
|
}))
|
|
|
|
|
2024-03-29 14:37:22 +01:00
|
|
|
// 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)
|
2024-03-29 14:37:22 +01:00
|
|
|
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
|
2024-03-29 14:37:22 +01:00
|
|
|
|
|
|
|
// 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)
|
2024-03-29 20:19:22 +01:00
|
|
|
api.Get("/getUserProjects/:username", projects.GetUserProjects)
|
2024-03-29 14:37:22 +01:00
|
|
|
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)
|
2024-04-03 18:08:02 +02:00
|
|
|
api.Delete("/removeUserFromProject/:projectName", projects.RemoveUserFromProject)
|
2024-03-29 14:37:22 +01:00
|
|
|
api.Delete("/removeProject/:projectName", projects.RemoveProject)
|
2024-03-29 15:36:42 +01:00
|
|
|
api.Delete("/project/:projectID", projects.DeleteProject)
|
2024-03-29 14:37:22 +01:00
|
|
|
|
|
|
|
// All report related routes
|
|
|
|
// reportGroup := api.Group("/report") // Not currently in use
|
|
|
|
api.Get("/getWeeklyReport", reports.GetWeeklyReport)
|
|
|
|
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
|
2024-04-03 17:31:39 +02:00
|
|
|
api.Get("/getAllWeeklyReports/:projectName", reports.GetAllWeeklyReports)
|
2024-04-14 07:49:39 +02:00
|
|
|
api.Get("/getStatistics", reports.GetStatistics)
|
2024-03-29 15:36:42 +01:00
|
|
|
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
|
2024-03-29 15:33:20 +01:00
|
|
|
api.Put("/signReport/:reportId", reports.SignReport)
|
2024-03-29 14:37:22 +01:00
|
|
|
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
|
2024-04-09 17:39:10 +02:00
|
|
|
api.Put("/unsignReport/:reportId", reports.UnsignReport)
|
2024-04-09 19:08:22 +02:00
|
|
|
api.Delete("/deleteReport/:reportId", reports.DeleteReport)
|
2024-03-20 00:35:37 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|