From 87cb08bde31c87bcc3dc73b03a02ced60a134648 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 12 Apr 2017 14:39:03 +0100 Subject: [PATCH] Last commit with the old router. The next one will only have the generated one! --- gen_router.go | 68 ++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- router.go | 17 +++++----- router_gen/gen_router.go | 38 ---------------------- router_gen/main.go | 53 ++++++++++++++++++++++++------- router_gen/routes.go | 3 +- routes.go | 4 +-- 7 files changed, 123 insertions(+), 62 deletions(-) create mode 100644 gen_router.go delete mode 100644 router_gen/gen_router.go diff --git a/gen_router.go b/gen_router.go new file mode 100644 index 00000000..9981a17b --- /dev/null +++ b/gen_router.go @@ -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) +} diff --git a/main.go b/main.go index 0d5947c6..370b2409 100644 --- a/main.go +++ b/main.go @@ -194,7 +194,7 @@ func main(){ router.HandleFunc("/overview/", route_overview)// router.HandleFunc("/topics/create/", route_topic_create) router.HandleFunc("/topics/", route_topics)// - router.HandleFunc("/forums/", route_forums) + router.HandleFunc("/forums/", route_forums)// router.HandleFunc("/forum/", route_forum) router.HandleFunc("/topic/create/submit/", route_create_topic) router.HandleFunc("/topic/", route_topic_id) diff --git a/router.go b/router.go index ed1c3b15..4853ff66 100644 --- a/router.go +++ b/router.go @@ -5,7 +5,7 @@ import "sync" import "net/http" type Router struct { - mu sync.RWMutex + sync.RWMutex routes map[string]func(http.ResponseWriter, *http.Request) } @@ -16,15 +16,15 @@ func NewRouter() *Router { } func (router *Router) Handle(pattern string, handle http.Handler) { - router.mu.Lock() + router.Lock() router.routes[pattern] = handle.ServeHTTP - router.mu.Unlock() + router.Unlock() } func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) { - router.mu.Lock() + router.Lock() router.routes[pattern] = handle - router.mu.Unlock() + router.Unlock() } 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 } - router.mu.RLock() + router.RLock() handle, ok := router.routes[prefix] if ok { - router.mu.RUnlock() + router.RUnlock() handle(w,req) return } //fmt.Println(req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]) - router.mu.RUnlock() + router.RUnlock() NotFound(w,req) - return } diff --git a/router_gen/gen_router.go b/router_gen/gen_router.go deleted file mode 100644 index ec80f2cb..00000000 --- a/router_gen/gen_router.go +++ /dev/null @@ -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) - } -} diff --git a/router_gen/main.go b/router_gen/main.go index 054a5b1a..d26d1ff8 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -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" 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 { out += ", " + item } - out += ")" + out += "*/)" } fdata += `package main +import "sync" import "net/http" 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 { 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) { + 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] != '/' { + /*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) {` + out + ` - case "/uploads/": UploadHandler(w,req) - default: NotFound(w,req) + 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) } ` - write_file("./gen_router.go",fdata) + write_file("../gen_router.go",fdata) fmt.Println("Successfully generated the router") } diff --git a/router_gen/routes.go b/router_gen/routes.go index 8eb5652d..45dc0992 100644 --- a/router_gen/routes.go +++ b/router_gen/routes.go @@ -20,8 +20,9 @@ func addRouteGroup(path string, routes ...Route) { } func routes() { - //addRoute("route_static","/static/","&extra_data") + addRoute("route_static","/static/","req.URL.Path + extra_data") addRoute("route_overview","/overview/") addRoute("route_custom_page","/pages/","&extra_data") addRoute("route_topics","/topics/","&groups","&forums") + addRoute("route_forums","/forums/","&forums") } diff --git a/routes.go b/routes.go index 5ea29e6e..35227bf9 100644 --- a/routes.go +++ b/routes.go @@ -58,11 +58,11 @@ func route_static(w http.ResponseWriter, r *http.Request){ /*func route_exit(w http.ResponseWriter, r *http.Request){ db.Close() os.Exit(0) -}*/ +} func route_fstatic(w http.ResponseWriter, r *http.Request){ http.ServeFile(w,r,r.URL.Path) -} +}*/ func route_overview(w http.ResponseWriter, r *http.Request){ user, noticeList, ok := SessionCheck(w,r)