Exemple #1
0
func main() {
	// Get the config file
	var config_path string
	flag.StringVar(&config_path, "config", "gobb.conf", "Specifies the location of a config file")
	flag.Parse()
	config.GetConfig(config_path)

	// URL Routing!
	r := mux.NewRouter()

	r.HandleFunc("/", controllers.Index)
	r.HandleFunc("/register", controllers.Register)
	r.HandleFunc("/login", controllers.Login)
	r.HandleFunc("/logout", controllers.Logout)
	r.HandleFunc("/admin", controllers.Admin)
	r.HandleFunc("/board/{id:[0-9]+}", controllers.Board)
	r.HandleFunc("/board/{id:[0-9]+}/new", controllers.NewThread)
	r.HandleFunc("/board/{board_id:[0-9]+}/{post_id:[0-9]+}", controllers.Thread)
	r.HandleFunc("/user/{id:[0-9]+}/settings", controllers.UserSettings)
	r.PathPrefix("/static/").Handler(http.FileServer(http.Dir("./")))
	http.Handle("/", r)

	fmt.Println("Starting server on port 8080")
	http.ListenAndServe(":8080", nil)
}
Exemple #2
0
func main() {
	// Get the config file
	var config_path string
	flag.StringVar(&config_path, "config", "gobb.conf", "Specifies the location of a config file")
	run_migrations := flag.Bool("migrate", false, "Runs database migrations")
	ign_migrations := flag.Bool("ignore-migrations", false, "Ignores an out of date database and runs the server anyways")
	serve := flag.Bool("serve", false, "run server")
	useradd := flag.Bool("add-user", false, "add a user")
	var name, password string
	flag.StringVar(&name, "name", "", "username to add")
	flag.StringVar(&password, "password", "", "password new user")
	group := flag.Int64("group", 0, "group of new user (<0 is special group, 0 is default, 1 is mod, 2 is admin)")
	flag.Parse()
	config.GetConfig(config_path)

	// Do we need to run migrations?
	latest_db_version, migrations, err := utils.GetMigrationInfo()
	if len(migrations) != 0 && *run_migrations {
		fmt.Println("[notice] Running database migrations:\n")
		err = utils.RunMigrations(latest_db_version)
		if err != nil {
			fmt.Printf("[error] Could not run migrations (%s)\n", err.Error())
			return
		}

		fmt.Println("\n[notice] Database migration successful!")
	} else if len(migrations) != 0 && !(*ign_migrations) {
		fmt.Println("Your database appears to be out of date. Please run migrations with --migrate or ignore this message with --ignore-migrations")
		return
	}
	db := models.GetDbSession()

	if *useradd {
		user := models.NewUser(name, password)
		user.GroupId = *group
		err = db.Insert(user)

	}

	if !*serve {
		return
	}
	// URL Routing!
	r := mux.NewRouter()
	r.StrictSlash(true)

	r.HandleFunc("/", controllers.Index)
	r.HandleFunc("/register", controllers.Register)
	r.HandleFunc("/login", controllers.Login)
	r.HandleFunc("/logout", controllers.Logout)
	r.HandleFunc("/admin", controllers.Admin)
	r.HandleFunc("/admin/boards", controllers.AdminBoards)
	r.HandleFunc("/admin/users/{id:[0-9]+}", controllers.AdminUser)
	r.HandleFunc("/admin/users", controllers.AdminUsers)
	r.HandleFunc("/action/stick", controllers.ActionStickThread)
	r.HandleFunc("/action/lock", controllers.ActionLockThread)
	r.HandleFunc("/action/delete", controllers.ActionDeleteThread)
	r.HandleFunc("/action/move", controllers.ActionMoveThread)
	r.HandleFunc("/action/mark_read", controllers.ActionMarkAllRead)
	r.HandleFunc("/action/edit", controllers.PostEditor)
	r.HandleFunc("/board/{id:[0-9]+}", controllers.Board)
	r.HandleFunc("/board/{board_id:[0-9]+}/new", controllers.PostEditor)
	r.HandleFunc("/board/{board_id:[0-9]+}/{post_id:[0-9]+}", controllers.Thread)
	r.HandleFunc("/user/{id:[0-9]+}", controllers.User)
	r.HandleFunc("/user/{id:[0-9]+}/settings", controllers.UserSettings)

	// Handle static files
	selected_template, _ := models.GetStringSetting("template")
	base_path, _ := config.Config.GetString("gobb", "base_path")
	if selected_template == "default" {
		pkg, _ := build.Import("github.com/stevenleeg/gobb/gobb", ".", build.FindOnly)
		static_path := filepath.Join(pkg.SrcRoot, pkg.ImportPath, "../templates")
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	} else {
		static_path := filepath.Join(base_path, "templates", selected_template)
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	}

	// User provided static files
	static_path, err := config.Config.GetString("gobb", "base_path")
	if err == nil {
		r.PathPrefix("/assets/").Handler(http.FileServer(http.Dir(static_path)))
	}

	http.Handle("/", r)

	port, err := config.Config.GetString("gobb", "port")
	fmt.Println("[notice] Starting server on port " + port)
	http.ListenAndServe(":"+port, nil)
}