Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config directory, YAML-driven configuration of DB #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ tmp/
bin/

tls/*.pem

# Exclude runtime configuration details from repo
config/*.yml
43 changes: 39 additions & 4 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

_ "github.com/jackc/pgx/v5/stdlib"
"gopkg.in/yaml.v2"
)

type application struct {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions config/development.yml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
postgres:
host: localhost
port: 5332
user: postgres
password: password
dbname: phinvads_dev
6 changes: 6 additions & 0 deletions config/test.yml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
postgres:
host: localhost
port: 5332
user: postgres
password: password
dbname: phinvads_test
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=