func createTestRouter(apiPath string) *routes.Router { ds, err := datastore.Open("sqlite3", "file:test.db?cache=shared&mode=memory") if err != nil { panic(err) } migrationsPath := config.GetString(keys.DatabaseMigrationsPath) if migrationsPath == "" { panic("Migrations path has not been defined.") } errs, ok := ds.UpSync(migrationsPath) if !ok { errMsg := "" for _, err := range errs { errMsg = errMsg + err.Error() } panic(errMsg) } dao := users.NewDao(ds) err = dao.Insert(testUser) if err != nil { panic(err) } return NewRouter(apiPath, dao) }
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) }