This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"net/url" | ||
"time" | ||
) | ||
|
||
// ApplicationConfig holds all configurations of the application. | ||
type ApplicationConfig struct { | ||
Union struct { | ||
DBConfig `yaml:"db"` | ||
GRPCConfig `yaml:"grpc"` | ||
MigrationConfig `yaml:"migration"` | ||
MonitoringConfig `yaml:"monitoring"` | ||
ProfilerConfig `yaml:"profiler"` | ||
ServerConfig `yaml:"server"` | ||
} `yaml:"config"` | ||
} | ||
|
||
// DBConfig contains configuration related to database. | ||
type DBConfig struct { | ||
DSN string `yaml:"dsn"` | ||
|
||
dsn *url.URL | ||
} | ||
|
||
// DriverName returns database driver name. | ||
func (cnf *DBConfig) DriverName() string { | ||
if cnf.dsn == nil { | ||
cnf.dsn, _ = url.Parse(cnf.DSN) | ||
} | ||
return cnf.dsn.Scheme | ||
} | ||
|
||
// GRPCConfig contains configuration related to gRPC server. | ||
type GRPCConfig struct { | ||
Interface string `yaml:"interface"` | ||
} | ||
|
||
// MigrationConfig contains configuration related to migrations. | ||
type MigrationConfig struct { | ||
Table string `yaml:"table"` | ||
Schema string `yaml:"schema"` | ||
Limit uint `yaml:"limit"` | ||
DryRun bool `yaml:"dry-run"` | ||
WithDemo bool `yaml:"with-demo"` | ||
} | ||
|
||
// MonitoringConfig contains configuration related to monitoring. | ||
type MonitoringConfig struct { | ||
Enabled bool `yaml:"enabled"` | ||
Interface string `yaml:"interface"` | ||
} | ||
|
||
// ProfilerConfig contains configuration related to profiler. | ||
type ProfilerConfig struct { | ||
Enabled bool `yaml:"enabled"` | ||
Interface string `yaml:"interface"` | ||
} | ||
|
||
// ServerConfig contains configuration related to the Forma server. | ||
type ServerConfig struct { | ||
Interface string `yaml:"interface"` | ||
CPUCount uint `yaml:"cpus"` | ||
ReadTimeout time.Duration `yaml:"read-timeout"` | ||
ReadHeaderTimeout time.Duration `yaml:"read-header-timeout"` | ||
WriteTimeout time.Duration `yaml:"write-timeout"` | ||
IdleTimeout time.Duration `yaml:"idle-timeout"` | ||
BaseURL string `yaml:"base-url"` | ||
TemplateDir string `yaml:"tpl-dir"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package config_test | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/kamilsk/form-api/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var update = flag.Bool("update", false, "update .golden files") | ||
|
||
func TestYAMLSerialization(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
in string | ||
out string | ||
}{ | ||
{"simple configuration", "fixtures/simple.yml", "fixtures/simple.yml.golden"}, | ||
} | ||
|
||
for _, test := range testCases { | ||
tc := test | ||
t.Run(test.name, func(t *testing.T) { | ||
raw, err := ioutil.ReadFile(tc.in) | ||
assert.NoError(t, err) | ||
|
||
var cnf config.ApplicationConfig | ||
err = yaml.UnmarshalStrict(raw, &cnf) | ||
assert.NoError(t, err) | ||
|
||
actual, err := yaml.Marshal(cnf) | ||
assert.NoError(t, err) | ||
|
||
if *update { | ||
err = ioutil.WriteFile(tc.out, actual, os.ModePerm) | ||
assert.NoError(t, err) | ||
} | ||
expected, err := ioutil.ReadFile(tc.out) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
config: | ||
|
||
db: | ||
dsn: postgres://user:pass@localhost:5432/postgres?connect_timeout=10&sslmode=disable | ||
|
||
grpc: | ||
interface: 127.0.0.1:8092 | ||
|
||
migration: | ||
table: migration | ||
schema: public | ||
limit: 0 | ||
dry-run: false | ||
with-demo: false | ||
|
||
monitoring: | ||
enabled: true | ||
interface: 127.0.0.1:8091 | ||
|
||
profiler: | ||
enabled: true | ||
interface: 127.0.0.1:8090 | ||
|
||
server: | ||
interface: 127.0.0.1:8080 | ||
cpus: 1 | ||
read-timeout: 1s | ||
read-header-timeout: 1s | ||
write-timeout: 1s | ||
idle-timeout: 1s | ||
base-url: http://localhost/ | ||
tpl-dir: ~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
config: | ||
db: | ||
dsn: postgres://user:pass@localhost:5432/postgres?connect_timeout=10&sslmode=disable | ||
grpc: | ||
interface: 127.0.0.1:8092 | ||
migration: | ||
table: migration | ||
schema: public | ||
limit: 0 | ||
dry-run: false | ||
with-demo: false | ||
monitoring: | ||
enabled: true | ||
interface: 127.0.0.1:8091 | ||
profiler: | ||
enabled: true | ||
interface: 127.0.0.1:8090 | ||
server: | ||
interface: 127.0.0.1:8080 | ||
cpus: 1 | ||
read-timeout: 1s | ||
read-header-timeout: 1s | ||
write-timeout: 1s | ||
idle-timeout: 1s | ||
base-url: http://localhost/ | ||
tpl-dir: "" |