output log messages during bootstrap, eg with config file location

This commit is contained in:
Danny 2018-05-23 09:03:42 +02:00
parent 7c3cecad09
commit fc35d68926
4 changed files with 43 additions and 14 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"log"
"os" "os"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/config" "github.com/usefathom/fathom/pkg/config"
"github.com/usefathom/fathom/pkg/datastore" "github.com/usefathom/fathom/pkg/datastore"
@ -72,7 +72,8 @@ func main() {
} }
func before(c *cli.Context) error { func before(c *cli.Context) error {
app.config = config.Parse(c.String("config")) configFile := c.String("config")
app.config = config.Parse(configFile)
app.database = datastore.New(app.config.Database) app.database = datastore.New(app.config.Database)
return nil return nil
} }

View File

@ -31,7 +31,7 @@ func server(c *cli.Context) error {
} }
// start listening // start listening
log.Infof("server will now listening on %s", addr) log.Printf("Fathom is now listening on %s", addr)
err := http.ListenAndServe(addr, h) err := http.ListenAndServe(addr, h)
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)

View File

@ -1,12 +1,12 @@
package config package config
import ( import (
"math/rand"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/usefathom/fathom/pkg/datastore/sqlstore" "github.com/usefathom/fathom/pkg/datastore/sqlstore"
"math/rand"
"os"
) )
// Config wraps the configuration structs for the various application parts // Config wraps the configuration structs for the various application parts
@ -21,15 +21,35 @@ func Parse(file string) *Config {
var err error var err error
if file != "" { if file != "" {
err = godotenv.Load(file) // get absolute path to config file
if err != nil && file != ".env" { wd, _ := os.Getwd()
log.Fatalf("error parsing config file: %s", err) absfile := wd + "/" + file
// check if file exists
_, err := os.Stat(absfile)
fileNotExists := os.IsNotExist(err)
// Print config file location
if file != ".env" || !fileNotExists {
log.Printf("Configuration file: %s", absfile)
}
// Abort if custom config file does not exist
if file != ".env" && fileNotExists {
log.Fatalf("Error reading configuration. File `%s` does not exist.", file)
}
// read file into env values
err = godotenv.Load(absfile)
if err != nil {
log.Fatalf("Error parsing configuration file: %s", err)
} }
} }
// with config file loaded into env values, we can now parse env into our config struct
err = envconfig.Process("Fathom", &cfg) err = envconfig.Process("Fathom", &cfg)
if err != nil { if err != nil {
log.Fatalf("error parsing config from environment values: %s", err) log.Fatalf("Error parsing configuration from environment: %s", err)
} }
// alias sqlite to sqlite3 // alias sqlite to sqlite3

View File

@ -33,20 +33,28 @@ func New(c *Config) *sqlstore {
} }
func (db *sqlstore) Migrate() { func (db *sqlstore) Migrate() {
migrations := &migrate.PackrMigrationSource{ migrationSource := &migrate.PackrMigrationSource{
Box: packr.NewBox("./migrations"), Box: packr.NewBox("./migrations"),
Dir: "./" + db.Config.Driver, Dir: "./" + db.Config.Driver,
} }
migrate.SetTable("migrations") migrate.SetTable("migrations")
n, err := migrate.Exec(db.DB.DB, db.Config.Driver, migrations, migrate.Up) migrations, err := migrationSource.FindMigrations()
if err != nil { if err != nil {
log.Errorf("database migrations failed: %s", err) log.Errorf("Error loading database migrations: %s", err)
}
if len(migrations) == 0 {
log.Fatalf("Missing database migrations")
}
n, err := migrate.Exec(db.DB.DB, db.Config.Driver, migrationSource, migrate.Up)
if err != nil {
log.Errorf("Error applying database migrations: %s", err)
} }
if n > 0 { if n > 0 {
log.Infof("applied %d database migrations", n) log.Infof("Applied %d database migrations!", n)
} }
} }