You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
2.2 KiB

package main
import (
"embed"
"log"
"net/http"
"os"
"person-site/internal/db"
"person-site/internal/handlers"
"person-site/internal/models"
)
//go:embed migrations/*.sql
var migrationFS embed.FS
func main() {
dbPath := os.Getenv("DB_PATH")
if dbPath == "" {
dbPath = "app.db"
}
addr := os.Getenv("ADDR")
if addr == "" {
addr = ":8080"
}
database, err := db.Open(dbPath)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer database.Close()
if err := db.Migrate(database, migrationFS); err != nil {
log.Fatalf("Failed to run migrations: %v", err)
}
projectStore := &models.ProjectStore{DB: database}
configStore := &models.ConfigStore{DB: database}
userStore := &models.UserStore{DB: database}
authHandler := handlers.NewAuthHandler(userStore)
if err := authHandler.SeedAdmin(); err != nil {
log.Fatalf("Failed to seed admin user: %v", err)
}
projectHandler := &handlers.ProjectHandler{Store: projectStore}
configHandler := &handlers.ConfigHandler{Store: configStore}
exportHandler := &handlers.ExportHandler{ProjectStore: projectStore, ConfigStore: configStore}
mux := http.NewServeMux()
mux.HandleFunc("GET /api/projects", projectHandler.ListPublic)
mux.HandleFunc("GET /api/projects/{id}", projectHandler.GetPublic)
mux.HandleFunc("GET /api/config", configHandler.GetPublic)
adminMux := http.NewServeMux()
adminMux.HandleFunc("POST /api/admin/login", authHandler.Login)
adminMux.HandleFunc("GET /api/admin/projects", projectHandler.ListAdmin)
adminMux.HandleFunc("POST /api/admin/projects", projectHandler.Create)
adminMux.HandleFunc("PUT /api/admin/projects/{id}", projectHandler.Update)
adminMux.HandleFunc("DELETE /api/admin/projects/{id}", projectHandler.Delete)
adminMux.HandleFunc("GET /api/admin/config", configHandler.GetAdmin)
adminMux.HandleFunc("PUT /api/admin/config", configHandler.Update)
adminMux.HandleFunc("GET /api/admin/export", exportHandler.Export)
adminMux.HandleFunc("POST /api/admin/import", exportHandler.Import)
mux.Handle("/api/admin/", authHandler.Middleware(adminMux))
mux.Handle("/", spaHandler())
log.Printf("Server starting on %s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}