From 71a60f80032ddbfe062302c26b2576a134d4195e Mon Sep 17 00:00:00 2001 From: Locria Cyber <74560659+iacore@users.noreply.github.com> Date: Sun, 1 Oct 2023 08:21:30 +0000 Subject: [PATCH] Clean up config loading code --- .gitignore | 1 + core/config.go | 60 +++++++++++++++++ main.go | 10 ++- proto/{testconfig.toml => config.toml.in} | 4 +- proto/matrix/main.go | 79 ++++++++++++----------- readme.md | 2 +- scanner/config.go | 67 ------------------- 7 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 core/config.go rename proto/{testconfig.toml => config.toml.in} (90%) diff --git a/.gitignore b/.gitignore index 529a224..38c3f99 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /status +/proto/config.toml \ No newline at end of file diff --git a/core/config.go b/core/config.go new file mode 100644 index 0000000..4a54485 --- /dev/null +++ b/core/config.go @@ -0,0 +1,60 @@ +package core + +import ( + "fmt" + "io" + "os" + + "git.exozy.me/exozyme/status/scanner" + "github.com/pelletier/go-toml/v2" +) + +type Config struct { + Service []scanner.ServiceConfig + Matrix MatrixConfig + UnixSocket string +} + +type MatrixConfig struct { + UserId string + AccessToken string + RoomId string +} + +func LoadConfigFromTOML(configPath string) (*Config, error) { + file, err := os.Open(configPath) + if err != nil { + return nil, err + } + defer file.Close() + + fileData, err := io.ReadAll(file) + if err != nil { + return nil, err + } + + var cfg Config + err = toml.Unmarshal([]byte(fileData), &cfg) + if err != nil { + return nil, err + } + return &cfg, nil +} + +func LoadConfig() (*Config, error) { + const path1 string = "/opt/status-config/config.toml" + const path2 string = "proto/config.toml" + const path3 string = "../config.toml" + config, err := LoadConfigFromTOML(path1) + if err != nil { + config, err = LoadConfigFromTOML(path2) + if err != nil { + config, err = LoadConfigFromTOML(path3) + if err != nil { + return nil, fmt.Errorf("Cannot load config file:\n- %v\n- %v\n- %v\n%w\nSuggestion: cp proto/config.toml.in proto/config.toml", path1, path2, path3, err) + } + } + } + + return config, nil +} diff --git a/main.go b/main.go index ebc0196..1066c6e 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "git.exozy.me/exozyme/status/scanner" + "git.exozy.me/exozyme/status/core" "github.com/cbroglie/mustache" ) @@ -26,8 +27,6 @@ func parseAndRenderTemplate(templateLocation string, data interface{}) (string, return res, nil } -var config scanner.Config = scanner.LoadTOMLConfig() - func rootPage(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { return @@ -56,7 +55,14 @@ func removeFile(path string) { } } +var config *core.Config + func main() { + config, err := core.LoadConfig() + if err != nil { + log.Fatal(err) + } + http.HandleFunc("/", rootPage) fs := http.FileServer(http.Dir("./public")) http.Handle("/public/", http.StripPrefix("/public/", fs)) diff --git a/proto/testconfig.toml b/proto/config.toml.in similarity index 90% rename from proto/testconfig.toml rename to proto/config.toml.in index 5a8e95f..8c358af 100644 --- a/proto/testconfig.toml +++ b/proto/config.toml.in @@ -1,4 +1,4 @@ -unixSocket = "" +# unixSocket = "" service = [ { description = "exozy.me", url = "https://exozy.me" }, { description = "drgns.space", url = "https://drgns.space" }, @@ -8,5 +8,5 @@ service = [ [matrix] userID = "@exozymebot76397:matrix.org" -accessToken = "nil" +# accessToken = "nil" roomID = "#room-name:matrix.org" diff --git a/proto/matrix/main.go b/proto/matrix/main.go index b8ab42a..1ac3462 100644 --- a/proto/matrix/main.go +++ b/proto/matrix/main.go @@ -1,61 +1,44 @@ package main import ( - "fmt" "os" + "fmt" "strings" "time" m "maunium.net/go/mautrix" . "maunium.net/go/mautrix/id" + + "git.exozy.me/exozyme/status/core" ) -const default_user_id = "@exozymebot76397:matrix.org" -const default_room_id = "#status-page-hackathon:exozy.me" -// this also works (opaque room id) -// const default_room_id = "!fXEYZcqHcbjztwNhhD:exozy.me" - -type Config struct { - user_id string - access_token string - room_id string -} - -func LoadConfig() Config { - user_id, success := os.LookupEnv("MAUTRIX_USER_ID") - if !success { - user_id = default_user_id - } - - access_token, success := os.LookupEnv("MAUTRIX_ACCESS_TOKEN") - if !success { - panic("no access token") - } - - room_id, success := os.LookupEnv("MAUTRIX_ROOM_ID") - if !success { - room_id = default_room_id - } - return Config{ user_id, access_token, room_id } -} - +type MatrixConfig = core.MatrixConfig func main() { // the API key can be got by logging in with any Matrix client, and get it // in element.io, find it under Settings -> About & Help -> Advanced - config := LoadConfig() + root_config, err := core.LoadConfig() + if err != nil { + panic(err) + } + config := root_config.Matrix - user_id := UserID(config.user_id) - client, err := m.NewClient(user_id.Homeserver(), user_id, config.access_token) + // todo: check Matrix configuration and stuff + if config.AccessToken == "" { + panic("no access token") + } + + user_id := UserID(config.UserId) + client, err := m.NewClient(user_id.Homeserver(), user_id, config.AccessToken) if err != nil { panic(err) } var opaque_room_id RoomID - if strings.HasPrefix(config.room_id, "!") { - opaque_room_id = RoomID(config.room_id) + if strings.HasPrefix(config.RoomId, "!") { + opaque_room_id = RoomID(config.RoomId) } else { - resp, err := client.ResolveAlias(RoomAlias(config.room_id)) + resp, err := client.ResolveAlias(RoomAlias(config.RoomId)) if err != nil { panic(err) } @@ -75,3 +58,27 @@ func main() { } fmt.Printf("%v", resp) } + +// backup loading code +func LoadConfigFromEnviron() MatrixConfig { + const default_user_id = "@exozymebot76397:matrix.org" + const default_room_id = "#status-page-hackathon:exozy.me" + // this also works (opaque room id) + // const default_room_id = "!fXEYZcqHcbjztwNhhD:exozy.me" + + user_id, success := os.LookupEnv("MAUTRIX_USER_ID") + if !success { + user_id = default_user_id + } + + access_token, success := os.LookupEnv("MAUTRIX_ACCESS_TOKEN") + if !success { + panic("no access token") + } + + room_id, success := os.LookupEnv("MAUTRIX_ROOM_ID") + if !success { + room_id = default_room_id + } + return MatrixConfig{UserId: user_id, AccessToken: access_token, RoomId: room_id} +} diff --git a/readme.md b/readme.md index 027e6f9..b82b4b3 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ Tech stack - [x] listen on unix socket PORT (if set) - [x] Templating - {{mustache}} - [x] spawn with dinit/systemd -- [x] Config file - load TOML config (1. `/opt/status-config/config.toml` 2. test config at `proto/testconfig.toml`) +- [x] Config file - load TOML config (1. `/opt/status-config/config.toml` 2. test config at `proto/config.toml`) - .socket_path (UNIX socket, string) - .service (array of services) - .matrix (Matrix-related settings, see below) diff --git a/scanner/config.go b/scanner/config.go index 77ad316..73e1803 100644 --- a/scanner/config.go +++ b/scanner/config.go @@ -1,30 +1,14 @@ package scanner import ( - "io" "net/url" - "os" - - "github.com/pelletier/go-toml/v2" ) -type Config struct { - Service []ServiceConfig - Matrix MatrixConfig - UnixSocket string -} - type ServiceConfig struct { Description string Url string } -type MatrixConfig struct { - UserID string - AccessToken string - RoomID string -} - func (s ServiceConfig) URL() *url.URL { res, err := url.Parse(s.Url) if err != nil { @@ -33,57 +17,6 @@ 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 - // todo: check envvar if the config file is down - panic(err) - } - defer file.Close() - - fileData, err := io.ReadAll(file) - if err != nil { - panic(err) - } - - var cfg Config - err = toml.Unmarshal([]byte(fileData), &cfg) - - // todo: check Matrix configuration and stuff - if cfg.Matrix.AccessToken == "" { - panic("no access token") - } - - return cfg -} - -func LoadConfig() Config { - // todo: make this load from toml - services := []ServiceConfig{ - { - 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", - }, - } - return Config{Service: services} -} - type ServiceStatus struct { Name string Ok bool