add config file

This commit is contained in:
XenGi 2023-12-26 17:23:53 +01:00
parent 99dc857d41
commit af8b50d464
Signed by: xengi
SSH key fingerprint: SHA256:FGp51kRvGOcWnTHiOI39ImwVO4A3fpvR30nPX3LpV7g
4 changed files with 51 additions and 2 deletions

12
config.ini Normal file
View file

@ -0,0 +1,12 @@
[mpd]
host = localhost
port = 6600
#username =
#pasword =
[ui]
hostname = localhost
port = 8080
tls = no
cert = cert.pem
key = key.pem

1
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/labstack/echo-contrib v0.15.0 github.com/labstack/echo-contrib v0.15.0
github.com/labstack/echo/v4 v4.11.2 github.com/labstack/echo/v4 v4.11.2
golang.org/x/net v0.17.0 golang.org/x/net v0.17.0
gopkg.in/ini.v1 v1.67.0
) )
require ( require (

2
go.sum
View file

@ -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 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 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/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-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.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -8,6 +8,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"gopkg.in/ini.v1"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -15,7 +16,37 @@ import (
"strings" "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() { 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 := echo.New()
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
@ -61,8 +92,11 @@ func main() {
e.GET("/ws", wsServe) e.GET("/ws", wsServe)
//e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem")) if config.UI.Tls {
e.Logger.Fatal(e.Start(":1323")) 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 { func wsServe(c echo.Context) error {