Exemplo n.º 1
0
func initDB() {
	var err error
	db, err = simplebolt.New(boltFile)
	if err != nil {
		log.Fatalf("Could not create database! %s", err)
	}
	config, _ = simplebolt.NewKeyValue(db, "config")
	holes, _ = simplebolt.NewHashMap(db, "holes")
	apps, _ = simplebolt.NewSet(db, "apps")
	appNames, _ = simplebolt.NewKeyValue(db, "appnames")
	cookie, _ = config.Get("cookie")
}
Exemplo n.º 2
0
// Create a new *UserState that can be used for managing users.
// connectionString may be on the form "username:password@host:port/database".
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
func NewUserState(filename string, randomseed bool) (*UserState, error) {
	var err error

	db, err := simplebolt.New(filename)
	if err != nil {
		return nil, err
	}

	state := new(UserState)

	state.users, err = simplebolt.NewHashMap(db, "users")
	if err != nil {
		return nil, err
	}
	state.usernames, err = simplebolt.NewSet(db, "usernames")
	if err != nil {
		return nil, err
	}
	state.unconfirmed, err = simplebolt.NewSet(db, "unconfirmed")
	if err != nil {
		return nil, err
	}

	state.db = db

	// For the secure cookies
	// This must happen before the random seeding, or
	// else people will have to log in again after every server restart
	state.cookieSecret = cookie.RandomCookieFriendlyString(30)

	// Seed the random number generator
	if randomseed {
		rand.Seed(time.Now().UnixNano())
	}

	// Cookies lasts for 24 hours by default. Specified in seconds.
	state.cookieTime = 3600 * 24

	// Default password hashing algorithm is "bcrypt+", which is the same as
	// "bcrypt", but with backwards compatibility for checking sha256 hashes.
	state.passwordAlgorithm = "bcrypt+" // "bcrypt+", "bcrypt" or "sha256"

	return state, nil
}