Files
llama-link/internal/api/router.go
T
darroyo 0d6c8c7989
CI / test (push) Failing after 13m11s
Add embedded Vue SPA admin panel with go:embed
This is the complete fix for the missing /admin/ route and frontend serving:

Backend:
- internal/web/web.go: new package with go:embed for web/dist/
- internal/api/router.go: add routes for /admin/, /admin/*, /assets/*
- internal/db/db.go: fix SQLite DSN parsing (sqlite:///path -> path)

Build system:
- Makefile: new 'embed-prep' target copies web/dist to internal/web/dist
- make build now runs embed-prep -> frontend/build automatically

Deployment:
- deploy/llamalink.service: remove invalid --host/--port flags,
  add EnvironmentFile=/etc/llamalink/env

Verified:
- /health returns 200
- /admin/ serves Vue SPA HTML
- /assets/* serves CSS and JS files from embedded FS
- sqlite:///./llamalink.db works correctly
2026-07-31 14:47:15 -04:00

108 lines
3.2 KiB
Go

package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/llamalink/llamalink/internal/api/handlers"
"github.com/llamalink/llamalink/internal/api/middleware"
"github.com/llamalink/llamalink/internal/auth"
"github.com/llamalink/llamalink/internal/config"
"github.com/llamalink/llamalink/internal/llama"
"github.com/llamalink/llamalink/internal/quota"
"github.com/llamalink/llamalink/internal/web"
"gorm.io/gorm"
)
func New(cfg *config.Config, db *gorm.DB, llamaManager *llama.Manager) *gin.Engine {
if cfg.LlamalinkEnv == "production" {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.Recovery())
r.Use(gin.Logger())
// Initialize services
authService := auth.NewService(db)
quotaSvc := quota.NewService(db)
webhookSvc := quota.NewWebhookService(db)
proxy := llama.NewProxy(llamaManager)
// Initialize handlers
healthHandler := handlers.NewHealthHandler(db, llamaManager)
chatHandler := handlers.NewChatHandler(proxy, authService, quotaSvc, webhookSvc)
keysHandler := handlers.NewKeysHandler(authService)
modelsHandler := handlers.NewModelsHandler(llamaManager)
usageHandler := handlers.NewUsageHandler(db, quotaSvc)
// Health endpoints (public)
r.GET("/health", healthHandler.Health)
r.GET("/ready", healthHandler.Ready)
// API v1 group
v1 := r.Group("/v1")
// Chat completions (requires API key auth + chat scope)
chat := v1.Group("/chat")
chat.Use(middleware.APIKeyAuth(authService))
chat.POST("/completions", chatHandler.ChatCompletions)
// Keys management (admin only)
keys := v1.Group("/keys")
keys.Use(middleware.APIKeyAuth(authService))
keys.Use(middleware.AdminOnly())
keys.GET("", keysHandler.ListKeys)
keys.POST("", keysHandler.CreateKey)
keys.DELETE("/:id", keysHandler.RevokeKey)
// Models management
models := v1.Group("/models")
models.Use(middleware.APIKeyAuth(authService))
models.GET("", modelsHandler.ListModels)
models.POST("", modelsHandler.CreateModel)
models.GET("/active", modelsHandler.GetActiveModel)
models.GET("/:name", modelsHandler.GetActiveModel) // alias for compatibility
// Model load (admin only)
modelLoad := v1.Group("/models")
modelLoad.Use(middleware.APIKeyAuth(authService))
modelLoad.Use(middleware.AdminOnly())
modelLoad.POST("/:name/load", modelsHandler.LoadModel)
// Usage (requires API key auth)
usage := v1.Group("/usage")
usage.Use(middleware.APIKeyAuth(authService))
usage.GET("", usageHandler.GetCurrentKeyUsage)
usage.GET("/:key_id", usageHandler.GetUsage)
// Admin SPA (static files with embedded frontend)
r.GET("/assets/*filepath", func(c *gin.Context) {
filepath := c.Param("filepath")
data, err := web.ServeAsset(filepath)
if err != nil {
c.String(http.StatusNotFound, "asset not found")
return
}
c.Data(http.StatusOK, http.DetectContentType(data), data)
})
r.GET("/admin", func(c *gin.Context) {
index, err := web.Index()
if err != nil {
c.String(http.StatusInternalServerError, "index.html not found")
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", index)
})
r.GET("/admin/*filepath", func(c *gin.Context) {
index, err := web.Index()
if err != nil {
c.String(http.StatusInternalServerError, "index.html not found")
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", index)
})
return r
}