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
import (
"log"
"os"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/config"
"github.com/usefathom/fathom/pkg/datastore"
@ -72,7 +72,8 @@ func main() {
}
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)
return nil
}

View File

@ -31,7 +31,7 @@ func server(c *cli.Context) error {
}
// 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)
if err != nil {
log.Errorln(err)

View File

@ -1,12 +1,12 @@
package config
import (
"math/rand"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
"github.com/usefathom/fathom/pkg/datastore/sqlstore"
"math/rand"
"os"
)
// Config wraps the configuration structs for the various application parts
@ -21,15 +21,35 @@ func Parse(file string) *Config {
var err error
if file != "" {
err = godotenv.Load(file)
if err != nil && file != ".env" {
log.Fatalf("error parsing config file: %s", err)
// get absolute path to config file
wd, _ := os.Getwd()
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)
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

View File

@ -33,20 +33,28 @@ func New(c *Config) *sqlstore {
}
func (db *sqlstore) Migrate() {
migrations := &migrate.PackrMigrationSource{
migrationSource := &migrate.PackrMigrationSource{
Box: packr.NewBox("./migrations"),
Dir: "./" + db.Config.Driver,
}
migrate.SetTable("migrations")
n, err := migrate.Exec(db.DB.DB, db.Config.Driver, migrations, migrate.Up)
migrations, err := migrationSource.FindMigrations()
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 {
log.Infof("applied %d database migrations", n)
log.Infof("Applied %d database migrations!", n)
}
}