gosora/router_gen/main.go
Azareal d7cf48e43d Router Generator WIP
Added a test for one of the router algorithms.
Added the ViewAdminLogs permission. Adminlogs are coming soon!
The empty comments next to HandleFuncs on main.go are there to remind me about which routes I've added to the router generator route list.
Fixed the Modlogs Page so that you can view it without the ManageThemes permissions. I accidentally used the wrong permission there before.
Changed the router algorithm to align better with the upcoming generated router. It's faster too.
2017-04-12 11:10:36 +01:00

76 lines
1.8 KiB
Go

/* WIP Under Construction */
package main
import "log"
import "fmt"
//import "strings"
import "os"
var route_list []Route
var route_groups []RouteGroup
func main() {
fmt.Println("Generating the router...")
// Load all the routes...
routes()
var out string
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"
for _, item := range route.Vars {
out += ", " + item
}
out += ")"
}
fdata += `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) {` + out + `
case "/uploads/": UploadHandler(w,req)
default: NotFound(w,req)
}
}
`
write_file("./gen_router.go",fdata)
fmt.Println("Successfully generated the router")
}
func write_file(name string, content string) {
f, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
log.Fatal(err)
}
f.Sync()
f.Close()
}