From 226b2ba14038ffaa6d6c15d8740d3cc926bad08b Mon Sep 17 00:00:00 2001 From: Christian Claus Date: Mon, 9 Apr 2018 14:12:54 +0200 Subject: [PATCH] Add basic config management --- Gopkg.lock | 80 +++++++++++++++++++++++++++++++++++++++++++++- app/app.go | 2 ++ app/config.go | 42 ++++++++++++++++++++++++ cmd/server/main.go | 8 ++++- config/config.yaml | 2 ++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 app/app.go create mode 100644 app/config.go create mode 100644 config/config.yaml diff --git a/Gopkg.lock b/Gopkg.lock index ab7226d..516fa43 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..a2355d7 --- /dev/null +++ b/app/app.go @@ -0,0 +1,2 @@ +// Package app provides all app related stuff like config parsing, serving, etc. +package app diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..f9fc967 --- /dev/null +++ b/app/config.go @@ -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") +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 5be262c..f542c40 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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) } diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..9da04b8 --- /dev/null +++ b/config/config.yaml @@ -0,0 +1,2 @@ +address: 127.0.0.1 +port: 8000