Last commit with the old router. The next one will only have the generated one!

This commit is contained in:
Azareal 2017-04-12 14:39:03 +01:00
parent a4ffc14a0c
commit 87cb08bde3
7 changed files with 123 additions and 62 deletions

68
gen_router.go Normal file
View File

@ -0,0 +1,68 @@
// 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 "sync"
import "net/http"
type GenRouter struct {
UploadHandler func(http.ResponseWriter, *http.Request)
sync.RWMutex // Temporary Fallback
old_routes map[string]func(http.ResponseWriter, *http.Request) // Temporary Fallback
}
func NewGenRouter() *GenRouter {
fs_u := http.FileServer(http.Dir("./uploads"))
return &GenRouter{
UploadHandler: http.StripPrefix("/uploads/",fs_u).ServeHTTP,
old_routes: make(map[string]func(http.ResponseWriter, *http.Request)),
}
}
func (router *GenRouter) Handle(pattern string, handle http.Handler) {
router.Lock()
router.old_routes[pattern] = handle.ServeHTTP
router.Unlock()
}
func (router *GenRouter) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
router.Lock()
router.old_routes[pattern] = handle
router.Unlock()
}
func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path[0] != '/' {
w.WriteHeader(405)
w.Write([]byte(""))
return
}
var extra_data string
/*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]
}*/
switch(req.URL.Path) {
case "/static/": route_static(w,req/*, req.URL.Path + extra_data*/)
case "/overview/": route_overview(w,req/**/)
case "/pages/": route_custom_page(w,req/*, &extra_data*/)
case "/topics/": route_topics(w,req/*, &groups, &forums*/)
case "/forums/": route_forums(w,req/*, &forums*/)
case "/uploads/": router.UploadHandler(w,req)
//default: NotFound(w,req)
}
// A fallback for the routes which haven't been converted to the new router yet
router.RLock()
handle, ok := router.old_routes[req.URL.Path]
if ok {
router.RUnlock()
req.URL.Path = req.URL.Path + extra_data
handle(w,req)
return
}
router.RUnlock()
NotFound(w,req)
}

View File

@ -194,7 +194,7 @@ func main(){
router.HandleFunc("/overview/", route_overview)// router.HandleFunc("/overview/", route_overview)//
router.HandleFunc("/topics/create/", route_topic_create) router.HandleFunc("/topics/create/", route_topic_create)
router.HandleFunc("/topics/", route_topics)// router.HandleFunc("/topics/", route_topics)//
router.HandleFunc("/forums/", route_forums) router.HandleFunc("/forums/", route_forums)//
router.HandleFunc("/forum/", route_forum) router.HandleFunc("/forum/", route_forum)
router.HandleFunc("/topic/create/submit/", route_create_topic) router.HandleFunc("/topic/create/submit/", route_create_topic)
router.HandleFunc("/topic/", route_topic_id) router.HandleFunc("/topic/", route_topic_id)

View File

@ -5,7 +5,7 @@ import "sync"
import "net/http" import "net/http"
type Router struct { type Router struct {
mu sync.RWMutex sync.RWMutex
routes map[string]func(http.ResponseWriter, *http.Request) routes map[string]func(http.ResponseWriter, *http.Request)
} }
@ -16,15 +16,15 @@ func NewRouter() *Router {
} }
func (router *Router) Handle(pattern string, handle http.Handler) { func (router *Router) Handle(pattern string, handle http.Handler) {
router.mu.Lock() router.Lock()
router.routes[pattern] = handle.ServeHTTP router.routes[pattern] = handle.ServeHTTP
router.mu.Unlock() router.Unlock()
} }
func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) { func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
router.mu.Lock() router.Lock()
router.routes[pattern] = handle router.routes[pattern] = handle
router.mu.Unlock() router.Unlock()
} }
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@ -42,16 +42,15 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
prefix = req.URL.Path prefix = req.URL.Path
} }
router.mu.RLock() router.RLock()
handle, ok := router.routes[prefix] handle, ok := router.routes[prefix]
if ok { if ok {
router.mu.RUnlock() router.RUnlock()
handle(w,req) handle(w,req)
return return
} }
//fmt.Println(req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]) //fmt.Println(req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')])
router.mu.RUnlock() router.RUnlock()
NotFound(w,req) NotFound(w,req)
return
} }

View File

@ -1,38 +0,0 @@
// 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 "net/http"
type GenRouter struct {
UploadHandler http.Handler
}
func NewGenRouter() *GenRouter {
fs_u := http.FileServer(http.Dir("./uploads"))
return &GenRouter{http.StripPrefix("/uploads/",fs_u).ServeHTTP}
}
func (router *GenRouter) Handle(_ string, _ http.Handler) {
}
func (router *GenRouter) HandleFunc(_ string, _ func(http.ResponseWriter, *http.Request)) {
}
func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var extra_data string
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]
}
switch(req.URL.Path) {
case "/overview/":
route_overview(w http.ResponseWriter, req *http.Request)
case "/pages/":
route_custom_page(w http.ResponseWriter, req *http.Request, &extra_data)
case "/topics/":
route_topics(w http.ResponseWriter, req *http.Request, &groups, &forums)
case "/uploads/": UploadHandler(w,req)
default: NotFound(w,req)
}
}

