Exemplo n.º 1
0
// dbManager manages database connections, and communicates back and forth with the manager goroutine
func dbManager(conf config.Config, dbLaunchChan chan struct{}, dbKillChan chan struct{}) {
	log.Println("db: starting...")

	// Attempt to open database connection, depending on configuration
	// sqlite
	if conf.Sqlite != nil {
		log.Println("db: sqlite:", conf.Sqlite.File)

		// Replace the home character to set path
		path := common.ExpandHomeDir(conf.Sqlite.File)

		// Set DSN
		data.DB = new(data.SqliteBackend)
		data.DB.DSN(path)

		// Set up the database
		if err := data.DB.Setup(); err != nil {
			log.Fatalf("db: could not set up database: %s", err.Error())
		}

		// Verify database file exists and is ready
		if _, err := os.Stat(path); err != nil {
			log.Fatalf("db: database file does not exist: %s", conf.Sqlite.File)
		}

		// Open the database connection
		if err := data.DB.Open(); err != nil {
			log.Fatalf("db: could not open database: %s", err)
		}

		// TODO: temporary, create a test user
		data.NewUser("test", "test", data.RoleAdmin)
	} else {
		// Invalid config
		log.Fatalf("db: invalid database selected")
	}

	// Database set up, trigger manager that it's ready
	close(dbLaunchChan)

	// Trigger events via channel
	for {
		select {
		// Stop database manager
		case <-dbKillChan:
			// Close the database connection pool
			if err := data.DB.Close(); err != nil {
				log.Fatalf("db: could not close connection")
			}

			// Inform manager that shutdown is complete
			log.Println("db: stopped!")
			dbKillChan <- struct{}{}
			return
		}
	}
}
Exemplo n.º 2
0
// DSN sets the Path for use with sqlite3
func (s *SqliteBackend) DSN(path string) {
	// Replace the home character to set path
	s.Path = common.ExpandHomeDir(path)
}
Exemplo n.º 3
0
// Media returns the media folder from config, but with special
// characters such as '~' replaced, and any trailing slashes trimmed.
func (c Config) Media() string {
	// Return path with strings replaced, trailing slash removed,
	// tilde replaced with current user's home directory
	return path.Clean(common.ExpandHomeDir(c.MediaFolder))
}