Skip to content

Commit

Permalink
feat: Support read config from file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AndresCidoncha authored and raphink committed Oct 29, 2019
1 parent c9152a9 commit 4fd47b4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type User struct {

// Setup sets up authentication
func Setup(c *config.Config) {
logoutURL = c.Authentication.LogoutURL
logoutURL = c.Web.LogoutURL
}

// UserInfo returns a User given a name and email
Expand Down
2 changes: 1 addition & 1 deletion auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestSetup_simple(t *testing.T) {
expected := "/log/me/out"

c := config.Config{}
c.Authentication.LogoutURL = expected
c.Web.LogoutURL = expected

Setup(&c)

Expand Down
75 changes: 54 additions & 21 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,65 @@ package config
import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

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

Port int `short:"p" long:"port" description:"Port to listen on." default:"8080"`
Port int `short:"p" long:"port" yaml:"port" description:"Port to listen on." default:"8080"`

ConfigFilePath string `long:"config-file" env:"CONFIG_FILE" description:"Config File path" default:""`

Log struct {
Level string `short:"l" long:"log-level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." env:"TERRABOARD_LOG_LEVEL" default:"info"`
Format string `long:"log-format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
} `group:"Logging Options"`
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"`
Format string `long:"log-format" yaml:"format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
} `group:"Logging Options" yaml:"log"`

DB struct {
Host string `long:"db-host" env:"DB_HOST" description:"Database host." default:"db"`
User string `long:"db-user" env:"DB_USER" description:"Database user." default:"gorm"`
Password string `long:"db-password" env:"DB_PASSWORD" description:"Database password."`
Name string `long:"db-name" env:"DB_NAME" description:"Database name." default:"gorm"`
NoSync bool `long:"no-sync" description:"Do not sync database."`
} `group:"Database Options"`

S3 struct {
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" description:"AWS S3 bucket."`
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" description:"AWS DynamoDB table for locks."`
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" description:"AWS Key Prefix."`
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" description:"File extension of state files." default:".tfstate"`
} `group:"AWS S3 Options"`

Authentication struct {
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" description:"Logout URL."`
} `group:"Authentication"`
Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"`
User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"`
Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."`
Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"`
NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."`
} `group:"Database Options" yaml:"database"`

AWS struct {
DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."`

S3 struct {
Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."`
KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."`
FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"`
} `group:"S3 Options" yaml:"s3"`
} `group:"AWS Options" yaml:"aws"`

Web struct {
BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"`
LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."`
} `group:"Web" yaml:"web"`
}

// LoadConfigFromYaml loads the config from config file
func (c *Config) LoadConfigFromYaml(configFilePath string) *Config {
fmt.Printf("Loading config from %s\n", c.ConfigFilePath)
yamlFile, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}

err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal err: %v", err)
}

return c
}

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

if c.ConfigFilePath != "" {
if _, err := os.Stat(c.ConfigFilePath); err == nil {
c.LoadConfigFromYaml(c.ConfigFilePath)
} else {
fmt.Printf("File %s doesn't exists!\n", c.ConfigFilePath)
os.Exit(1)
}
}

if c.Version {
fmt.Printf("Terraboard v%v\n", version)
os.Exit(0)
Expand Down
24 changes: 24 additions & 0 deletions example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
port: 9090

log:
level: info
format: plain

database:
host: 127.0.0.1
port: 5432
user: terraboard
password: terraboard
name: terraboard
no-sync: true

aws:
dynamodb-table: terraboard
s3:
bucket: terraboard
key-prefix:
file-extension: .tfstate

web:
base-url: /terraboard/
logout-url: /test
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func getVersion(w http.ResponseWriter, r *http.Request) {
func main() {
c := config.LoadConfig(version)

util.UpdateBase(c.Web.BaseURL)

log.Infof("Terraboard v%s is starting...", version)

err := c.SetupLogging()
Expand Down
8 changes: 4 additions & 4 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var fileExtension string
func Setup(c *config.Config) {
sess := session.Must(session.NewSession())
svc = s3.New(sess, &aws.Config{})
bucket = c.S3.Bucket
keyPrefix = c.S3.KeyPrefix
fileExtension = c.S3.FileExtension
bucket = c.AWS.S3.Bucket
keyPrefix = c.AWS.S3.KeyPrefix
fileExtension = c.AWS.S3.FileExtension

dynamoSvc = dynamodb.New(sess, &aws.Config{})
dynamoTable = c.S3.DynamoDBTable
dynamoTable = c.AWS.DynamoDBTable
}

// LockInfo stores information on a State Lock
Expand Down
9 changes: 3 additions & 6 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package util
import (
"fmt"
"net/http"
"os"
"strings"
)

var baseURL string

func init() {
baseURL = os.Getenv("BASE_URL")
if baseURL == "" {
baseURL = "/"
}
// UpdateBase replaces baseURL with a new one
func UpdateBase(new string) {
baseURL = new
}

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

0 comments on commit 4fd47b4

Please sign in to comment.