diff --git a/.gitignore b/.gitignore index ac93ed2..ff00a30 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ tmp/ bin/ tls/*.pem + +# Exclude runtime configuration details from repo +config/*.yml \ No newline at end of file diff --git a/cmd/web/main.go b/cmd/web/main.go index 5611e85..f0f5adf 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -11,6 +11,7 @@ import ( "time" _ "github.com/jackc/pgx/v5/stdlib" + "gopkg.in/yaml.v2" ) type application struct { @@ -19,13 +20,17 @@ type application struct { } func main() { + env := getEnvironment() + config := readConfig(env) + addr := flag.String("addr", "localhost:4000", "HTTP network address") - dsn := flag.String("dsn", "postgresql://localhost:5432/phinvads", "PostgreSQL data source name") + //dsn := flag.String("dsn", "postgresql://localhost:5432/phinvads", "PostgreSQL data source name") flag.Parse() logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) - db, err := openDB(*dsn) + db, err := openDB(config["postgres"]) + if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) os.Exit(1) @@ -55,8 +60,38 @@ func main() { os.Exit(1) } -func openDB(dsn string) (*sql.DB, error) { - db, err := sql.Open("pgx", dsn) +const DEFAULT_ENV = "development" + +func getEnvironment() string { + e := os.Getenv("PHINVADS_GO_ENV") + if len(e) == 0 { + return DEFAULT_ENV + } else { + return e + } +} + +func readConfig(environment string) map[string]map[string]interface{} { + config := make(map[string]map[string]interface{}) + + yamlFile, err := os.ReadFile(fmt.Sprintf("config/%s.yml", environment)) + if err != nil { + fmt.Printf("Error reading config file for environment '%s': #%v ", env, err) + } + + err = yaml.Unmarshal(yamlFile, config) + if err != nil { + fmt.Printf("Unmarshal: %v", err) + } + + return config +} + +func openDB(config map[string]interface{}) (*sql.DB, error) { + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config["host"], config["port"], config["user"], config["password"], config["dbname"]) + flag.String("dsn", psqlInfo, "PostgreSQL data source name") + + db, err := sql.Open("postgres", psqlInfo) if err != nil { return nil, err } diff --git a/config/development.yml.template b/config/development.yml.template new file mode 100644 index 0000000..9f92afb --- /dev/null +++ b/config/development.yml.template @@ -0,0 +1,6 @@ +postgres: + host: localhost + port: 5332 + user: postgres + password: password + dbname: phinvads_dev diff --git a/config/test.yml.template b/config/test.yml.template new file mode 100644 index 0000000..69ed2de --- /dev/null +++ b/config/test.yml.template @@ -0,0 +1,6 @@ +postgres: + host: localhost + port: 5332 + user: postgres + password: password + dbname: phinvads_test diff --git a/go.mod b/go.mod index a4598b6..a31cab2 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,5 @@ require ( golang.org/x/crypto v0.17.0 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 4ba72d1..6786bc3 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=