161 lines
4.0 KiB
Go
161 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/gofiber/fiber/v3/middleware/logger"
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
|
"github.com/gofiber/fiber/v3/middleware/static"
|
|
|
|
"github.com/rogeecn/database_render/internal/template"
|
|
)
|
|
|
|
func main() {
|
|
// Initialize logging
|
|
// slog.SetLogLoggerLevel(slog.LevelDebug)
|
|
|
|
// Create template loader
|
|
config := template.TemplateConfig{
|
|
BuiltinPath: "./web/templates/builtin",
|
|
CustomPath: "./web/templates/custom",
|
|
CacheEnabled: true,
|
|
HotReload: true,
|
|
}
|
|
|
|
loader := template.NewTemplateLoader(config)
|
|
defer func() {
|
|
if loader.HotReload != nil {
|
|
loader.HotReload.Stop()
|
|
}
|
|
}()
|
|
|
|
// Create Fiber app
|
|
app := fiber.New()
|
|
|
|
// Middleware
|
|
app.Use(recover.New())
|
|
app.Use(logger.New())
|
|
app.Use(static.New("./web/static"))
|
|
|
|
// API routes
|
|
setupAPIRoutes(app, loader)
|
|
|
|
// Start server
|
|
port := ":8080"
|
|
fmt.Printf("Template system server starting on http://localhost%s\n", port)
|
|
if err := app.Listen(port); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func setupAPIRoutes(app *fiber.App, loader *template.TemplateLoader) {
|
|
// Template API routes
|
|
api := app.Group("/api/templates")
|
|
|
|
// Get available templates
|
|
api.Get("/available", func(c fiber.Ctx) error {
|
|
table := c.Query("table", "default")
|
|
templates, err := loader.GetAvailableTemplates(table)
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"templates": templates})
|
|
})
|
|
|
|
// Get template configuration
|
|
api.Get("/config", func(c fiber.Ctx) error {
|
|
config, err := loader.ConfigLoader.LoadTemplateConfig()
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(config)
|
|
})
|
|
|
|
// Get version history
|
|
api.Get("/versions/:template", func(c fiber.Ctx) error {
|
|
templateName := c.Params("template")
|
|
history, err := loader.VersionManager.GetVersionHistory(templateName)
|
|
if err != nil {
|
|
return c.Status(404).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(history)
|
|
})
|
|
|
|
// Create new version
|
|
api.Post("/versions/:template", func(c fiber.Ctx) error {
|
|
templateName := c.Params("template")
|
|
var request struct {
|
|
Description string `json:"description"`
|
|
Author string `json:"author"`
|
|
}
|
|
if err := c.Bind().JSON(&request); err != nil {
|
|
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
templatePath := fmt.Sprintf("./web/templates/custom/%s.html", templateName)
|
|
if err := loader.VersionManager.SaveVersion(templateName, templatePath, request.Description, request.Author); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(fiber.Map{"message": "Version saved successfully"})
|
|
})
|
|
|
|
// Rollback to version
|
|
api.Post("/versions/:template/rollback/:version", func(c fiber.Ctx) error {
|
|
templateName := c.Params("template")
|
|
version := c.Params("version")
|
|
|
|
if err := loader.VersionManager.Rollback(templateName, version); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.JSON(fiber.Map{"message": "Rollback successful"})
|
|
})
|
|
|
|
// Get template stats
|
|
api.Get("/stats", func(c fiber.Ctx) error {
|
|
stats := loader.VersionManager.GetVersionSummary()
|
|
return c.JSON(stats)
|
|
})
|
|
|
|
// Hot reload status
|
|
api.Get("/hotreload/status", func(c fiber.Ctx) error {
|
|
if loader.HotReload == nil {
|
|
return c.JSON(fiber.Map{"status": "disabled"})
|
|
}
|
|
|
|
stats := loader.HotReload.GetReloadStats()
|
|
return c.JSON(stats)
|
|
})
|
|
|
|
// Manual reload
|
|
api.Post("/hotreload/reload", func(c fiber.Ctx) error {
|
|
if loader.HotReload != nil {
|
|
loader.HotReload.ReloadTemplates()
|
|
}
|
|
loader.ClearCache()
|
|
return c.JSON(fiber.Map{"message": "Templates reloaded"})
|
|
})
|
|
|
|
// Health check
|
|
api.Get("/health", func(c fiber.Ctx) error {
|
|
health := map[string]interface{}{
|
|
"status": "healthy",
|
|
"timestamp": time.Now(),
|
|
}
|
|
|
|
if loader.VersionManager != nil {
|
|
health["version_manager"] = loader.VersionManager.GetHealth()
|
|
}
|
|
|
|
if loader.HotReload != nil {
|
|
health["hot_reload"] = loader.HotReload.GetReloadStats()
|
|
}
|
|
|
|
return c.JSON(health)
|
|
})
|
|
}
|