From ca618cc6099a140a4a4866283ea24303d29450ab Mon Sep 17 00:00:00 2001 From: David Ashby Date: Fri, 2 Jul 2021 18:47:14 -0400 Subject: [PATCH] environment-ize configuration --- .gitignore | 3 ++- Makefile | 11 ++++++++++- cmd/serve/main.go | 19 ++++++++++++++++++- go.mod | 5 ++++- go.sum | 2 ++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 227e820..31d7886 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /server -/manager \ No newline at end of file +/manager +*.properties \ No newline at end of file diff --git a/Makefile b/Makefile index 2420306..3e3b266 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,26 @@ -.PHONY: up down +.PHONY: up down run GOFILES=$(shell find . -name '*.go' -o -name 'go.*') STATICFILES=$(shell find . -name '*.js' -o -name '*.css' -o -name '*.html') SQLFILES=$(shell find . -name '*.sql') +ifneq (,$(wildcard ./local.properties)) +include local.properties +export +endif + build: server manager +run: build + ./server + server: $(GOFILES) $(STATICFILES) go build -o server ./cmd/serve manager: $(GOFILES) $(SQLFILES) go build -o manager ./cmd/manage +# dev dependencies up: docker compose up -d diff --git a/cmd/serve/main.go b/cmd/serve/main.go index 4130001..e59d55e 100644 --- a/cmd/serve/main.go +++ b/cmd/serve/main.go @@ -10,8 +10,17 @@ import ( "git.yetaga.in/alazyreader/library/book" "git.yetaga.in/alazyreader/library/database" "git.yetaga.in/alazyreader/library/frontend" + "github.com/kelseyhightower/envconfig" ) +type Config struct { + DBUser string + DBPass string + DBHost string + DBPort string + DBName string +} + type Library interface { GetAllBooks(context.Context) ([]book.Book, error) } @@ -54,11 +63,19 @@ func StaticHandler(f fs.FS) http.Handler { } func main() { + var c Config + err := envconfig.Process("library", &c) + if err != nil { + log.Fatalln(err) + } f, err := frontend.Root() if err != nil { log.Fatalln(err) } - lib, err := database.NewMySQLConnection("root", "KigYBNCT9IU5XyB3ehzMLFWyI", "127.0.0.1", "3306", "library") + if c.DBUser == "" || c.DBPass == "" || c.DBHost == "" || c.DBPort == "" || c.DBName == "" { + log.Fatalf("vars: %+v", c) + } + lib, err := database.NewMySQLConnection(c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName) if err != nil { log.Fatalln(err) } diff --git a/go.mod b/go.mod index 5e7f8e3..ee0cfff 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.yetaga.in/alazyreader/library go 1.16 -require github.com/go-sql-driver/mysql v1.6.0 +require ( + github.com/go-sql-driver/mysql v1.6.0 + github.com/kelseyhightower/envconfig v1.4.0 +) diff --git a/go.sum b/go.sum index 20c16d6..0415f9f 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=