From 4990ecea0fd65ad563a5c6a55ae45402ef078ba7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Apr 2020 08:46:34 +0200 Subject: [PATCH] Load configuration --- .vscode/launch.json | 2 +- config.go | 36 +++++++++++++++++++++++++++++++++--- config.yaml | 9 +++++++++ go.mod | 2 ++ go.sum | 3 +++ main.go | 15 ++++++++++++++- 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 config.yaml create mode 100644 go.sum diff --git a/.vscode/launch.json b/.vscode/launch.json index 2978dcf..9d10d0a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "mode": "auto", "program": "${fileDirname}", "env": {}, - "args": [] + "args": ["config.yaml"] } ] } \ No newline at end of file diff --git a/config.go b/config.go index 94dae1c..3c73460 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,36 @@ package main -// Value Main value -func Name() string { - return "pierre" +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +// Config stores application configuration +type Config struct { + Secure bool + Hostname string + Port int + Path string + Token string +} + +// Load the configuration +func loadConfig(path string) Config { + + // Load data + dat, err := ioutil.ReadFile(path) + if err != nil { + log.Fatalf("Could not read configuration: %v", err) + } + + // Decode data + conf := Config{} + err = yaml.UnmarshalStrict(dat, &conf) + if err != nil { + log.Fatalf("Could not decode configuration: %v", err) + } + + return conf } diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..03ff419 --- /dev/null +++ b/config.yaml @@ -0,0 +1,9 @@ +# Proxy configuration +# +# @author Pierre Hubert + +secure: false +hostname: 127.0.0.1 +port: 3000 +path: /rtc_proxy/ws +token: SecretToken \ No newline at end of file diff --git a/go.mod b/go.mod index 90f13e2..e4099ef 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module comunic_rtc_proxy go 1.14 + +require gopkg.in/yaml.v2 v2.2.8 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0c4da7f --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 1fc419b..ebfc87c 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,21 @@ package main import ( "fmt" + "os" ) func main() { - fmt.Println("secret", Name()) + + println("Comunic RTC Proxy. (c) Pierre Hubert 2020") + + // Command line args + if len(os.Args) != 2 { + fmt.Printf("Usage: %s ", os.Args[0]) + } + + // First, load the config + conf := loadConfig(os.Args[1]) + + fmt.Println(conf) + }