From af8b50d464f2167936febf3b612f4a3e04929824 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Tue, 26 Dec 2023 17:23:53 +0100 Subject: [PATCH] add config file --- config.ini | 12 ++++++++++++ go.mod | 1 + go.sum | 2 ++ server.go | 38 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 config.ini diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..3f54744 --- /dev/null +++ b/config.ini @@ -0,0 +1,12 @@ +[mpd] +host = localhost +port = 6600 +#username = +#pasword = + +[ui] +hostname = localhost +port = 8080 +tls = no +cert = cert.pem +key = key.pem diff --git a/go.mod b/go.mod index 25cad23..a81631f 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/labstack/echo-contrib v0.15.0 github.com/labstack/echo/v4 v4.11.2 golang.org/x/net v0.17.0 + gopkg.in/ini.v1 v1.67.0 ) require ( diff --git a/go.sum b/go.sum index c6db544..153f4b9 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/server.go b/server.go index aa1a168..fbd02c5 100644 --- a/server.go +++ b/server.go @@ -8,6 +8,7 @@ import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "golang.org/x/net/websocket" + "gopkg.in/ini.v1" "log" "net/http" "os" @@ -15,7 +16,37 @@ import ( "strings" ) +type Config struct { + MPD struct { + Hostname string `ini:"hostname"` + Port int `ini:"port"` + Username string `ini:"username"` + Password string `ini:"password"` + } `ini:"mpd"` + UI struct { + Hostname string `ini:"hostname"` + Port int `ini:"port"` + Tls bool `ini:"tls"` + Certificate string `ini:"cert"` + Key string `ini:"key"` + } `ini:"ui"` +} + func main() { + iniData, err := ini.Load("config.ini") + if err != nil { + fmt.Printf("Fail to read configuration file: %v", err) + os.Exit(1) + } + + var config Config + + err = iniData.MapTo(&config) + if err != nil { + fmt.Printf("Fail to parse configuration file: %v", err) + os.Exit(1) + } + e := echo.New() e.Use(middleware.Logger()) e.Use(middleware.Recover()) @@ -61,8 +92,11 @@ func main() { e.GET("/ws", wsServe) - //e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem")) - e.Logger.Fatal(e.Start(":1323")) + if config.UI.Tls { + e.Logger.Fatal(e.StartTLS(fmt.Sprintf("%s:%d", config.UI.Hostname, config.UI.Port), config.UI.Certificate, config.UI.Key)) + } else { + e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", config.UI.Hostname, config.UI.Port))) + } } func wsServe(c echo.Context) error {