Load configuration

This commit is contained in:
Pierre HUBERT 2020-04-10 08:46:34 +02:00
parent 0ce15f51d9
commit 4990ecea0f
6 changed files with 62 additions and 5 deletions

2
.vscode/launch.json vendored
View File

@ -11,7 +11,7 @@
"mode": "auto", "mode": "auto",
"program": "${fileDirname}", "program": "${fileDirname}",
"env": {}, "env": {},
"args": [] "args": ["config.yaml"]
} }
] ]
} }

View File

@ -1,6 +1,36 @@
package main package main
// Value Main value import (
func Name() string { "io/ioutil"
return "pierre" "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
} }

9
config.yaml Normal file
View File

@ -0,0 +1,9 @@
# Proxy configuration
#
# @author Pierre Hubert
secure: false
hostname: 127.0.0.1
port: 3000
path: /rtc_proxy/ws
token: SecretToken

2
go.mod
View File

@ -1,3 +1,5 @@
module comunic_rtc_proxy module comunic_rtc_proxy
go 1.14 go 1.14
require gopkg.in/yaml.v2 v2.2.8

3
go.sum Normal file
View File

@ -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=

15
main.go
View File

@ -2,8 +2,21 @@ package main
import ( import (
"fmt" "fmt"
"os"
) )
func main() { 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 <config>", os.Args[0])
}
// First, load the config
conf := loadConfig(os.Args[1])
fmt.Println(conf)
} }