Add basic config management

This commit is contained in:
Christian Claus 2018-04-09 14:12:54 +02:00
parent cc750494b3
commit 226b2ba140
5 changed files with 132 additions and 2 deletions

80
Gopkg.lock generated
View file

@ -1,15 +1,93 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/fsnotify/fsnotify"
packages = ["."]
revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9"
version = "v1.4.7"
[[projects]]
branch = "master"
name = "github.com/hashicorp/hcl"
packages = [".","hcl/ast","hcl/parser","hcl/printer","hcl/scanner","hcl/strconv","hcl/token","json/parser","json/scanner","json/token"]
revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168"
[[projects]]
name = "github.com/magiconair/properties"
packages = ["."]
revision = "c3beff4c2358b44d0493c7dda585e7db7ff28ae6"
version = "v1.7.6"
[[projects]]
branch = "master"
name = "github.com/mitchellh/mapstructure"
packages = ["."]
revision = "00c29f56e2386353d58c599509e8dc3801b0d716"
[[projects]]
name = "github.com/pelletier/go-toml"
packages = ["."]
revision = "acdc4509485b587f5e675510c4f2c63e90ff68a8"
version = "v1.1.0"
[[projects]]
name = "github.com/spf13/afero"
packages = [".","mem"]
revision = "63644898a8da0bc22138abf860edaf5277b6102e"
version = "v1.1.0"
[[projects]]
name = "github.com/spf13/cast"
packages = ["."]
revision = "8965335b8c7107321228e3e3702cab9832751bac"
version = "v1.2.0"
[[projects]]
branch = "master"
name = "github.com/spf13/jwalterweatherman"
packages = ["."]
revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394"
[[projects]]
name = "github.com/spf13/pflag"
packages = ["."]
revision = "e57e3eeb33f795204c1ca35f56c44f83227c6e66"
version = "v1.0.0"
[[projects]]
name = "github.com/spf13/viper"
packages = ["."]
revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736"
version = "v1.0.2"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context","webdav","webdav/internal/xml"]
revision = "61147c48b25b599e5b561d2e9c4f3e1ef489ca41"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = ["unix"]
revision = "3b87a42e500a6dc65dae1a55d0b641295971163e"
[[projects]]
name = "golang.org/x/text"
packages = ["internal/gen","internal/triegen","internal/ucd","transform","unicode/cldr","unicode/norm"]
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
name = "gopkg.in/yaml.v2"
packages = ["."]
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "75aa6455008c8c6d06a98a51811e64bea0bf5428a0b53a5171e5885f7e9bdfa7"
inputs-digest = "75f05f8972c0c4f13108f362676581e484253474b57d6fcc5f9ecb6c657acf2b"
solver-name = "gps-cdcl"
solver-version = 1

2
app/app.go Normal file
View file

@ -0,0 +1,2 @@
// Package app provides all app related stuff like config parsing, serving, etc.
package app

42
app/config.go Normal file
View file

@ -0,0 +1,42 @@
package app
import (
"github.com/spf13/viper"
"fmt"
)
// Config represents the configuration of the server application.
type Config struct {
Address string
Port string
}
// ParseConfig parses the application configuration an sets defaults.
func ParseConfig() Config {
var cfg Config
setDefaults();
viper.SetConfigName("config")
viper.AddConfigPath("./config")
viper.AddConfigPath("$HOME/.swd")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s", err))
}
err = viper.Unmarshal(&cfg)
if err != nil {
panic(fmt.Errorf("Fatal error parsing config file: %s", err))
}
return cfg
}
// setDefaults adds some default values for the configuration
func setDefaults() {
viper.SetDefault("Address", "127.0.0.1")
viper.SetDefault("Port", "8000")
}

View file

@ -3,13 +3,19 @@ package main
import (
"golang.org/x/net/webdav"
"net/http"
"github.com/micromata/swd/app"
"fmt"
)
func main() {
config := app.ParseConfig()
wdHandler := &webdav.Handler{
FileSystem: webdav.Dir("/tmp"),
LockSystem: webdav.NewMemLS(),
}
http.ListenAndServe(":8000", wdHandler)
connAddr := fmt.Sprintf("%s:%s", config.Address, config.Port)
fmt.Printf("Server is starting and listening at: %s \n", connAddr)
http.ListenAndServe(connAddr, wdHandler)
}

2
config/config.yaml Normal file
View file

@ -0,0 +1,2 @@
address: 127.0.0.1
port: 8000