Skip to content

Commit

Permalink
Merge pull request #59 from fabiante/feat/refactor-env-config
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante authored Sep 19, 2023
2 parents e255c90 + 4903911 commit 68b9d8d
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 63 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# devtools
.idea

# config files
/.env
/app.*

# other
*.sqlite
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Application to manage and resolve [PURL](https://en.wikipedia.org/wiki/Persistent_uniform_resource_locator) links.

## Usage

### Configuration

The application can be configured using:

- env variables, prefixed with `PERSURL_` (example: `PERSURL_DB_DSN`)
- config files (example: `app.yml`)

Have a look at `example.config.yml` for an example configuration.

## Documentation

Until the documentation becomes large enough, this README will be used to
Expand Down Expand Up @@ -32,7 +43,7 @@ test specifications ensure correct behaviour. Test drivers execute test specific

#### Load Tests

Load tests can enabled via the env variable `TEST_LOAD=1`.
Load tests can enabled via the env variable `TEST_LOAD=1` or setting `test_load` to `1` in your config file.

These run the application and generate load by running multiple agents simulating user behaviour.
The motivation of these tests is to ensure that the application can be used for a large user base which
Expand Down
48 changes: 7 additions & 41 deletions config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,16 @@ package config

import (
"errors"
"fmt"
"log"
"os"
"strconv"

"github.com/joho/godotenv"
"github.com/spf13/viper"
)

func LoadEnv() {
path := ".env"

// check if .env file exists - if not, exit early.
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return
}

err = godotenv.Load(path)
if err != nil {
panic(fmt.Errorf("loading env failed: %w", err))
}
}

func DbDSN() string {
dsn := os.Getenv("PERSURL_DB_DSN")
if dsn == "" {
dsn = os.Getenv("DATABASE_URL")
}
if dsn == "" {
log.Fatalf("persurl db dsn may not be empty")
}
return dsn
}

func DbMaxConnections() int {
val := os.Getenv("PERSURL_DB_MAX_CONNECTIONS")
if val == "" {
val = "10"
}

maxCon, err := strconv.ParseInt(val, 10, 32)
if err != nil {
log.Fatalf("invalid db max connection parameter %s", val)
func setupEnv(v *viper.Viper) error {
errs := []error{
v.BindEnv("test_load", "TEST_LOAD"),
v.BindEnv("db.dsn", "PERSURL_DB_DSN", "DATABASE_URL"),
v.BindEnv("db.max_connections", "PERSURL_DB_MAX_CONNECTIONS"),
}

return int(maxCon)
return errors.Join(errs...)
}
23 changes: 23 additions & 0 deletions config/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"errors"
)

func DbDSN() string {
dsn := vip.GetString("db.dsn")

if dsn == "" {
panic(errors.New("db dsn may not be empty"))
}

return dsn
}

func DbMaxConnections() int {
return vip.GetInt("db.max_connections")
}

func TestLoad() bool {
return vip.IsSet("test_load")
}
44 changes: 44 additions & 0 deletions config/viper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"errors"

"github.com/spf13/viper"
)

var vip *viper.Viper

func init() {
vip = setupViper()
}

func setupViper() *viper.Viper {
v := viper.New()

// loading
v.AddConfigPath(".")
v.SetConfigName("app")

// helper to panic on any error
check := func(e error) {
if e != nil {
panic(e)
}
}

// defaults
v.SetDefault("db.max_connections", 10)

// env binding
check(setupEnv(v))

// trigger config parsing - optional
if err := v.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if !errors.As(err, &configFileNotFoundError) {
check(err)
}
}

return v
}
8 changes: 8 additions & 0 deletions example.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration - copy as app.yml and modify

# Enable this if you want to run load tests
# test_load: true

db:
dsn: postgresql://persurl:persurl@localhost:5432/persurl?sslmode=disable
max_connections: 10
8 changes: 0 additions & 8 deletions example.env

This file was deleted.

12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,41 @@ go 1.21
require (
github.com/doug-martin/goqu/v9 v9.18.0
github.com/gin-gonic/gin v1.9.1
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.1
github.com/lopezator/migrator v0.3.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
Expand All @@ -41,5 +48,6 @@ require (
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 68b9d8d

Please sign in to comment.