package api import ( "context" "fmt" "log" "net" "gitea.loveuer.com/loveuer/cluster/internal/middleware" "gitea.loveuer.com/loveuer/cluster/internal/model" "gitea.loveuer.com/loveuer/cluster/internal/module/k8s" "gitea.loveuer.com/loveuer/cluster/internal/module/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(&model.RegistryConfig{}); err != nil { log.Printf("Warning: failed to migrate RegistryConfig: %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.Get("/deployment/list", k8s.K8sDeploymentList(ctx, db, store)) k8sAPI.Get("/statefulset/list", k8s.K8sStatefulSetList(ctx, db, store)) k8sAPI.Get("/configmap/list", k8s.K8sConfigMapList(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)) } 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 }