mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-11-26 03:23:07 +01:00
Fix init order. Reformat.
This commit is contained in:
@@ -80,7 +80,7 @@ func initGops() error {
|
|||||||
if !flagGops {
|
if !flagGops {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := agent.Listen(agent.Options{}); err != nil {
|
if err := agent.Listen(agent.Options{}); err != nil {
|
||||||
return fmt.Errorf("starting gops agent: %w", err)
|
return fmt.Errorf("starting gops agent: %w", err)
|
||||||
}
|
}
|
||||||
@@ -96,17 +96,17 @@ func loadEnvironment() error {
|
|||||||
|
|
||||||
func initConfiguration() error {
|
func initConfiguration() error {
|
||||||
ccconf.Init(flagConfigFile)
|
ccconf.Init(flagConfigFile)
|
||||||
|
|
||||||
cfg := ccconf.GetPackageConfig("main")
|
cfg := ccconf.GetPackageConfig("main")
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return fmt.Errorf("main configuration must be present")
|
return fmt.Errorf("main configuration must be present")
|
||||||
}
|
}
|
||||||
|
|
||||||
clustercfg := ccconf.GetPackageConfig("clusters")
|
clustercfg := ccconf.GetPackageConfig("clusters")
|
||||||
if clustercfg == nil {
|
if clustercfg == nil {
|
||||||
return fmt.Errorf("cluster configuration must be present")
|
return fmt.Errorf("cluster configuration must be present")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Init(cfg, clustercfg)
|
config.Init(cfg, clustercfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ func handleDatabaseCommands() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("migrating database to version %d: %w", repository.Version, err)
|
return fmt.Errorf("migrating database to version %d: %w", repository.Version, err)
|
||||||
}
|
}
|
||||||
cclog.Exitf("MigrateDB Success: Migrated '%s' database at location '%s' to version %d.\n",
|
cclog.Exitf("MigrateDB Success: Migrated '%s' database at location '%s' to version %d.\n",
|
||||||
config.Keys.DBDriver, config.Keys.DB, repository.Version)
|
config.Keys.DBDriver, config.Keys.DB, repository.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func handleDatabaseCommands() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("reverting database to version %d: %w", repository.Version-1, err)
|
return fmt.Errorf("reverting database to version %d: %w", repository.Version-1, err)
|
||||||
}
|
}
|
||||||
cclog.Exitf("RevertDB Success: Reverted '%s' database at location '%s' to version %d.\n",
|
cclog.Exitf("RevertDB Success: Reverted '%s' database at location '%s' to version %d.\n",
|
||||||
config.Keys.DBDriver, config.Keys.DB, repository.Version-1)
|
config.Keys.DBDriver, config.Keys.DB, repository.Version-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,10 +140,10 @@ func handleDatabaseCommands() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("forcing database to version %d: %w", repository.Version, err)
|
return fmt.Errorf("forcing database to version %d: %w", repository.Version, err)
|
||||||
}
|
}
|
||||||
cclog.Exitf("ForceDB Success: Forced '%s' database at location '%s' to version %d.\n",
|
cclog.Exitf("ForceDB Success: Forced '%s' database at location '%s' to version %d.\n",
|
||||||
config.Keys.DBDriver, config.Keys.DB, repository.Version)
|
config.Keys.DBDriver, config.Keys.DB, repository.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ func handleUserCommands() error {
|
|||||||
if config.Keys.DisableAuthentication && (flagNewUser != "" || flagDelUser != "") {
|
if config.Keys.DisableAuthentication && (flagNewUser != "" || flagDelUser != "") {
|
||||||
return fmt.Errorf("--add-user and --del-user can only be used if authentication is enabled")
|
return fmt.Errorf("--add-user and --del-user can only be used if authentication is enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !config.Keys.DisableAuthentication {
|
if !config.Keys.DisableAuthentication {
|
||||||
if cfg := ccconf.GetPackageConfig("auth"); cfg != nil {
|
if cfg := ccconf.GetPackageConfig("auth"); cfg != nil {
|
||||||
auth.Init(&cfg)
|
auth.Init(&cfg)
|
||||||
@@ -189,7 +189,7 @@ func handleUserCommands() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ func handleUserCommands() error {
|
|||||||
func checkDefaultSecurityKeys() {
|
func checkDefaultSecurityKeys() {
|
||||||
// Default JWT public key from init.go
|
// Default JWT public key from init.go
|
||||||
defaultJWTPublic := "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0="
|
defaultJWTPublic := "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0="
|
||||||
|
|
||||||
if os.Getenv("JWT_PUBLIC_KEY") == defaultJWTPublic {
|
if os.Getenv("JWT_PUBLIC_KEY") == defaultJWTPublic {
|
||||||
cclog.Warn("Using default JWT keys - not recommended for production environments")
|
cclog.Warn("Using default JWT keys - not recommended for production environments")
|
||||||
}
|
}
|
||||||
@@ -218,7 +218,7 @@ func addUser(userSpec string) error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("adding user '%s' with roles '%s': %w", parts[0], parts[1], err)
|
return fmt.Errorf("adding user '%s' with roles '%s': %w", parts[0], parts[1], err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cclog.Printf("Add User: Added new user '%s' with roles '%s'.\n", parts[0], parts[1])
|
cclog.Printf("Add User: Added new user '%s' with roles '%s'.\n", parts[0], parts[1])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -240,7 +240,7 @@ func syncLDAP(authHandle *auth.Authentication) error {
|
|||||||
if err := authHandle.LdapAuth.Sync(); err != nil {
|
if err := authHandle.LdapAuth.Sync(); err != nil {
|
||||||
return fmt.Errorf("synchronizing LDAP: %w", err)
|
return fmt.Errorf("synchronizing LDAP: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cclog.Print("Sync LDAP: LDAP synchronization successfull.")
|
cclog.Print("Sync LDAP: LDAP synchronization successfull.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,7 @@ func initSubsystems() error {
|
|||||||
return fmt.Errorf("running job taggers: %w", err)
|
return fmt.Errorf("running job taggers: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,7 +322,7 @@ func runServer(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
memorystore.Init(mscfg, &wg)
|
memorystore.Init(mscfg, &wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start archiver and task manager
|
// Start archiver and task manager
|
||||||
archiver.Start(repository.GetJobRepository())
|
archiver.Start(repository.GetJobRepository())
|
||||||
taskManager.Start(ccconf.GetPackageConfig("cron"), ccconf.GetPackageConfig("archive"))
|
taskManager.Start(ccconf.GetPackageConfig("cron"), ccconf.GetPackageConfig("archive"))
|
||||||
@@ -424,11 +424,11 @@ func run() error {
|
|||||||
// Initialize subsystems in dependency order:
|
// Initialize subsystems in dependency order:
|
||||||
// 1. Load environment variables from .env file (contains sensitive configuration)
|
// 1. Load environment variables from .env file (contains sensitive configuration)
|
||||||
// 2. Load configuration from config.json (may reference environment variables)
|
// 2. Load configuration from config.json (may reference environment variables)
|
||||||
// 3. Initialize database connection (requires config for connection string)
|
// 3. Handle database migration commands if requested
|
||||||
// 4. Handle database commands if requested (requires active database connection)
|
// 4. Initialize database connection (requires config for connection string)
|
||||||
// 5. Handle user commands if requested (requires database and authentication config)
|
// 5. Handle user commands if requested (requires database and authentication config)
|
||||||
// 6. Initialize subsystems like archive and metrics (require config and database)
|
// 6. Initialize subsystems like archive and metrics (require config and database)
|
||||||
|
|
||||||
// Load environment and configuration
|
// Load environment and configuration
|
||||||
if err := loadEnvironment(); err != nil {
|
if err := loadEnvironment(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -438,13 +438,13 @@ func run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize database
|
// Handle database migration (migrate, revert, force)
|
||||||
if err := initDatabase(); err != nil {
|
if err := handleDatabaseCommands(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle database commands (migrate, revert, force)
|
// Initialize database
|
||||||
if err := handleDatabaseCommands(); err != nil {
|
if err := initDatabase(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,7 +458,7 @@ func run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start server if requested
|
// Exit if start server is not requested
|
||||||
if !flagServer {
|
if !flagServer {
|
||||||
cclog.Exit("No errors, server flag not set. Exiting cc-backend.")
|
cclog.Exit("No errors, server flag not set. Exiting cc-backend.")
|
||||||
}
|
}
|
||||||
@@ -466,7 +466,7 @@ func run() error {
|
|||||||
// Run server with context
|
// Run server with context
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return runServer(ctx)
|
return runServer(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,4 +476,3 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,9 +39,7 @@ import (
|
|||||||
httpSwagger "github.com/swaggo/http-swagger"
|
httpSwagger "github.com/swaggo/http-swagger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var buildInfo web.Build
|
||||||
buildInfo web.Build
|
|
||||||
)
|
|
||||||
|
|
||||||
// Environment variable names
|
// Environment variable names
|
||||||
const (
|
const (
|
||||||
@@ -67,15 +65,15 @@ func onFailureResponse(rw http.ResponseWriter, r *http.Request, err error) {
|
|||||||
// NewServer creates and initializes a new Server instance
|
// NewServer creates and initializes a new Server instance
|
||||||
func NewServer(version, commit, buildDate string) (*Server, error) {
|
func NewServer(version, commit, buildDate string) (*Server, error) {
|
||||||
buildInfo = web.Build{Version: version, Hash: commit, Buildtime: buildDate}
|
buildInfo = web.Build{Version: version, Hash: commit, Buildtime: buildDate}
|
||||||
|
|
||||||
s := &Server{
|
s := &Server{
|
||||||
router: mux.NewRouter(),
|
router: mux.NewRouter(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.init(); err != nil {
|
if err := s.init(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +268,6 @@ func (s *Server) init() error {
|
|||||||
handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}),
|
handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}),
|
||||||
handlers.AllowedOrigins([]string{"*"})))
|
handlers.AllowedOrigins([]string{"*"})))
|
||||||
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user