Files
cluster/internal/api/api.go
loveuer 8b655d3496 refactor: reorganize models to pkg/model and add authentication module
- Move ORM models from internal/model to pkg/model organized by module (auth/k8s/registry)
- Add authentication module with login, user management handlers
- Update all import paths to use new model locations
- Add frontend auth pages (Login, UserManagement) and authStore
- Remove deprecated internal/model/model.go
2025-11-20 14:55:48 +08:00

132 lines
4.4 KiB
Go

package api
import (
"context"
"fmt"
"log"
"net"
"gitea.loveuer.com/loveuer/cluster/internal/middleware"
"gitea.loveuer.com/loveuer/cluster/internal/module/auth"
"gitea.loveuer.com/loveuer/cluster/internal/module/k8s"
"gitea.loveuer.com/loveuer/cluster/internal/module/registry"
registry_model "gitea.loveuer.com/loveuer/cluster/pkg/model/registry"
"gitea.loveuer.com/loveuer/cluster/pkg/store"
"github.com/gofiber/fiber/v3"
"gorm.io/gorm"
)
func Init(ctx context.Context, address string, db *gorm.DB, store store.Store) (func(context.Context) error, error) {
var (
err error
ln net.Listener
cfg = fiber.Config{
BodyLimit: 1024 * 1024 * 1024 * 10, // 10GB limit for large image layers
}
fn func(context.Context) error
)
app := fiber.New(cfg)
app.Use(middleware.Logger())
app.Use(middleware.Recovery())
app.Use(middleware.CORS())
// Ensure database migration for RegistryConfig
// This is done here to ensure the table exists before config APIs are called
if err := db.AutoMigrate(&registry_model.RegistryConfig{}); err != nil {
log.Printf("Warning: failed to migrate RegistryConfig: %v", err)
}
// Initialize auth module
if err := auth.Init(ctx, db); err != nil {
log.Printf("Warning: failed to initialize auth module: %v", err)
}
// Initialize k8s module
if err := k8s.Init(ctx, db, store); err != nil {
log.Printf("Warning: failed to initialize k8s module: %v", err)
}
// oci image apis
{
app.All("/v2/*", registry.Registry(ctx, db, store))
}
// registry image apis
{
registryAPI := app.Group("/api/v1/registry")
registryAPI.Get("/image/list", registry.RegistryImageList(ctx, db, store))
registryAPI.Get("/image/download/*", registry.RegistryImageDownload(ctx, db, store))
registryAPI.Post("/image/upload", registry.RegistryImageUpload(ctx, db, store))
registryAPI.Post("/image/fetch", registry.RegistryImageFetch(ctx, db, store))
// registry config apis
registryAPI.Get("/config", registry.RegistryConfigGet(ctx, db, store))
registryAPI.Post("/config", registry.RegistryConfigSet(ctx, db, store))
}
// k8s cluster apis
{
k8sAPI := app.Group("/api/v1/k8s")
// cluster config
k8sAPI.Get("/config", k8s.ClusterConfigGet(ctx, db, store))
k8sAPI.Post("/config", k8s.ClusterConfigSet(ctx, db, store))
// resource operations
k8sAPI.Post("/resource/apply", k8s.K8sResourceApply(ctx, db, store))
k8sAPI.Get("/resource/get", k8s.K8sResourceFetch(ctx, db, store))
k8sAPI.Post("/resource/update", k8s.K8sResourceUpdate(ctx, db, store))
// resource list
k8sAPI.Get("/namespace/list", k8s.K8sNamespaceList(ctx, db, store))
k8sAPI.Delete("/namespace/delete", k8s.K8sNamespaceDelete(ctx, db, store))
k8sAPI.Get("/deployment/list", k8s.K8sDeploymentList(ctx, db, store))
k8sAPI.Delete("/deployment/delete", k8s.K8sDeploymentDelete(ctx, db, store))
k8sAPI.Get("/statefulset/list", k8s.K8sStatefulSetList(ctx, db, store))
k8sAPI.Delete("/statefulset/delete", k8s.K8sStatefulSetDelete(ctx, db, store))
k8sAPI.Get("/configmap/list", k8s.K8sConfigMapList(ctx, db, store))
k8sAPI.Delete("/configmap/delete", k8s.K8sConfigMapDelete(ctx, db, store))
k8sAPI.Get("/pod/list", k8s.K8sPodList(ctx, db, store))
k8sAPI.Get("/pod/logs", k8s.K8sPodLogs(ctx, db, store))
k8sAPI.Delete("/pod/delete", k8s.K8sPodDelete(ctx, db, store))
k8sAPI.Get("/pv/list", k8s.K8sPVList(ctx, db, store))
k8sAPI.Get("/pvc/list", k8s.K8sPVCList(ctx, db, store))
k8sAPI.Get("/service/list", k8s.K8sServiceList(ctx, db, store))
k8sAPI.Delete("/service/delete", k8s.K8sServiceDelete(ctx, db, store))
}
// auth apis
{
authAPI := app.Group("/api/v1/auth")
authAPI.Post("/login", auth.Login(ctx, db))
authAPI.Get("/wallpaper", auth.Wallpaper(ctx))
authAPI.Get("/current", auth.GetCurrentUser(ctx))
// user management
authAPI.Get("/user/list", auth.UserList(ctx, db))
authAPI.Post("/user/create", auth.UserCreate(ctx, db))
authAPI.Post("/user/update/:id", auth.UserUpdate(ctx, db))
authAPI.Delete("/user/delete/:id", auth.UserDelete(ctx, db))
}
ln, err = net.Listen("tcp", address)
if err != nil {
return fn, fmt.Errorf("failed to listen on %s: %w", address, err)
}
go func() {
if err = app.Listener(ln); err != nil {
log.Fatalf("Fiber server failed on %s: %v", address, err)
}
}()
fn = func(_ctx context.Context) error {
log.Println("[W] service shutdown...")
if err = app.ShutdownWithContext(_ctx); err != nil {
return fmt.Errorf("[E] service shutdown failed, err = %w", err)
}
return nil
}
return fn, nil
}