Пример #1
0
func assertConfigExists(key string, value string) {
	if value == "" {
		log.Fatal("Missing environment variable.", log.Fields{
			"variable": config.GetEnvironmentVariableName(key),
		})
	}
}
Пример #2
0
func main() {

	initLogger()

	dbDriver := config.GetString(keys.DatabaseDriver)
	assertConfigExists(keys.DatabaseDriver, dbDriver)

	dbDSN := config.GetString(keys.DatabaseDSN)
	assertConfigExists(keys.DatabaseDSN, dbDSN)

	port := config.GetString(keys.Port)
	assertConfigExists(keys.Port, port)

	migrationsPath := config.GetString(keys.DatabaseMigrationsPath)
	assertConfigExists(keys.DatabaseMigrationsPath, migrationsPath)

	log.Debug("Opening database connection", log.Fields{"driver": dbDriver, "dsn": dbDSN})
	ds, err := datastore.Open(dbDriver, dbDSN)
	if err != nil {
		log.Fatal("Failed to open database connection", log.Fields{
			"error": err,
		})
	}

	errs, ok := ds.UpSync(migrationsPath)
	if !ok {
		for _, err := range errs {
			log.Fatal("Failed to synchronise datastore tables", log.Fields{
				"error": err,
			})
		}
	}

	server := NewServer(ds)

	log.Info("Server is now listening for requests", log.Fields{
		"port": port,
	})
	server.ListenAndServe(":" + port)
}