init: first init
This commit is contained in:
37
internal/config/database.go
Normal file
37
internal/config/database.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"blog/internal/config/env"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Name string
|
||||
User string
|
||||
Password string
|
||||
SSLMode string
|
||||
TimeZone string
|
||||
}
|
||||
|
||||
func NewDatabaseConfig() *DatabaseConfig {
|
||||
return &DatabaseConfig{}
|
||||
}
|
||||
|
||||
func (c *DatabaseConfig) LoadConfig() {
|
||||
c.Host = env.GetString("DB_HOST", "localhost")
|
||||
c.Port = env.GetInt("DB_PORT", 5432)
|
||||
c.Name = env.GetString("DB_NAME", "blog")
|
||||
c.User = env.GetString("DB_USER", "blog")
|
||||
c.Password = env.GetString("DB_PASSWORD", "blog")
|
||||
c.SSLMode = env.GetString("DB_SSL_MODE", "disable")
|
||||
c.TimeZone = env.GetString("DB_TIME_ZONE", "America/New_York")
|
||||
|
||||
log.Info("Successfully loaded database config")
|
||||
}
|
||||
|
||||
func (c *DatabaseConfig) GetDSN() string {
|
||||
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", c.Host, c.User, c.Password, c.Name, c.Port, c.SSLMode, c.TimeZone)
|
||||
}
|
||||
48
internal/config/env/env.go
vendored
Normal file
48
internal/config/env/env.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetString returns the string value from an environment variable
|
||||
func GetString(name, defaultValue string) string {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// GetInt returns the int value from an environment variable
|
||||
func GetInt(name string, defaultValue int) int {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// Convert string to int
|
||||
intValue, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return intValue
|
||||
}
|
||||
|
||||
// GetBool returns the bool value from an environment variable
|
||||
func GetBool(name string, defaultValue bool) bool {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// Convert string to bool
|
||||
boolValue, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return boolValue
|
||||
}
|
||||
2
internal/config/frontend.go
Normal file
2
internal/config/frontend.go
Normal file
@@ -0,0 +1,2 @@
|
||||
package config
|
||||
|
||||
40
internal/config/server.go
Normal file
40
internal/config/server.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"blog/internal/config/env"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
StrapiHost string
|
||||
RedisHost string
|
||||
RedisPort int
|
||||
StrapiApiKey string
|
||||
CacheTTL int
|
||||
EchoMode string
|
||||
}
|
||||
|
||||
func NewServerConfig() *ServerConfig {
|
||||
return &ServerConfig{}
|
||||
}
|
||||
|
||||
func (c *ServerConfig) LoadConfig() {
|
||||
c.Host = env.GetString("HOST", "0.0.0.0")
|
||||
c.Port = env.GetInt("PORT", 3000)
|
||||
c.StrapiHost = env.GetString("STRAPI_HOST", "https://strapi.darwincereska.dev")
|
||||
c.StrapiApiKey = env.GetString("STRAPI_API_KEY", "")
|
||||
c.RedisHost = env.GetString("REDIS_HOST", "localhost")
|
||||
c.RedisPort = env.GetInt("REDIS_PORT", 6379)
|
||||
c.CacheTTL = env.GetInt("CACHE_TTL", 3600)
|
||||
c.EchoMode = env.GetString("ECHO_MODE", "release")
|
||||
|
||||
log.Info("Sucessfully loaded server config")
|
||||
log.Info("Host", "host", c.Host)
|
||||
log.Info("Port", "port", c.Port)
|
||||
log.Info("Redis Host", "host", c.RedisHost)
|
||||
log.Info("Strapi URL", "host", c.StrapiHost)
|
||||
log.Info("Echo Mode", "mode", c.EchoMode)
|
||||
log.Info("Cache TTL", "ttl", c.CacheTTL)
|
||||
}
|
||||
Reference in New Issue
Block a user