environment-ize configuration

This commit is contained in:
David 2021-07-02 18:47:14 -04:00
parent 04506ed01f
commit ca618cc609
5 changed files with 36 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/server
/manager
*.properties

View File

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

View File

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

5
go.mod
View File

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

2
go.sum
View File

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