77 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 	_ "ttime/docs"
 | |
| 	"ttime/internal/config"
 | |
| 	"ttime/internal/database"
 | |
| 	"ttime/internal/handlers"
 | |
| 
 | |
| 	"github.com/gofiber/fiber/v2"
 | |
| 	"github.com/gofiber/swagger"
 | |
| 	_ "github.com/mattn/go-sqlite3"
 | |
| 
 | |
| 	jwtware "github.com/gofiber/contrib/jwt"
 | |
| )
 | |
| 
 | |
| //	@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/
 | |
| 
 | |
| func main() {
 | |
| 	conf, err := config.ReadConfigFromFile("config.toml")
 | |
| 	if err != nil {
 | |
| 		conf = config.NewConfig()
 | |
| 		_ = conf.WriteConfigToFile("config.toml")
 | |
| 	}
 | |
| 
 | |
| 	// Pretty print the current config
 | |
| 	str, _ := json.MarshalIndent(conf, "", "  ")
 | |
| 	fmt.Println(string(str))
 | |
| 
 | |
| 	// Connect to the database
 | |
| 	db := database.DbConnect(conf.DbPath)
 | |
| 	// Get our global state
 | |
| 	gs := handlers.NewGlobalState(db)
 | |
| 	// Create the server
 | |
| 	server := fiber.New()
 | |
| 
 | |
| 	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
 | |
| 	server.Static("/", "./static")
 | |
| 
 | |
| 	// Register our unprotected routes
 | |
| 	server.Post("/api/register", gs.Register)
 | |
| 
 | |
| 	// Register handlers for example button count
 | |
| 	server.Get("/api/button", gs.GetButtonCount)
 | |
| 	server.Post("/api/button", gs.IncrementButtonCount)
 | |
| 
 | |
| 	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")},
 | |
| 	}))
 | |
| 
 | |
| 	server.Post("/api/loginrenew", gs.LoginRenew)
 | |
| 	server.Delete("/api/userdelete", gs.UserDelete) // Perhaps just use POST to avoid headaches
 | |
| 
 | |
| 	// Announce the port we are listening on and start the server
 | |
| 	err = server.Listen(fmt.Sprintf(":%d", conf.Port))
 | |
| 	if err != nil {
 | |
| 		fmt.Println("Error starting server: ", err)
 | |
| 	}
 | |
| }
 | 
