New Endpoint and security enhancement

- Userinfo endpoint is now available
- HTTPS is now required (closes #2)
This commit is contained in:
2026-03-03 15:41:07 +01:00
parent c75c405200
commit ef7ef3cf74
6 changed files with 93 additions and 21 deletions

View File

@@ -10,9 +10,11 @@ import (
)
type Server struct {
Port string
JWTSecret []byte
DatabasePath string
Port string
JWTSecret []byte
DatabasePath string
CertificatePath string
PrivateKeyPath string
}
var cfg, _ = config.LoadConfig()
@@ -42,9 +44,11 @@ func InitServer() *Server {
}
return &Server{
Port: cfg.Port,
JWTSecret: []byte(jwtSecret),
DatabasePath: cfg.DatabasePath,
Port: cfg.Port,
JWTSecret: []byte(jwtSecret),
DatabasePath: cfg.DatabasePath,
CertificatePath: cfg.CertificatePath,
PrivateKeyPath: cfg.PrivateKeyPath,
}
}
@@ -62,10 +66,11 @@ func (server *Server) Run() {
mux.Handle("/api/expenses", auth.AuthMiddleware(server.JWTSecret)(http.HandlerFunc(handlers.Expenses)))
mux.Handle("/api/balance", auth.AuthMiddleware(server.JWTSecret)(http.HandlerFunc(handlers.GetBalance)))
mux.Handle("/api/ping", auth.AuthMiddleware(server.JWTSecret)(http.HandlerFunc(handlers.TestHandler)))
mux.Handle("/api/userinfo", auth.AuthMiddleware(server.JWTSecret)(http.HandlerFunc(handlers.UserInfo)))
// Admin-only
mux.Handle("/api/admin", auth.AuthMiddleware(server.JWTSecret)(auth.RequireRole("admin")(http.HandlerFunc(handlers.AdminPanel))))
log.Printf("Listening on port %s", server.Port)
log.Fatal(http.ListenAndServe(":"+server.Port, mux))
log.Fatal(http.ListenAndServeTLS(":"+server.Port, server.CertificatePath, server.PrivateKeyPath, mux))
}