View File

@ -19,45 +19,76 @@ func main() {
var fdata string = "// Code generated by. DO NOT EDIT.\n/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n" var fdata string = "// Code generated by. DO NOT EDIT.\n/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n"
for _, route := range route_list { for _, route := range route_list {
out += "\n\t\tcase \"" + route.Path + "\":\n\t\t\t" + route.Name+ "(w http.ResponseWriter, req *http.Request" out += "\n\t\tcase \"" + route.Path + "\": " + route.Name+ "(w,req/*"
for _, item := range route.Vars { for _, item := range route.Vars {
out += ", " + item out += ", " + item
} }
out += ")" out += "*/)"
} }
fdata += `package main fdata += `package main
import "sync"
import "net/http" import "net/http"
type GenRouter struct { type GenRouter struct {
UploadHandler http.Handler UploadHandler func(http.ResponseWriter, *http.Request)
sync.RWMutex // Temporary Fallback
old_routes map[string]func(http.ResponseWriter, *http.Request) // Temporary Fallback
} }
func NewGenRouter() *GenRouter { func NewGenRouter() *GenRouter {
fs_u := http.FileServer(http.Dir("./uploads")) fs_u := http.FileServer(http.Dir("./uploads"))
return &GenRouter{http.StripPrefix("/uploads/",fs_u).ServeHTTP} return &GenRouter{
UploadHandler: http.StripPrefix("/uploads/",fs_u).ServeHTTP,
old_routes: make(map[string]func(http.ResponseWriter, *http.Request)),
}
} }
func (router *GenRouter) Handle(_ string, _ http.Handler) { func (router *GenRouter) Handle(pattern string, handle http.Handler) {
router.Lock()
router.old_routes[pattern] = handle.ServeHTTP
router.Unlock()
} }
func (router *GenRouter) HandleFunc(_ string, _ func(http.ResponseWriter, *http.Request)) { func (router *GenRouter) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
router.Lock()
router.old_routes[pattern] = handle
router.Unlock()
} }
func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (router *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path[0] != '/' {
w.WriteHeader(405)
w.Write([]byte(""))
return
}
var extra_data string var extra_data string
if req.URL.Path[len(req.URL.Path) - 1] != '/' { /*if req.URL.Path[len(req.URL.Path) - 1] != '/' {
extra_data = req.URL.Path[strings.LastIndexByte(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] req.URL.Path = req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/') + 1]
} }*/
switch(req.URL.Path) {` + out + ` switch(req.URL.Path) {` + out + `
case "/uploads/": UploadHandler(w,req) case "/uploads/": router.UploadHandler(w,req)
default: NotFound(w,req) //default: NotFound(w,req)
} }
// A fallback for the routes which haven't been converted to the new router yet
router.RLock()
handle, ok := router.old_routes[req.URL.Path]
if ok {
router.RUnlock()
req.URL.Path = req.URL.Path + extra_data
handle(w,req)
return
}
router.RUnlock()
NotFound(w,req)
} }
` `
write_file("./gen_router.go",fdata) write_file("../gen_router.go",fdata)
fmt.Println("Successfully generated the router") fmt.Println("Successfully generated the router")
} }

View File

@ -20,8 +20,9 @@ func addRouteGroup(path string, routes ...Route) {
} }
func routes() { func routes() {
//addRoute("route_static","/static/","&extra_data") addRoute("route_static","/static/","req.URL.Path + extra_data")
addRoute("route_overview","/overview/") addRoute("route_overview","/overview/")
addRoute("route_custom_page","/pages/","&extra_data") addRoute("route_custom_page","/pages/","&extra_data")
addRoute("route_topics","/topics/","&groups","&forums") addRoute("route_topics","/topics/","&groups","&forums")
addRoute("route_forums","/forums/","&forums")
} }

View File

@ -58,11 +58,11 @@ func route_static(w http.ResponseWriter, r *http.Request){
/*func route_exit(w http.ResponseWriter, r *http.Request){ /*func route_exit(w http.ResponseWriter, r *http.Request){
db.Close() db.Close()
os.Exit(0) os.Exit(0)
}*/ }
func route_fstatic(w http.ResponseWriter, r *http.Request){ func route_fstatic(w http.ResponseWriter, r *http.Request){
http.ServeFile(w,r,r.URL.Path) http.ServeFile(w,r,r.URL.Path)
} }*/
func route_overview(w http.ResponseWriter, r *http.Request){ func route_overview(w http.ResponseWriter, r *http.Request){
user, noticeList, ok := SessionCheck(w,r) user, noticeList, ok := SessionCheck(w,r)