annotate config/config.go @ 143:abfac07bd82a vue-gettext

closing branch vue-gettext
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 02 Jul 2018 09:37:53 +0200
parents a98a282f00e1
children 5ca6f436d0b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package config
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
125
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
3 import (
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
4 "log"
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
5 "os"
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
6 "strconv"
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
7 )
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
8
28
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 var Config = NewConfiguration()
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 type Configuration struct {
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 DBHost string
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 DBPort uint
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 DBName string
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 DBSSLMode string
119
29e56c342c9f Added first middleware for JWT token extraction. TODO: Add second one to check against logged in users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 28
diff changeset
16
29e56c342c9f Added first middleware for JWT token extraction. TODO: Add second one to check against logged in users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 28
diff changeset
17 JWTSignKey []byte
28
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 }
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 func NewConfiguration() *Configuration {
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 // TODO: Load from file.
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 return &Configuration{
125
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
23 DBHost: envString("GEMMA_DB_HOST", "localhost"),
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
24 DBPort: envUint("GEMMA_DB_PORT", 5432),
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
25 DBName: envString("GEMMA_DB_NAME", "gemma"),
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
26 DBSSLMode: envString("GEMMA_DB_SSL_MODE", "require"),
119
29e56c342c9f Added first middleware for JWT token extraction. TODO: Add second one to check against logged in users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 28
diff changeset
27
125
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
28 JWTSignKey: []byte(
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
29 envString("GEMMA_JWT_SIGN_KEY", "SECRET! CHANGE ME!")),
28
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 }
714787accd26 Fetch database connection string parts from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 }
125
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
32
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
33 func envString(key, def string) string {
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
34 if v, ok := os.LookupEnv(key); ok {
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
35 return v
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
36 }
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
37 return def
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
38 }
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
39
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
40 func envUint(key string, def uint) uint {
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
41 if v, ok := os.LookupEnv(key); ok {
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
42 x, err := strconv.ParseUint(v, 10, 64)
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
43 if err != nil {
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
44 log.Printf("warn: invalid uint env %s: %v\n", key, err)
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
45 return def
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
46 }
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
47 return uint(x)
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
48 }
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
49 return def
a98a282f00e1 Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 119
diff changeset
50 }