Initial commit
This commit is contained in:
78
server/server.go
Normal file
78
server/server.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"MiauInv/config"
|
||||
"MiauInv/frontend"
|
||||
"MiauInv/handlers"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Port string
|
||||
JWTSecret []byte
|
||||
DatabasePath string
|
||||
CertificatePath string
|
||||
PrivateKeyPath string
|
||||
}
|
||||
|
||||
func InitServer() *Server {
|
||||
|
||||
err := config.CheckIfExists()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
jwtSecret := os.Getenv("JWT_SECRET")
|
||||
if jwtSecret == "" {
|
||||
log.Fatal("JWT_SECRET environment variable not set.")
|
||||
return nil
|
||||
}
|
||||
if len(jwtSecret) < 32 {
|
||||
log.Fatal("JWT_SECRET must be at least 32 characters long.")
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Server{
|
||||
Port: cfg.Port,
|
||||
JWTSecret: []byte(jwtSecret),
|
||||
DatabasePath: cfg.DatabasePath,
|
||||
CertificatePath: cfg.CertificatePath,
|
||||
PrivateKeyPath: cfg.PrivateKeyPath,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Server) Run() {
|
||||
log.Println("Starting server...")
|
||||
mux := http.NewServeMux()
|
||||
|
||||
//
|
||||
// FRONTEND
|
||||
//
|
||||
mux.HandleFunc("/", frontend.Home)
|
||||
|
||||
//
|
||||
// API
|
||||
//
|
||||
|
||||
// Public
|
||||
mux.HandleFunc("/api/login", handlers.Login)
|
||||
mux.HandleFunc("/api/register", handlers.Register)
|
||||
mux.HandleFunc("/api/refresh", handlers.RefreshToken)
|
||||
mux.HandleFunc("/api/logout", handlers.Logout)
|
||||
|
||||
// Login required
|
||||
|
||||
// Admin-only
|
||||
|
||||
log.Printf("Listening on port %s", this.Port)
|
||||
log.Fatal(http.ListenAndServeTLS(":"+this.Port, this.CertificatePath, this.PrivateKeyPath, mux))
|
||||
}
|
||||
Reference in New Issue
Block a user