首次提交
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Env string
|
||||
Host string
|
||||
Port int
|
||||
Database DatabaseConfig
|
||||
Meilisearch MeilisearchConfig
|
||||
Redis RedisConfig
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
port, err := strconv.Atoi(getEnv("APP_PORT", "8080"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid APP_PORT: %w", err)
|
||||
}
|
||||
|
||||
env := getEnv("APP_ENV", "development")
|
||||
|
||||
db, err := loadDatabase(env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := db.validate(env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ms := loadMeilisearch(env)
|
||||
if err := ms.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rd := loadRedis(env)
|
||||
if err := rd.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Env: env,
|
||||
Host: getEnv("APP_HOST", "0.0.0.0"),
|
||||
Port: port,
|
||||
Database: db,
|
||||
Meilisearch: ms,
|
||||
Redis: rd,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Config) Addr() string {
|
||||
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Name string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func loadDatabase(env string) (DatabaseConfig, error) {
|
||||
if os.Getenv("DATABASE_HOST") != "" {
|
||||
return databaseFromEnv()
|
||||
}
|
||||
|
||||
switch env {
|
||||
case "production":
|
||||
return DatabaseConfig{
|
||||
Host: "database-1.chmuueamo72p.eu-central-1.rds.amazonaws.com",
|
||||
Port: 3306,
|
||||
Name: "audio",
|
||||
User: "root",
|
||||
Password: os.Getenv("DATABASE_PASSWORD"),
|
||||
}, nil
|
||||
default:
|
||||
return DatabaseConfig{
|
||||
Host: "localhost",
|
||||
Port: 3306,
|
||||
Name: "audio",
|
||||
User: "root",
|
||||
Password: "root123",
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func databaseFromEnv() (DatabaseConfig, error) {
|
||||
port, err := strconv.Atoi(getEnv("DATABASE_PORT", "3306"))
|
||||
if err != nil {
|
||||
return DatabaseConfig{}, fmt.Errorf("invalid DATABASE_PORT: %w", err)
|
||||
}
|
||||
|
||||
return DatabaseConfig{
|
||||
Host: os.Getenv("DATABASE_HOST"),
|
||||
Port: port,
|
||||
Name: getEnv("DATABASE_NAME", "audio"),
|
||||
User: getEnv("DATABASE_USER", "root"),
|
||||
Password: os.Getenv("DATABASE_PASSWORD"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d DatabaseConfig) validate(env string) error {
|
||||
if d.Host == "" {
|
||||
return fmt.Errorf("database host is required")
|
||||
}
|
||||
if d.Name == "" {
|
||||
return fmt.Errorf("database name is required")
|
||||
}
|
||||
if d.User == "" {
|
||||
return fmt.Errorf("database user is required")
|
||||
}
|
||||
if env == "production" && d.Password == "" {
|
||||
return fmt.Errorf("DATABASE_PASSWORD is required in production")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type MeilisearchConfig struct {
|
||||
Host string
|
||||
APIKey string
|
||||
Index string
|
||||
}
|
||||
|
||||
func loadMeilisearch(env string) MeilisearchConfig {
|
||||
if os.Getenv("MEILISEARCH_HOST") != "" {
|
||||
return MeilisearchConfig{
|
||||
Host: os.Getenv("MEILISEARCH_HOST"),
|
||||
APIKey: os.Getenv("MEILISEARCH_API_KEY"),
|
||||
Index: getEnv("MEILISEARCH_INDEX", "models"),
|
||||
}
|
||||
}
|
||||
|
||||
switch env {
|
||||
case "production":
|
||||
return MeilisearchConfig{
|
||||
Host: "http://ip-172-31-22-170.eu-central-1.compute.internal:7700",
|
||||
APIKey: getEnv("MEILISEARCH_API_KEY", "young9#!UJsD219921031"),
|
||||
Index: "models",
|
||||
}
|
||||
default:
|
||||
return MeilisearchConfig{
|
||||
Host: "http://ec2-18-184-205-87.eu-central-1.compute.amazonaws.com:7700",
|
||||
APIKey: getEnv("MEILISEARCH_API_KEY", "young9#!UJsD219921031"),
|
||||
Index: "models",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m MeilisearchConfig) validate() error {
|
||||
if m.Host == "" {
|
||||
return fmt.Errorf("meilisearch host is required")
|
||||
}
|
||||
if m.APIKey == "" {
|
||||
return fmt.Errorf("meilisearch api key is required")
|
||||
}
|
||||
if m.Index == "" {
|
||||
return fmt.Errorf("meilisearch index is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Password string
|
||||
Database int
|
||||
}
|
||||
|
||||
func loadRedis(env string) RedisConfig {
|
||||
if os.Getenv("REDIS_HOST") != "" {
|
||||
return redisFromEnv()
|
||||
}
|
||||
|
||||
switch env {
|
||||
case "production":
|
||||
return RedisConfig{
|
||||
Host: "172.31.38.162",
|
||||
Port: 16279,
|
||||
Password: getEnv("REDIS_PASSWORD", "eafon123!"),
|
||||
Database: 1,
|
||||
}
|
||||
default:
|
||||
return RedisConfig{
|
||||
Host: "ec2-3-69-138-29.eu-central-1.compute.amazonaws.com",
|
||||
Port: 16279,
|
||||
Password: getEnv("REDIS_PASSWORD", "eafon123!"),
|
||||
Database: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func redisFromEnv() RedisConfig {
|
||||
port, _ := strconv.Atoi(getEnv("REDIS_PORT", "16279"))
|
||||
db, _ := strconv.Atoi(getEnv("REDIS_DATABASE", "1"))
|
||||
|
||||
return RedisConfig{
|
||||
Host: os.Getenv("REDIS_HOST"),
|
||||
Port: port,
|
||||
Password: os.Getenv("REDIS_PASSWORD"),
|
||||
Database: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r RedisConfig) validate() error {
|
||||
if r.Host == "" {
|
||||
return fmt.Errorf("redis host is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user