gosora/router_gen/main.go

175 lines
4.3 KiB
Go
Raw Normal View History

/* WIP Under Construction */
Happy 100th commit! For the 100th commit, I've revamped a good portion of the user interface, and we don't plan on stopping! There's more to come! Each theme has more of a distinct feel in the control panel now. I moved a large portion of the inline CSS into global.css, tweaked some core code which didn't line up, and added CSS text insertions to make things more flexible. Revamped the alerts interface. We have more changes coming up! Added screenshots for Tempra Cursive and Tempra Conflux Mobile to the README. Added a .htaccess file, just in case someone plops Gosora in Apache's /www/ folder to stop the contents of config.go from becoming publically visible. Never put Gosora in your /www/ folder, Gosora is a standalone program which does NOT use Apache or any other webserver. Fixed a bug in the inline forum editor. Added a hand-written Markdown parser which is much faster than the previous Regex based one. This is still a work in progress. Added support for strikethrough and underline elements to the Markdown parser. Fixed the missing semicolons in global.js Revamped the form CSS. Author bits on the theme manager now link to the author's website. Improved the profiles a little. The code in the stylesheets now have a more consistent style. Fixed a bug in the Cosmo theme relating to the fact that Gosora doesn't have sidebars yet. There are many more changes which aren't listed here. If weirdness regarding lines or spacing occurs, I'm experimenting with a new text editor. I hope to have this fixed by the next commit.
2017-05-29 14:52:37 +00:00
// The router generator might be discontinued in favour of syncmaps in Go 1.9, it will be temporarily used for a couple of months as a lockless alternative to maps
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
Happy 100th commit! For the 100th commit, I've revamped a good portion of the user interface, and we don't plan on stopping! There's more to come! Each theme has more of a distinct feel in the control panel now. I moved a large portion of the inline CSS into global.css, tweaked some core code which didn't line up, and added CSS text insertions to make things more flexible. Revamped the alerts interface. We have more changes coming up! Added screenshots for Tempra Cursive and Tempra Conflux Mobile to the README. Added a .htaccess file, just in case someone plops Gosora in Apache's /www/ folder to stop the contents of config.go from becoming publically visible. Never put Gosora in your /www/ folder, Gosora is a standalone program which does NOT use Apache or any other webserver. Fixed a bug in the inline forum editor. Added a hand-written Markdown parser which is much faster than the previous Regex based one. This is still a work in progress. Added support for strikethrough and underline elements to the Markdown parser. Fixed the missing semicolons in global.js Revamped the form CSS. Author bits on the theme manager now link to the author's website. Improved the profiles a little. The code in the stylesheets now have a more consistent style. Fixed a bug in the Cosmo theme relating to the fact that Gosora doesn't have sidebars yet. There are many more changes which aren't listed here. If weirdness regarding lines or spacing occurs, I'm experimenting with a new text editor. I hope to have this fixed by the next commit.
2017-05-29 14:52:37 +00:00
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// The router generator might be discontinued in favour of syncmaps in Go 1.9, it will be temporarily used for a couple of months as a lockless alternative to maps\n"
for _, route := range route_list {
var end int
if route.Path[len(route.Path)-1] == '/' {
end = len(route.Path) - 1
} else {
end = len(route.Path) - 1
}
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
if route.Before != "" {
out += "\n\t\t\t" + route.Before
}
out += "\n\t\t\t" + route.Name + "(w,req"
for _, item := range route.Vars {
out += "," + item
}
out += ")\n\t\t\treturn"
}
for _, group := range route_groups {
var end int
if group.Path[len(group.Path)-1] == '/' {
end = len(group.Path) - 1
} else {
end = len(group.Path) - 1
}
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
}
out += "\n\t\t\t\t\t" + route.Name + "(w,req"
for _, item := range route.Vars {
out += "," + item
}
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}"
}
fdata += `package main
//import "fmt"
import "sync"
import "strings"
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(uploads http.Handler) *GenRouter {
return &GenRouter{
UploadHandler: http.StripPrefix("/uploads/",uploads).ServeHTTP,
old_routes: make(map[string]func(http.ResponseWriter, *http.Request)),
}
}
func (router *GenRouter) Handle(_ string, _ http.Handler) {
}
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 == "/" {
// default_route(w,req)
// return
//}
if 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]
}
//fmt.Println("prefix:",prefix)
//fmt.Println("req.URL.Path:",req.URL.Path)
//fmt.Println("extra_data:",extra_data)
switch(prefix) {` + out + `
case "/uploads":
if extra_data == "" {
NotFound(w,req)
return
}
req.URL.Path += extra_data
router.UploadHandler(w,req)
return
case "":
default_route(w,req)
return
//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]
router.RUnlock()
if ok {
req.URL.Path += extra_data
handle(w,req)
return
}
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()
}