Made the default routes in the route groups a little more efficient.

Added the Forum Manager routes to the generated route list.
run.bat now generates a router for you. I'll work on the Linux equivalents later.
This commit is contained in:
Azareal 2017-04-13 11:55:51 +01:00
parent c045b456b4
commit ade818b1b2
6 changed files with 83 additions and 20 deletions

View File

@ -65,12 +65,36 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
case "/topics":
switch(req.URL.Path) {
case "/topics/":
route_topics(w,req)
return
case "/topics/create/":
route_topic_create(w,req, extra_data)
return
default:
route_topics(w,req)
return
}
case "/panel":
switch(req.URL.Path) {
case "/panel/forums/":
route_panel_forums(w,req)
return
case "/panel/forums/create/":
route_panel_forums_create_submit(w,req)
return
case "/panel/forums/delete/":
route_panel_forums_delete(w,req, extra_data)
return
case "/panel/forums/delete/submit/":
route_panel_forums_delete_submit(w,req, extra_data)
return
case "/panel/forums/edit/":
route_panel_forums_edit(w,req, extra_data)
return
case "/panel/forums/edit/submit/":
route_panel_forums_edit_submit(w,req, extra_data)
return
default:
route_panel(w,req)
return
}
case "/uploads":
if extra_data == "" {

16
main.go
View File

@ -239,14 +239,14 @@ func main(){
router.HandleFunc("/users/unban/", route_unban)
router.HandleFunc("/users/activate/", route_activate)
// Admin
router.HandleFunc("/panel/", route_panel)
router.HandleFunc("/panel/forums/", route_panel_forums)
router.HandleFunc("/panel/forums/create/", route_panel_forums_create_submit)
router.HandleFunc("/panel/forums/delete/", route_panel_forums_delete)
router.HandleFunc("/panel/forums/delete/submit/", route_panel_forums_delete_submit)
router.HandleFunc("/panel/forums/edit/", route_panel_forums_edit)
router.HandleFunc("/panel/forums/edit/submit/", route_panel_forums_edit_submit)
// The Control Panel
///router.HandleFunc("/panel/", route_panel)
///router.HandleFunc("/panel/forums/", route_panel_forums)
///router.HandleFunc("/panel/forums/create/", route_panel_forums_create_submit)
///router.HandleFunc("/panel/forums/delete/", route_panel_forums_delete)
///router.HandleFunc("/panel/forums/delete/submit/", route_panel_forums_delete_submit)
///router.HandleFunc("/panel/forums/edit/", route_panel_forums_edit)
///router.HandleFunc("/panel/forums/edit/submit/", route_panel_forums_edit_submit)
router.HandleFunc("/panel/settings/", route_panel_settings)
router.HandleFunc("/panel/settings/edit/", route_panel_setting)
router.HandleFunc("/panel/settings/edit/submit/", route_panel_setting_edit)

View File

@ -85,7 +85,7 @@ func route_panel_forums_create_submit(w http.ResponseWriter, r *http.Request){
http.Redirect(w,r,"/panel/forums/",http.StatusSeeOther)
}
func route_panel_forums_delete(w http.ResponseWriter, r *http.Request){
func route_panel_forums_delete(w http.ResponseWriter, r *http.Request, sfid string){
user, noticeList, ok := SessionCheck(w,r)
if !ok {
return
@ -99,7 +99,7 @@ func route_panel_forums_delete(w http.ResponseWriter, r *http.Request){
return
}
fid, err := strconv.Atoi(r.URL.Path[len("/panel/forums/delete/"):])
fid, err := strconv.Atoi(sfid)
if err != nil {
LocalError("The provided Forum ID is not a valid number.",w,r,user)
return
@ -117,7 +117,7 @@ func route_panel_forums_delete(w http.ResponseWriter, r *http.Request){
templates.ExecuteTemplate(w,"areyousure.html",pi)
}
func route_panel_forums_delete_submit(w http.ResponseWriter, r *http.Request) {
func route_panel_forums_delete_submit(w http.ResponseWriter, r *http.Request, sfid string) {
user, ok := SimpleSessionCheck(w,r)
if !ok {
return
@ -131,7 +131,7 @@ func route_panel_forums_delete_submit(w http.ResponseWriter, r *http.Request) {
return
}
fid, err := strconv.Atoi(r.URL.Path[len("/panel/forums/delete/submit/"):])
fid, err := strconv.Atoi(sfid)
if err != nil {
LocalError("The provided Forum ID is not a valid number.",w,r,user)
return
@ -149,7 +149,7 @@ func route_panel_forums_delete_submit(w http.ResponseWriter, r *http.Request) {
http.Redirect(w,r,"/panel/forums/",http.StatusSeeOther)
}
func route_panel_forums_edit(w http.ResponseWriter, r *http.Request) {
func route_panel_forums_edit(w http.ResponseWriter, r *http.Request, sfid string) {
user, noticeList, ok := SessionCheck(w,r)
if !ok {
return
@ -159,7 +159,7 @@ func route_panel_forums_edit(w http.ResponseWriter, r *http.Request) {
return
}
fid, err := strconv.Atoi(r.URL.Path[len("/panel/forums/edit/"):])
fid, err := strconv.Atoi(sfid)
if err != nil {
LocalError("The provided Forum ID is not a valid number.",w,r,user)
return
@ -173,7 +173,7 @@ func route_panel_forums_edit(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w,"panel-forum-edit.html",pi)
}
func route_panel_forums_edit_submit(w http.ResponseWriter, r *http.Request) {
func route_panel_forums_edit_submit(w http.ResponseWriter, r *http.Request, sfid string) {
user, ok := SimpleSessionCheck(w,r)
if !ok {
return
@ -197,7 +197,7 @@ func route_panel_forums_edit_submit(w http.ResponseWriter, r *http.Request) {
is_js = "0"
}
fid, err := strconv.Atoi(r.URL.Path[len("/panel/forums/edit/submit/"):])
fid, err := strconv.Atoi(sfid)
if err != nil {
LocalErrorJSQ("The provided Forum ID is not a valid number.",w,r,user,is_js)
return

View File

@ -46,7 +46,13 @@ func main() {
out += `
case "` + group.Path[0:end] + `":
switch(req.URL.Path) {`
var default_route Route
for _, route := range group.Routes {
if group.Path == route.Path {
default_route = route
continue
}
out += "\n\t\t\t\tcase \"" + route.Path + "\":"
if route.Before != "" {
out += "\n\t\t\t\t\t" + route.Before
@ -57,6 +63,18 @@ func main() {
}
out += ")\n\t\t\t\t\treturn"
}
if default_route.Name != "" {
out += "\n\t\t\t\tdefault:"
if default_route.Before != "" {
out += "\n\t\t\t\t\t" + default_route.Before
}
out += "\n\t\t\t\t\t" + default_route.Name + "(w,req"
for _, item := range default_route.Vars {
out += ", " + item
}
out += ")\n\t\t\t\t\treturn"
}
out += "\n\t\t\t}"
}
@ -137,7 +155,7 @@ func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
NotFound(w,req)
}
`
write_file("../gen_router.go",fdata)
write_file("./gen_router.go",fdata)
fmt.Println("Successfully generated the router")
}

View File

@ -33,4 +33,15 @@ func routes() {
Route{"route_topics","/topics/","",[]string{}},
Route{"route_topic_create","/topics/create/","",[]string{"extra_data"}},
)
// The Control Panel
addRouteGroup("/panel/",
Route{"route_panel","/panel/","",[]string{}},
Route{"route_panel_forums","/panel/forums/","",[]string{}},
Route{"route_panel_forums_create_submit","/panel/forums/create/","",[]string{}},
Route{"route_panel_forums_delete","/panel/forums/delete/","",[]string{"extra_data"}},
Route{"route_panel_forums_delete_submit","/panel/forums/delete/submit/","",[]string{"extra_data"}},
Route{"route_panel_forums_edit","/panel/forums/edit/","",[]string{"extra_data"}},
Route{"route_panel_forums_edit_submit","/panel/forums/edit/submit/","",[]string{"extra_data"}},
)
}

10
run.bat
View File

@ -6,6 +6,16 @@ if %errorlevel% neq 0 (
exit /b %errorlevel%
)
echo Building the router generator
go build ./router_gen
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Running the router generator
router_gen.exe
echo Building the executable
go build -o gosora.exe
if %errorlevel% neq 0 (