From dd22f01dac2ecb5077e4cfd461899124925cdd2b Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 4 Feb 2023 20:21:10 -0500 Subject: [PATCH] Centralize config --- config/config.go | 26 ++++++++++++++++++++++++++ matrix/matrix.go | 15 ++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..d05413d --- /dev/null +++ b/config/config.go @@ -0,0 +1,26 @@ +package config + +import ( + "os" +) + +type ( + Config struct { + MatrixIdentityServer string + MatrixWellKnownAddress string + } +) + +func GetConfig() Config { + return Config{ + MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""), + MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""), + } +} + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} diff --git a/matrix/matrix.go b/matrix/matrix.go index ff028a3..ab378dc 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -4,7 +4,8 @@ import ( "encoding/json" "fmt" "net/http" - "os" + + "git.minhas.io/asara/well-goknown/config" ) type matrixServerWellKnown struct { @@ -13,8 +14,8 @@ type matrixServerWellKnown struct { func MatrixServer(w http.ResponseWriter, req *http.Request) { // uses an environment variable for now - wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") - if !ok { + wellKnownAddr := config.GetConfig().MatrixWellKnownAddress + if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) return } @@ -42,14 +43,14 @@ type matrixClientWellKnown struct { func MatrixClient(w http.ResponseWriter, req *http.Request) { // uses an environment variable for now - wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") - if !ok { + wellKnownAddr := config.GetConfig().MatrixWellKnownAddress + if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) return } - identityServer, ok := os.LookupEnv("MATRIX_IDENTITY_SERVER") - if !ok { + identityServer := config.GetConfig().MatrixIdentityServer + if identityServer == "" { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) baseUrl := &matrixClientWellKnown{