Configuration file with TOML

This commit is contained in:
VnPower 2023-10-01 10:07:29 +07:00
parent ff4a1a5ea0
commit 840d8cda6c
Signed by: vnpower
GPG key ID: 881DE3DEB966106C
6 changed files with 59 additions and 15 deletions

1
go.mod
View file

@ -9,6 +9,7 @@ require maunium.net/go/mautrix v0.16.1
require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rs/zerolog v1.30.0 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect

10
go.sum
View file

@ -1,6 +1,7 @@
github.com/cbroglie/mustache v1.4.0 h1:Azg0dVhxTml5me+7PsZ7WPrQq1Gkf3WApcHMjMprYoU=
github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9DtDsHo9iM=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@ -8,12 +9,19 @@ github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZb
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
@ -37,6 +45,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=

View file

@ -26,7 +26,7 @@ func parseAndRenderTemplate(templateLocation string, data interface{}) (string,
return res, nil
}
var config scanner.Config = scanner.LoadConfig()
var config scanner.Config = scanner.LoadTOMLConfig()
func rootPage(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
@ -61,7 +61,6 @@ func main() {
fs := http.FileServer(http.Dir("./public"))
http.Handle("/public/", http.StripPrefix("/public/", fs))
protocol := "tcp"
addr := "localhost:3333"

6
proto/testconfig.toml Normal file
View file

@ -0,0 +1,6 @@
service = [
{ description = "exozy.me", url = "https://exozy.me" },
{ description = "drgns.space", url = "https://drgns.space"},
{ description = "invalid.example.com", url = "https://invalid.example.com"},
{ description = "port 1234", url = "tcp://localhost:1234"},
]

View file

@ -1,13 +1,19 @@
package scanner
import "net/url"
import (
"io"
"net/url"
"os"
"github.com/pelletier/go-toml/v2"
)
type Config struct {
Service []ServiceConfig
}
type ServiceConfig struct {
description string
Description string
Url string
}
@ -19,27 +25,49 @@ func (s ServiceConfig) URL() *url.URL {
return res
}
func LoadTOMLConfig() Config {
configPath := "proto/testconfig.toml"
// configPath := "/opt/status-config/config.toml"
file, err := os.Open(configPath)
if err != nil {
// we might not need this check if the config file is not required
panic(err)
}
defer file.Close()
fileData, err := io.ReadAll(file)
if err != nil {
panic(err)
}
var cfg Config
err = toml.Unmarshal([]byte(fileData), &cfg)
return cfg
}
func LoadConfig() Config {
// todo: make this load from toml
services := []ServiceConfig{
{
description: "exozy.me",
Url: "https://exozy.me",
Description: "exozy.me",
Url: "https://exozy.me",
},
{
description: "drgns.space",
Url: "https://drgns.space",
Description: "drgns.space",
Url: "https://drgns.space",
},
{
description: "invalid.example.com",
Url: "https://invalid.example.com",
Description: "invalid.example.com",
Url: "https://invalid.example.com",
},
{
description: "port 1234",
Url: "tcp://localhost:1234",
Description: "port 1234",
Url: "tcp://localhost:1234",
},
}
return Config {Service: services}
return Config{Service: services}
}
type ServiceStatus struct {

View file

@ -26,8 +26,8 @@ func TestService(service ServiceConfig) ServiceStatus {
}
return ServiceStatus{
Name: service.description,
Ok: serviceResult,
Name: service.Description,
Ok: serviceResult,
Status: status,
}
}