Пример #1
0
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)
}
Пример #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)
}
Пример #3
0
func initLogger() {
	levelString := config.GetString(keys.LogLevel)
	level := log.InfoLevel
	if levelString != "" {
		if parsedLevel, err := log.ParseLevel(levelString); err != nil {
			log.Error("Unrecognised log level", log.Fields{
				"level": levelString,
			})
		} else {
			level = parsedLevel
		}
	}

	log.SetLevel(level)
}
Пример #4
0
func NewRouter(viewsPath string, userDao *users.Dao, wizardDao *wizards.Dao) http.Handler {

	// Initialise the session store with the necessary keys
	sessionStore := sessions.NewCookieStore([]byte(config.GetString(keys.SessionKey))) // TODO: read this directly from config? make it another arg?
	sessionStore.Options = &sessions.Options{
		Path:   "/",
		MaxAge: secondsPerHour,
		Secure: config.GetBool(keys.SessionSecure, false),
	}

	// Create a new router instance with the obtained data
	router := &Router{Router: mux.NewRouter(),
		path:         viewsPath,
		userDao:      userDao,
		wizardDao:    wizardDao,
		sessionStore: sessionStore,
	}

	// Add all of the routes to the router
	router.NotFoundHandler = &custom404Handler{}
	initRoutes(router)
	return router
}