mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 16:04:23 +00:00
Centralize env vars loading
This commit is contained in:
@@ -9,18 +9,22 @@ import (
|
|||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
"novamd/internal/api"
|
"novamd/internal/api"
|
||||||
|
"novamd/internal/config"
|
||||||
"novamd/internal/db"
|
"novamd/internal/db"
|
||||||
"novamd/internal/filesystem"
|
"novamd/internal/filesystem"
|
||||||
"novamd/internal/user"
|
"novamd/internal/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize database
|
|
||||||
dbPath := os.Getenv("NOVAMD_DB_PATH")
|
// Load configuration
|
||||||
if dbPath == "" {
|
cfg, err := config.Load()
|
||||||
dbPath = "./sqlite.db"
|
if err != nil {
|
||||||
|
log.Fatal("Failed to load configuration:", err)
|
||||||
}
|
}
|
||||||
database, err := db.Init(dbPath)
|
|
||||||
|
// Initialize database
|
||||||
|
database, err := db.Init(cfg.DBPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -31,21 +35,13 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize filesystem
|
// Initialize filesystem
|
||||||
workdir := os.Getenv("NOVAMD_WORKDIR")
|
fs := filesystem.New(cfg.WorkDir)
|
||||||
if workdir == "" {
|
|
||||||
workdir = "./data"
|
|
||||||
}
|
|
||||||
fs := filesystem.New(workdir)
|
|
||||||
|
|
||||||
// Initialize user service
|
// Initialize user service
|
||||||
userService := user.NewUserService(database, fs)
|
userService := user.NewUserService(database, fs)
|
||||||
|
|
||||||
adminEmail := os.Getenv("NOVAMD_ADMIN_EMAIL")
|
// Create admin user
|
||||||
adminPassword := os.Getenv("NOVAMD_ADMIN_PASSWORD")
|
if _, err := userService.SetupAdminUser(cfg.AdminEmail, cfg.AdminPassword); err != nil {
|
||||||
if adminEmail == "" || adminPassword == "" {
|
|
||||||
log.Fatal("NOVAMD_ADMIN_EMAIL and NOVAMD_ADMIN_PASSWORD environment variables must be set")
|
|
||||||
}
|
|
||||||
if _, err := userService.SetupAdminUser(adminEmail, adminPassword); err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,14 +55,8 @@ func main() {
|
|||||||
api.SetupRoutes(r, database, fs)
|
api.SetupRoutes(r, database, fs)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Static file serving
|
|
||||||
staticPath := os.Getenv("NOVAMD_STATIC_PATH")
|
|
||||||
if staticPath == "" {
|
|
||||||
staticPath = "../frontend/dist"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle all other routes with static file server
|
// Handle all other routes with static file server
|
||||||
r.Get("/*", api.NewStaticHandler(staticPath).ServeHTTP)
|
r.Get("/*", api.NewStaticHandler(cfg.StaticPath).ServeHTTP)
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
port := os.Getenv("NOVAMD_PORT")
|
port := os.Getenv("NOVAMD_PORT")
|
||||||
|
|||||||
67
backend/internal/config/config.go
Normal file
67
backend/internal/config/config.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
DBPath string
|
||||||
|
WorkDir string
|
||||||
|
StaticPath string
|
||||||
|
Port string
|
||||||
|
AdminEmail string
|
||||||
|
AdminPassword string
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
DBPath: "./novamd.db",
|
||||||
|
WorkDir: "./data",
|
||||||
|
StaticPath: "../frontend/dist",
|
||||||
|
Port: "8080",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load creates a new Config instance with values from environment variables
|
||||||
|
func Load() (*Config, error) {
|
||||||
|
config := DefaultConfig()
|
||||||
|
|
||||||
|
if dbPath := os.Getenv("NOVAMD_DB_PATH"); dbPath != "" {
|
||||||
|
config.DBPath = dbPath
|
||||||
|
}
|
||||||
|
if err := ensureDir(filepath.Dir(config.DBPath)); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create database directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if workDir := os.Getenv("NOVAMD_WORKDIR"); workDir != "" {
|
||||||
|
config.WorkDir = workDir
|
||||||
|
}
|
||||||
|
if err := ensureDir(config.WorkDir); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create work directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if staticPath := os.Getenv("NOVAMD_STATIC_PATH"); staticPath != "" {
|
||||||
|
config.StaticPath = staticPath
|
||||||
|
}
|
||||||
|
|
||||||
|
if port := os.Getenv("NOVAMD_PORT"); port != "" {
|
||||||
|
config.Port = port
|
||||||
|
}
|
||||||
|
|
||||||
|
config.AdminEmail = os.Getenv("NOVAMD_ADMIN_EMAIL")
|
||||||
|
config.AdminPassword = os.Getenv("NOVAMD_ADMIN_PASSWORD")
|
||||||
|
if config.AdminEmail == "" || config.AdminPassword == "" {
|
||||||
|
return nil, fmt.Errorf("NOVAMD_ADMIN_EMAIL and NOVAMD_ADMIN_PASSWORD must be set")
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDir(dir string) error {
|
||||||
|
if dir == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return os.MkdirAll(dir, 0755)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user