github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/app/config.go (about)

     1  package app
     2  
     3  import (
     4  	"github.com/ShoshinNikita/budget-manager/internal/db"
     5  	"github.com/ShoshinNikita/budget-manager/internal/db/pg"
     6  	"github.com/ShoshinNikita/budget-manager/internal/db/sqlite"
     7  	"github.com/ShoshinNikita/budget-manager/internal/logger"
     8  	"github.com/ShoshinNikita/budget-manager/internal/pkg/env"
     9  	"github.com/ShoshinNikita/budget-manager/internal/web"
    10  )
    11  
    12  type Config struct {
    13  	Logger logger.Config
    14  	DB     DBConfig
    15  	Server web.Config
    16  }
    17  
    18  type DBConfig struct {
    19  	Type     db.Type
    20  	Postgres pg.Config
    21  	SQLite   sqlite.Config
    22  }
    23  
    24  func ParseConfig() (Config, error) {
    25  	cfg := Config{
    26  		Logger: logger.Config{
    27  			Mode:  "prod",
    28  			Level: "info",
    29  		},
    30  		DB: DBConfig{
    31  			Type: db.Postgres,
    32  			Postgres: pg.Config{
    33  				Host:     "localhost",
    34  				Port:     5432,
    35  				User:     "postgres",
    36  				Password: "",
    37  				Database: "postgres",
    38  			},
    39  			SQLite: sqlite.Config{
    40  				Path: "./var/budget-manager.db",
    41  			},
    42  		},
    43  		//
    44  		Server: web.Config{
    45  			Port:            8080,
    46  			UseEmbed:        true,
    47  			EnableProfiling: false,
    48  			Auth: web.AuthConfig{
    49  				Disable:        false,
    50  				BasicAuthCreds: nil,
    51  			},
    52  		},
    53  	}
    54  
    55  	for _, v := range []struct {
    56  		key    string
    57  		target interface{}
    58  	}{
    59  		{"LOGGER_MODE", &cfg.Logger.Mode},
    60  		{"LOGGER_LEVEL", &cfg.Logger.Level},
    61  		//
    62  		{"DB_TYPE", &cfg.DB.Type},
    63  		{"DB_PG_HOST", &cfg.DB.Postgres.Host},
    64  		{"DB_PG_PORT", &cfg.DB.Postgres.Port},
    65  		{"DB_PG_USER", &cfg.DB.Postgres.User},
    66  		{"DB_PG_PASSWORD", &cfg.DB.Postgres.Password},
    67  		{"DB_PG_DATABASE", &cfg.DB.Postgres.Database},
    68  		{"DB_SQLITE_PATH", &cfg.DB.SQLite.Path},
    69  		//
    70  		{"SERVER_PORT", &cfg.Server.Port},
    71  		{"SERVER_USE_EMBED", &cfg.Server.UseEmbed},
    72  		{"SERVER_ENABLE_PROFILING", &cfg.Server.EnableProfiling},
    73  		{"SERVER_AUTH_DISABLE", &cfg.Server.Auth.Disable},
    74  		{"SERVER_AUTH_BASIC_CREDS", &cfg.Server.Auth.BasicAuthCreds},
    75  	} {
    76  		if err := env.Load(v.key, v.target); err != nil {
    77  			return Config{}, err
    78  		}
    79  	}
    80  	return cfg, nil
    81  }