Init Eslogad
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
_ "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
YAML_PATH = "infra/%s"
|
||||
ENV_MODE = "ENV_MODE"
|
||||
DEFAULT_ENV_MODE = "development"
|
||||
)
|
||||
|
||||
var (
|
||||
validEnvMode = map[string]struct{}{
|
||||
"local": {},
|
||||
"development": {},
|
||||
"production": {},
|
||||
}
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server Server `mapstructure:"server"`
|
||||
Database Database `mapstructure:"postgresql"`
|
||||
Jwt Jwt `mapstructure:"jwt"`
|
||||
Log Log `mapstructure:"log"`
|
||||
S3Config S3Config `mapstructure:"s3"`
|
||||
}
|
||||
|
||||
var (
|
||||
config *Config
|
||||
configOnce sync.Once
|
||||
)
|
||||
|
||||
func LoadConfig() *Config {
|
||||
envMode := os.Getenv(ENV_MODE)
|
||||
if _, ok := validEnvMode[envMode]; !ok {
|
||||
envMode = DEFAULT_ENV_MODE
|
||||
}
|
||||
cfgFilePath := fmt.Sprintf(YAML_PATH, envMode)
|
||||
|
||||
configOnce.Do(func() {
|
||||
v := viper.New()
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath(".")
|
||||
v.SetConfigName(cfgFilePath)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
panic(fmt.Errorf("failed to read config file: %s", err))
|
||||
}
|
||||
|
||||
config = &Config{}
|
||||
if err := v.Unmarshal(config); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal config: %s", err))
|
||||
}
|
||||
})
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *Config) Auth() *AuthConfig {
|
||||
return &AuthConfig{
|
||||
jwtTokenSecret: c.Jwt.Token.Secret,
|
||||
jwtTokenExpiresTTL: c.Jwt.Token.ExpiresTTL,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) LogLevel() string {
|
||||
return c.Log.LogLevel
|
||||
}
|
||||
|
||||
func (c *Config) Port() string {
|
||||
return c.Server.Port
|
||||
}
|
||||
|
||||
func (c *Config) LogFormat() string {
|
||||
return c.Log.LogFormat
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type AuthConfig struct {
|
||||
jwtTokenExpiresTTL int
|
||||
jwtTokenSecret string
|
||||
}
|
||||
|
||||
type JWT struct {
|
||||
secret string
|
||||
expireTTL int
|
||||
}
|
||||
|
||||
func (c *AuthConfig) AccessTokenSecret() string {
|
||||
return c.jwtTokenSecret
|
||||
}
|
||||
|
||||
func (c *AuthConfig) AccessTokenExpiresDate() time.Time {
|
||||
duration := time.Duration(c.jwtTokenExpiresTTL)
|
||||
return time.Now().UTC().Add(time.Minute * duration)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port string `mapstructure:"port"`
|
||||
DB string `mapstructure:"db"`
|
||||
Driver string `mapstructure:"driver"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
SslMode string `mapstructure:"ssl-mode"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
MaxIdleConnectionsInSecond int `mapstructure:"max-idle-connections-in-second"`
|
||||
MaxOpenConnectionsInSecond int `mapstructure:"max-open-connections-in-second"`
|
||||
ConnectionMaxLifetimeInSecond int64 `mapstructure:"connection-max-life-time-in-second"`
|
||||
}
|
||||
|
||||
func (c Database) DSN() string {
|
||||
return fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s TimeZone=Asia/Jakarta", c.Host, c.Port, c.DB, c.Username, c.Password, c.SslMode)
|
||||
}
|
||||
|
||||
func (c Database) ConnectionMaxLifetime() time.Duration {
|
||||
return time.Duration(c.ConnectionMaxLifetimeInSecond) * time.Second
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
type httpConfig struct {
|
||||
host string
|
||||
port int
|
||||
detailedError bool
|
||||
}
|
||||
|
||||
func (c *httpConfig) Address() string {
|
||||
return fmt.Sprintf("%s:%d", c.host, c.port)
|
||||
}
|
||||
|
||||
func (c *httpConfig) DetailedError() bool {
|
||||
return c.detailedError
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
type Jwt struct {
|
||||
Token Token `mapstructure:"token"`
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
ExpiresTTL int `mapstructure:"expires-ttl"`
|
||||
Secret string `mapstructure:"secret"`
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package config
|
||||
|
||||
type Log struct {
|
||||
LogFormat string `mapstructure:"log_format"`
|
||||
LogLevel string `mapstructure:"log_level"`
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package config
|
||||
|
||||
type S3Config struct {
|
||||
AccessKeyID string `mapstructure:"access_key_id"`
|
||||
AccessKeySecret string `mapstructure:"access_key_secret"`
|
||||
Endpoint string `mapstructure:"endpoint"`
|
||||
BucketName string `mapstructure:"bucket_name"`
|
||||
PhotoFolder string `mapstructure:"photo_folder"`
|
||||
LogLevel string `mapstructure:"log_level"`
|
||||
HostURL string `mapstructure:"host_url"`
|
||||
}
|
||||
|
||||
func (c S3Config) GetAccessKeyID() string {
|
||||
return c.AccessKeyID
|
||||
}
|
||||
|
||||
func (c S3Config) GetAccessKeySecret() string {
|
||||
return c.AccessKeySecret
|
||||
}
|
||||
|
||||
func (c S3Config) GetEndpoint() string {
|
||||
return c.Endpoint
|
||||
}
|
||||
|
||||
func (c S3Config) GetBucketName() string {
|
||||
return c.BucketName
|
||||
}
|
||||
|
||||
func (c S3Config) GetLogLevel() string {
|
||||
return c.LogLevel
|
||||
}
|
||||
|
||||
func (c S3Config) GetHostURL() string {
|
||||
return c.HostURL
|
||||
}
|
||||
|
||||
func (c S3Config) GetPhotoFolder() string {
|
||||
return c.PhotoFolder
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type Server struct {
|
||||
Port string `mapstructure:"port"`
|
||||
BaseUrl string `mapstructure:"common-url"`
|
||||
LocalUrl string `mapstructure:"local-url"`
|
||||
}
|
||||
Reference in New Issue
Block a user