Skip to content

Commit 4fd47b4

Browse files
AndresCidoncharaphink
authored andcommitted
feat: Support read config from file
feat: Add base_url as config parameter refactor: Move S3/DynamoDB configs in AWS Config block doc: add example configuration file fix: fix auth module tests fix: remove debug print
1 parent c9152a9 commit 4fd47b4

File tree

7 files changed

+89
-33
lines changed

7 files changed

+89
-33
lines changed

auth/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type User struct {
1818

1919
// Setup sets up authentication
2020
func Setup(c *config.Config) {
21-
logoutURL = c.Authentication.LogoutURL
21+
logoutURL = c.Web.LogoutURL
2222
}
2323

2424
// UserInfo returns a User given a name and email

auth/auth_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestSetup_simple(t *testing.T) {
1111
expected := "/log/me/out"
1212

1313
c := config.Config{}
14-
c.Authentication.LogoutURL = expected
14+
c.Web.LogoutURL = expected
1515

1616
Setup(&c)
1717

config/config.go

+54-21
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,65 @@ package config
33
import (
44
"errors"
55
"fmt"
6+
"io/ioutil"
67
"os"
78

89
"github.com/jessevdk/go-flags"
910
log "github.com/sirupsen/logrus"
11+
"gopkg.in/yaml.v2"
1012
)
1113

1214
// Config stores the handler's configuration and UI interface parameters
1315
type Config struct {
1416
Version bool `short:"V" long:"version" description:"Display version."`
1517

16-
Port int `short:"p" long:"port" description:"Port to listen on." default:"8080"`
18+
Port int `short:"p" long:"port" yaml:"port" description:"Port to listen on." default:"8080"`
19+
20+
ConfigFilePath string `long:"config-file" env:"CONFIG_FILE" description:"Config File path" default:""`
1721

1822
Log struct {
19-
Level string `short:"l" long:"log-level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." env:"TERRABOARD_LOG_LEVEL" default:"info"`
20-
Format string `long:"log-format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
21-
} `group:"Logging Options"`
23+
Level string `short:"l" long:"log-level" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." env:"TERRABOARD_LOG_LEVEL" default:"info"`
24+
Format string `long:"log-format" yaml:"format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
25+
} `group:"Logging Options" yaml:"log"`
2226

2327
DB struct {
24-
Host string `long:"db-host" env:"DB_HOST" description:"Database host." default:"db"`
25-
User string `long:"db-user" env:"DB_USER" description:"Database user." default:"gorm"`
26-
Password string `long:"db-password" env:"DB_PASSWORD" description:"Database password."`
27-
Name string `long:"db-name" env:"DB_NAME" description:"Database name." default:"gorm"`
28-
NoSync bool `long:"no-sync" description:"Do not sync database."`
29-
} `group:"Database Options"`
30-
31-
S3 struct {
32-
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" description:"AWS S3 bucket."`
33-
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" description:"AWS DynamoDB table for locks."`
34-
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" description:"AWS Key Prefix."`
35-
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" description:"File extension of state files." default:".tfstate"`
36-
} `group:"AWS S3 Options"`
37-
38-
Authentication struct {
39-
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" description:"Logout URL."`
40-
} `group:"Authentication"`
28+
Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"`
29+
User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"`
30+
Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."`
31+
Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"`
32+
NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."`
33+
} `group:"Database Options" yaml:"database"`
34+
35+
AWS struct {
36+
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."`
37+
38+
S3 struct {
39+
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."`
40+
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."`
41+
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"`
42+
} `group:"S3 Options" yaml:"s3"`
43+
} `group:"AWS Options" yaml:"aws"`
44+
45+
Web struct {
46+
BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"`
47+
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."`
48+
} `group:"Web" yaml:"web"`
49+
}
50+
51+
// LoadConfigFromYaml loads the config from config file
52+
func (c *Config) LoadConfigFromYaml(configFilePath string) *Config {
53+
fmt.Printf("Loading config from %s\n", c.ConfigFilePath)
54+
yamlFile, err := ioutil.ReadFile(configFilePath)
55+
if err != nil {
56+
log.Printf("yamlFile.Get err #%v ", err)
57+
}
58+
59+
err = yaml.Unmarshal(yamlFile, c)
60+
if err != nil {
61+
log.Fatalf("Unmarshal err: %v", err)
62+
}
63+
64+
return c
4165
}
4266

4367
// LoadConfig loads the config from flags & environment
@@ -48,6 +72,15 @@ func LoadConfig(version string) *Config {
4872
os.Exit(1)
4973
}
5074

75+
if c.ConfigFilePath != "" {
76+
if _, err := os.Stat(c.ConfigFilePath); err == nil {
77+
c.LoadConfigFromYaml(c.ConfigFilePath)
78+
} else {
79+
fmt.Printf("File %s doesn't exists!\n", c.ConfigFilePath)
80+
os.Exit(1)
81+
}
82+
}
83+
5184
if c.Version {
5285
fmt.Printf("Terraboard v%v\n", version)
5386
os.Exit(0)

example.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
port: 9090
2+
3+
log:
4+
level: info
5+
format: plain
6+
7+
database:
8+
host: 127.0.0.1
9+
port: 5432
10+
user: terraboard
11+
password: terraboard
12+
name: terraboard
13+
no-sync: true
14+
15+
aws:
16+
dynamodb-table: terraboard
17+
s3:
18+
bucket: terraboard
19+
key-prefix:
20+
file-extension: .tfstate
21+
22+
web:
23+
base-url: /terraboard/
24+
logout-url: /test

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ func getVersion(w http.ResponseWriter, r *http.Request) {
129129
func main() {
130130
c := config.LoadConfig(version)
131131

132+
util.UpdateBase(c.Web.BaseURL)
133+
132134
log.Infof("Terraboard v%s is starting...", version)
133135

134136
err := c.SetupLogging()

s3/s3.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ var fileExtension string
2828
func Setup(c *config.Config) {
2929
sess := session.Must(session.NewSession())
3030
svc = s3.New(sess, &aws.Config{})
31-
bucket = c.S3.Bucket
32-
keyPrefix = c.S3.KeyPrefix
33-
fileExtension = c.S3.FileExtension
31+
bucket = c.AWS.S3.Bucket
32+
keyPrefix = c.AWS.S3.KeyPrefix
33+
fileExtension = c.AWS.S3.FileExtension
3434

3535
dynamoSvc = dynamodb.New(sess, &aws.Config{})
36-
dynamoTable = c.S3.DynamoDBTable
36+
dynamoTable = c.AWS.DynamoDBTable
3737
}
3838

3939
// LockInfo stores information on a State Lock

util/util.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ package util
33
import (
44
"fmt"
55
"net/http"
6-
"os"
76
"strings"
87
)
98

109
var baseURL string
1110

12-
func init() {
13-
baseURL = os.Getenv("BASE_URL")
14-
if baseURL == "" {
15-
baseURL = "/"
16-
}
11+
// UpdateBase replaces baseURL with a new one
12+
func UpdateBase(new string) {
13+
baseURL = new
1714
}
1815

1916
// ReplaceBase replaces a pattern in a string, injecting baseURL into it

0 commit comments

Comments
 (0)