Clean up config loading code
This commit is contained in:
parent
5b27a0568c
commit
71a60f8003
7 changed files with 115 additions and 108 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/status
|
||||
/proto/config.toml
|
60
core/config.go
Normal file
60
core/config.go
Normal file
|
@ -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
|
||||
}
|
10
main.go
10
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))
|
||||
|
|
|
@ -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"
|
|
@ -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}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue