3fa9bf7373
Refactored the router. Added the MemberOnly middleware and tests for it.
363 lines
9.9 KiB
Go
363 lines
9.9 KiB
Go
// Code generated by. DO NOT EDIT.
|
|
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
|
|
package main
|
|
|
|
import "log"
|
|
import "strings"
|
|
import "sync"
|
|
import "errors"
|
|
import "net/http"
|
|
|
|
var ErrNoRoute = errors.New("That route doesn't exist.")
|
|
|
|
type GenRouter struct {
|
|
UploadHandler func(http.ResponseWriter, *http.Request)
|
|
extra_routes map[string]func(http.ResponseWriter, *http.Request, User) RouteError
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
func NewGenRouter(uploads http.Handler) *GenRouter {
|
|
return &GenRouter{
|
|
UploadHandler: http.StripPrefix("/uploads/",uploads).ServeHTTP,
|
|
extra_routes: make(map[string]func(http.ResponseWriter, *http.Request, User) RouteError),
|
|
}
|
|
}
|
|
|
|
func (router *GenRouter) handleError(err RouteError, w http.ResponseWriter, r *http.Request, user User) {
|
|
if err.Handled() {
|
|
return
|
|
}
|
|
|
|
if err.Type() == "system" {
|
|
InternalErrorJSQ(err,w,r,err.Json())
|
|
return
|
|
}
|
|
LocalErrorJSQ(err.Error(),w,r,user,err.Json())
|
|
}
|
|
|
|
func (router *GenRouter) Handle(_ string, _ http.Handler) {
|
|
}
|
|
|
|
func (router *GenRouter) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request, User) RouteError) {
|
|
router.Lock()
|
|
router.extra_routes[pattern] = handle
|
|
router.Unlock()
|
|
}
|
|
|
|
func (router *GenRouter) RemoveFunc(pattern string) error {
|
|
router.Lock()
|
|
_, ok := router.extra_routes[pattern]
|
|
if !ok {
|
|
router.Unlock()
|
|
return ErrNoRoute
|
|
}
|
|
delete(router.extra_routes,pattern)
|
|
router.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
//if req.URL.Path == "/" {
|
|
// default_route(w,req)
|
|
// return
|
|
//}
|
|
if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' {
|
|
w.WriteHeader(405)
|
|
w.Write([]byte(""))
|
|
return
|
|
}
|
|
|
|
var prefix, extra_data string
|
|
prefix = req.URL.Path[0:strings.IndexByte(req.URL.Path[1:],'/') + 1]
|
|
if req.URL.Path[len(req.URL.Path) - 1] != '/' {
|
|
extra_data = req.URL.Path[strings.LastIndexByte(req.URL.Path,'/') + 1:]
|
|
req.URL.Path = req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/') + 1]
|
|
}
|
|
|
|
if dev.SuperDebug {
|
|
log.Print("before routeStatic")
|
|
log.Print("prefix: ", prefix)
|
|
log.Print("req.URL.Path: ", req.URL.Path)
|
|
log.Print("extra_data: ", extra_data)
|
|
log.Print("req.Referer(): ", req.Referer())
|
|
}
|
|
|
|
if prefix == "/static" {
|
|
req.URL.Path += extra_data
|
|
routeStatic(w,req)
|
|
return
|
|
}
|
|
|
|
if dev.SuperDebug {
|
|
log.Print("before PreRoute")
|
|
}
|
|
|
|
// Deal with the session stuff, etc.
|
|
user, ok := PreRoute(w,req)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if dev.SuperDebug {
|
|
log.Print("after PreRoute")
|
|
}
|
|
|
|
var err RouteError
|
|
switch(prefix) {
|
|
case "/api":
|
|
err = routeAPI(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/overview":
|
|
err = routeOverview(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/forums":
|
|
err = routeForums(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/forum":
|
|
err = routeForum(w,req,user,extra_data)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/theme":
|
|
err = routeChangeTheme(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/attachs":
|
|
err = routeShowAttachment(w,req,user,extra_data)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/report":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
switch(req.URL.Path) {
|
|
case "/report/submit/":
|
|
err = routeReportSubmit(w,req,user,extra_data)
|
|
}
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/topics":
|
|
switch(req.URL.Path) {
|
|
case "/topics/create/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeTopicCreate(w,req,user,extra_data)
|
|
default:
|
|
err = routeTopics(w,req,user)
|
|
}
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/panel":
|
|
err = SuperModOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
switch(req.URL.Path) {
|
|
case "/panel/forums/":
|
|
err = routePanelForums(w,req,user)
|
|
case "/panel/forums/create/":
|
|
err = routePanelForumsCreateSubmit(w,req,user)
|
|
case "/panel/forums/delete/":
|
|
err = routePanelForumsDelete(w,req,user,extra_data)
|
|
case "/panel/forums/delete/submit/":
|
|
err = routePanelForumsDeleteSubmit(w,req,user,extra_data)
|
|
case "/panel/forums/edit/":
|
|
err = routePanelForumsEdit(w,req,user,extra_data)
|
|
case "/panel/forums/edit/submit/":
|
|
err = routePanelForumsEditSubmit(w,req,user,extra_data)
|
|
case "/panel/forums/edit/perms/submit/":
|
|
err = routePanelForumsEditPermsSubmit(w,req,user,extra_data)
|
|
case "/panel/settings/":
|
|
err = routePanelSettings(w,req,user)
|
|
case "/panel/settings/edit/":
|
|
err = routePanelSetting(w,req,user,extra_data)
|
|
case "/panel/settings/edit/submit/":
|
|
err = routePanelSettingEdit(w,req,user,extra_data)
|
|
case "/panel/settings/word-filters/":
|
|
err = routePanelWordFilters(w,req,user)
|
|
case "/panel/settings/word-filters/create/":
|
|
err = routePanelWordFiltersCreate(w,req,user)
|
|
case "/panel/settings/word-filters/edit/":
|
|
err = routePanelWordFiltersEdit(w,req,user,extra_data)
|
|
case "/panel/settings/word-filters/edit/submit/":
|
|
err = routePanelWordFiltersEditSubmit(w,req,user,extra_data)
|
|
case "/panel/settings/word-filters/delete/submit/":
|
|
err = routePanelWordFiltersDeleteSubmit(w,req,user,extra_data)
|
|
case "/panel/themes/":
|
|
err = routePanelThemes(w,req,user)
|
|
case "/panel/themes/default/":
|
|
err = routePanelThemesSetDefault(w,req,user,extra_data)
|
|
case "/panel/plugins/":
|
|
err = routePanelPlugins(w,req,user)
|
|
case "/panel/plugins/activate/":
|
|
err = routePanelPluginsActivate(w,req,user,extra_data)
|
|
case "/panel/plugins/deactivate/":
|
|
err = routePanelPluginsDeactivate(w,req,user,extra_data)
|
|
case "/panel/plugins/install/":
|
|
err = routePanelPluginsInstall(w,req,user,extra_data)
|
|
case "/panel/users/":
|
|
err = routePanelUsers(w,req,user)
|
|
case "/panel/users/edit/":
|
|
err = routePanelUsersEdit(w,req,user,extra_data)
|
|
case "/panel/users/edit/submit/":
|
|
err = routePanelUsersEditSubmit(w,req,user,extra_data)
|
|
case "/panel/groups/":
|
|
err = routePanelGroups(w,req,user)
|
|
case "/panel/groups/edit/":
|
|
err = routePanelGroupsEdit(w,req,user,extra_data)
|
|
case "/panel/groups/edit/perms/":
|
|
err = routePanelGroupsEditPerms(w,req,user,extra_data)
|
|
case "/panel/groups/edit/submit/":
|
|
err = routePanelGroupsEditSubmit(w,req,user,extra_data)
|
|
case "/panel/groups/edit/perms/submit/":
|
|
err = routePanelGroupsEditPermsSubmit(w,req,user,extra_data)
|
|
case "/panel/groups/create/":
|
|
err = routePanelGroupsCreateSubmit(w,req,user)
|
|
case "/panel/backups/":
|
|
err = routePanelBackups(w,req,user,extra_data)
|
|
case "/panel/logs/mod/":
|
|
err = routePanelLogsMod(w,req,user)
|
|
case "/panel/debug/":
|
|
err = routePanelDebug(w,req,user)
|
|
default:
|
|
err = routePanel(w,req,user)
|
|
}
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/user":
|
|
switch(req.URL.Path) {
|
|
case "/user/edit/critical/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditCritical(w,req,user)
|
|
case "/user/edit/critical/submit/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditCriticalSubmit(w,req,user)
|
|
case "/user/edit/avatar/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditAvatar(w,req,user)
|
|
case "/user/edit/avatar/submit/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditAvatarSubmit(w,req,user)
|
|
case "/user/edit/username/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditUsername(w,req,user)
|
|
case "/user/edit/username/submit/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditUsernameSubmit(w,req,user)
|
|
case "/user/edit/email/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditEmail(w,req,user)
|
|
case "/user/edit/token/":
|
|
err = MemberOnly(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
return
|
|
}
|
|
|
|
err = routeAccountOwnEditEmailTokenSubmit(w,req,user,extra_data)
|
|
default:
|
|
req.URL.Path += extra_data
|
|
err = routeProfile(w,req,user)
|
|
}
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
case "/uploads":
|
|
if extra_data == "" {
|
|
NotFound(w,req)
|
|
return
|
|
}
|
|
req.URL.Path += extra_data
|
|
// TODO: Find a way to propagate errors up from this?
|
|
router.UploadHandler(w,req)
|
|
case "":
|
|
// Stop the favicons, robots.txt file, etc. resolving to the topics list
|
|
// TODO: Add support for favicons and robots.txt files
|
|
switch(extra_data) {
|
|
case "robots.txt":
|
|
err = routeRobotsTxt(w,req)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
return
|
|
}
|
|
|
|
if extra_data != "" {
|
|
NotFound(w,req)
|
|
return
|
|
}
|
|
config.DefaultRoute(w,req,user)
|
|
default:
|
|
// A fallback for the routes which haven't been converted to the new router yet or plugins
|
|
router.RLock()
|
|
handle, ok := router.extra_routes[req.URL.Path]
|
|
router.RUnlock()
|
|
|
|
if ok {
|
|
req.URL.Path += extra_data
|
|
err = handle(w,req,user)
|
|
if err != nil {
|
|
router.handleError(err,w,req,user)
|
|
}
|
|
return
|
|
}
|
|
NotFound(w,req)
|
|
}
|
|
}
|