Centralize config

This commit is contained in:
Amarpreet Minhas 2023-02-04 20:21:10 -05:00
parent ebbe258b0a
commit dd22f01dac
2 changed files with 34 additions and 7 deletions

26
config/config.go Normal file
View file

@ -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
}

View file

@ -4,7 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os"
"git.minhas.io/asara/well-goknown/config"
) )
type matrixServerWellKnown struct { type matrixServerWellKnown struct {
@ -13,8 +14,8 @@ type matrixServerWellKnown struct {
func MatrixServer(w http.ResponseWriter, req *http.Request) { func MatrixServer(w http.ResponseWriter, req *http.Request) {
// uses an environment variable for now // uses an environment variable for now
wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
if !ok { if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
@ -42,14 +43,14 @@ type matrixClientWellKnown struct {
func MatrixClient(w http.ResponseWriter, req *http.Request) { func MatrixClient(w http.ResponseWriter, req *http.Request) {
// uses an environment variable for now // uses an environment variable for now
wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
if !ok { if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
identityServer, ok := os.LookupEnv("MATRIX_IDENTITY_SERVER") identityServer := config.GetConfig().MatrixIdentityServer
if !ok { if identityServer == "" {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
baseUrl := &matrixClientWellKnown{ baseUrl := &matrixClientWellKnown{