~xenrox/10man-api

312f1f47bdfc8fdd5d270e19b05db78c2cfd5d56 — Thorben Günther 2 years ago 34e8900
config: Read configuration from file with go-scfg
3 files changed, 48 insertions(+), 2 deletions(-)

M config/config.go
M go.mod
M go.sum
M config/config.go => config/config.go +42 -2
@@ 1,7 1,47 @@
package config

import (
	"log"
	"strings"

	"git.sr.ht/~emersion/go-scfg"
)

var configPath string = "/etc/10man-api/config"

// ConnectionString is used to connect to the database
const ConnectionString = "postgresql://user:pass@localhost/db?sslmode=disable"
var ConnectionString string

// Admins array of steamID64 of users that should become admins
var Admins = [1]string{"STEAMID64"}
var Admins []string

func init() {
	cfg, err := scfg.Load(configPath)
	if err != nil {
		log.Fatalf("could not read config file %q", configPath)
	}

	for _, d := range cfg {
		switch d.Name {
		case "connection-string":
			if err := d.ParseParams(&ConnectionString); err != nil {
				log.Fatalf("could not parse connection-string: %v", err)
			}

		case "admins":
			var admins string
			if err := d.ParseParams(&admins); err != nil {
				log.Fatalf("could not parse admins: %v", err)
			}

			Admins = strings.Split(admins, ",")

		default:
			log.Fatalf("illegal config value %q", d.Name)
		}
	}

	if ConnectionString == "" {
		log.Fatalf("could not parse connection-string")
	}
}

M go.mod => go.mod +2 -0
@@ 9,7 9,9 @@ require (
)

require (
	git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc // indirect
	github.com/agnivade/levenshtein v1.1.1 // indirect
	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
	github.com/gorilla/websocket v1.4.2 // indirect
	github.com/hashicorp/golang-lru v0.5.4 // indirect
	github.com/mitchellh/mapstructure v1.4.2 // indirect

M go.sum => go.sum +4 -0
@@ 1,3 1,5 @@
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc h1:51BD67xFX+bozd3ZRuOUfalrhx4/nQSh6A9lI08rYOk=
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U=
github.com/99designs/gqlgen v0.14.0 h1:Wg8aNYQUjMR/4v+W3xD+7SizOy6lSvVeQ06AobNQAXI=
github.com/99designs/gqlgen v0.14.0/go.mod h1:S7z4boV+Nx4VvzMUpVrY/YuHjFX4n7rDyuTqvAkuoRE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=


@@ 16,6 18,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=