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"
"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{