示例#1
0
func addAdmin() error {
	// TODO: Parameter checking
	db, err := DBConn()
	if err != nil {
		return err
	}
	store := postgrestore.MustCreate(db)
	store.MustPrepareStmts()
	sessionStore := auth.NewCookieSessionStore(
		[]byte("new-authentication-key"),
		[]byte("new-encryption-key"))

	um := auth.NewUserManager(nil, store, nil, sessionStore)

	user, err := um.New(*adminName, *adminEmail, *adminPw, *adminPw, true, true)
	if err != nil {
		return err
	}

	err = um.Insert(user)
	if err != nil {
		return err
	}
	return nil
}
示例#2
0
func start() error {
	db, err := DBConn()
	if err != nil {
		return err
	}

	store := postgrestore.MustCreate(db)
	store.MustPrepareStmts()
	sessionStore := auth.NewCookieSessionStore(
		[]byte("newauthenticatio"),
		[]byte("newencryptionkey"))

	um := auth.NewUserManager(nil, store, nil, sessionStore)
	m := models.NewManager(store)

	e := &env.Env{
		m,
		um,
		env.Config{
			Port: *port,
		},
	}

	router := httprouter.New()
	// Main React route
	router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		http.ServeFile(w, r, "html/index.html")
	})

	// CSS, JS, etc
	router.ServeFiles("/static/*filepath", http.Dir("static"))

	// Admin Routes
	router.GET("/admin/users", CreateHandlerWithEnv(e, handlers.CreateAdminUsersGETHandler))
	router.POST("/admin/user", CreateHandlerWithEnv(e, handlers.CreateAdminUsersPOSTHandler))
	router.DELETE("/admin/user/:user_id", CreateHandlerWithEnv(e, handlers.CreateAdminUserDELETEHandler))

	router.POST("/auth/login", CreateHandlerWithEnv(e, handlers.CreateLoginHandler))
	router.GET("/auth/logout", CreateHandlerWithEnv(e, handlers.CreateLogoutHandler))
	router.POST("/auth/change_password", CreateHandlerWithEnv(e, handlers.CreateChangePasswordHandler))

	fmt.Println("Server started on port", e.Conf.Port)
	return http.ListenAndServe(fmt.Sprintf(":%d", e.Conf.Port), router)
}