mirror of
https://github.com/Dvorinka/Trackeep.git
synced 2026-06-03 20:12:58 +00:00
6c448b336a
This commit implements a unified Docker deployment strategy, moving from separate frontend and backend images to a single, multi-stage build image containing both services. It also introduces a major reorganization of the frontend directory structure and simplifies the environment configuration. Key changes: - **Deployment**: Added a multi-stage `Dockerfile` and `docker-entrypoint.sh` to package the Go backend and Nginx-served frontend into a single container. - **CI/CD**: Updated GitHub Actions workflows (`ci-cd.yml`, `release.yml`) to build and push the new unified image instead of separate ones. - **Frontend Refactor**: Reorganized `frontend/src/pages` into a domain-driven directory structure (e.g., `auth/`, `admin/`, `content/`, `communication/`, `productivity/`, `settings/`, `misc/`). - **Configuration**: Simplified `.env.example` and updated `docker-compose.yml` to reflect the unified service model and single host port. - **Cleanup**: Removed deprecated `docker-compose.demo.yml`, `docker-compose.prod.yml`, and various unused frontend components and services. - **Backend**: Refactored configuration loading to use exported `GetDurationEnv` for better consistency.
119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
IdleTimeout time.Duration
|
|
ShutdownTimeout time.Duration
|
|
}
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
App AppConfig
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Host string
|
|
Port string
|
|
User string
|
|
Password string
|
|
Name string
|
|
SSLMode string
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Version string
|
|
DemoMode bool
|
|
GinMode string
|
|
JWTSecret string
|
|
CorsOrigins string
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnvWithDefault("PORT", getEnvWithDefault("BACKEND_PORT", "8080")),
|
|
ReadTimeout: GetDurationEnv("READ_TIMEOUT", 15*time.Second),
|
|
WriteTimeout: GetDurationEnv("WRITE_TIMEOUT", 15*time.Second),
|
|
IdleTimeout: GetDurationEnv("IDLE_TIMEOUT", 60*time.Second),
|
|
ShutdownTimeout: GetDurationEnv("SHUTDOWN_TIMEOUT", 30*time.Second),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Host: getEnvWithDefault("DB_HOST", "localhost"),
|
|
Port: getEnvWithDefault("DB_PORT", "5432"),
|
|
User: getEnvWithDefault("DB_USER", "trackeep"),
|
|
Password: os.Getenv("DB_PASSWORD"),
|
|
Name: getEnvWithDefault("DB_NAME", "trackeep"),
|
|
SSLMode: getEnvWithDefault("DB_SSL_MODE", "disable"),
|
|
},
|
|
App: AppConfig{
|
|
Version: getEnvWithDefault("APP_VERSION", "1.0.0"),
|
|
DemoMode: os.Getenv("VITE_DEMO_MODE") == "true",
|
|
GinMode: getEnvWithDefault("GIN_MODE", "debug"),
|
|
JWTSecret: os.Getenv("JWT_SECRET"),
|
|
CorsOrigins: getEnvWithDefault("CORS_ALLOWED_ORIGINS", ""),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Config) Validate() error {
|
|
if c.Database.Password == "" && !c.App.DemoMode {
|
|
return fmt.Errorf("DB_PASSWORD environment variable is required")
|
|
}
|
|
|
|
if c.App.GinMode == "release" && c.App.CorsOrigins == "" {
|
|
return fmt.Errorf("CORS_ALLOWED_ORIGINS must be set in production mode")
|
|
}
|
|
|
|
if c.App.GinMode == "release" && c.App.JWTSecret == "" {
|
|
return fmt.Errorf("JWT_SECRET must be set in production mode")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) DSN() string {
|
|
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
|
|
c.Database.Host,
|
|
c.Database.User,
|
|
c.Database.Password,
|
|
c.Database.Name,
|
|
c.Database.Port,
|
|
c.Database.SSLMode,
|
|
)
|
|
}
|
|
|
|
func getEnvWithDefault(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func GetDurationEnv(key string, defaultValue time.Duration) time.Duration {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return defaultValue
|
|
}
|
|
|
|
seconds, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
duration, err := time.ParseDuration(value)
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return duration
|
|
}
|
|
|
|
return time.Duration(seconds) * time.Second
|
|
}
|