2017-04-12 10:10:36 +00:00
/* 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
2017-06-05 11:57:27 +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"
2017-04-12 10:10:36 +00:00
for _ , route := range route_list {
2017-04-13 09:26:40 +00:00
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"
2017-04-12 10:10:36 +00:00
for _ , item := range route . Vars {
2017-04-13 15:01:30 +00:00
out += "," + item
2017-04-12 10:10:36 +00:00
}
2017-04-13 09:26:40 +00:00
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 ) { `
2017-04-13 10:55:51 +00:00
var default_route Route
2017-04-13 09:26:40 +00:00
for _ , route := range group . Routes {
2017-04-13 10:55:51 +00:00
if group . Path == route . Path {
default_route = route
continue
}
2017-04-13 09:26:40 +00:00
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 {
2017-04-13 15:01:30 +00:00
out += "," + item
2017-04-13 09:26:40 +00:00
}
out += ")\n\t\t\t\t\treturn"
}
2017-04-13 10:55:51 +00:00
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"
}
2017-04-13 09:26:40 +00:00
out += "\n\t\t\t}"
2017-04-12 10:10:36 +00:00
}
fdata += ` package main
2017-04-13 09:26:40 +00:00
//import "fmt"
2017-04-12 13:39:03 +00:00
import "sync"
2017-04-13 09:26:40 +00:00
import "strings"
2017-04-12 10:10:36 +00:00
import "net/http"
type GenRouter struct {
2017-04-12 13:39:03 +00:00
UploadHandler func ( http . ResponseWriter , * http . Request )
sync . RWMutex // Temporary Fallback
old_routes map [ string ] func ( http . ResponseWriter , * http . Request ) // Temporary Fallback
2017-04-12 10:10:36 +00:00
}
2017-04-13 09:26:40 +00:00
func NewGenRouter ( uploads http . Handler ) * GenRouter {
2017-04-12 13:39:03 +00:00
return & GenRouter {
2017-04-13 09:26:40 +00:00
UploadHandler : http . StripPrefix ( "/uploads/" , uploads ) . ServeHTTP ,
2017-04-12 13:39:03 +00:00
old_routes : make ( map [ string ] func ( http . ResponseWriter , * http . Request ) ) ,
}
2017-04-12 10:10:36 +00:00
}
2017-04-13 09:26:40 +00:00
func ( router * GenRouter ) Handle ( _ string , _ http . Handler ) {
2017-04-12 10:10:36 +00:00
}
2017-04-12 13:39:03 +00:00
func ( router * GenRouter ) HandleFunc ( pattern string , handle func ( http . ResponseWriter , * http . Request ) ) {
router . Lock ( )
router . old_routes [ pattern ] = handle
router . Unlock ( )
2017-04-12 10:10:36 +00:00
}
func ( router * GenRouter ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
2017-04-13 09:26:40 +00:00
//if req.URL.Path == "/" {
// default_route(w,req)
// return
//}
2017-04-12 13:39:03 +00:00
if req . URL . Path [ 0 ] != '/' {
w . WriteHeader ( 405 )
w . Write ( [ ] byte ( "" ) )
return
}
2017-04-13 09:26:40 +00:00
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 ] != '/' {
2017-04-12 10:10:36 +00:00
extra_data = req . URL . Path [ strings . LastIndexByte ( req . URL . Path , '/' ) + 1 : ]
req . URL . Path = req . URL . Path [ : strings . LastIndexByte ( req . URL . Path , '/' ) + 1 ]
2017-04-13 09:26:40 +00:00
}
//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
2017-04-12 13:39:03 +00:00
//default: NotFound(w,req)
2017-04-12 10:10:36 +00:00
}
2017-04-12 13:39:03 +00:00
// 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 ]
2017-04-13 09:26:40 +00:00
router . RUnlock ( )
2017-04-12 13:39:03 +00:00
if ok {
2017-04-13 09:26:40 +00:00
req . URL . Path += extra_data
2017-04-12 13:39:03 +00:00
handle ( w , req )
return
}
NotFound ( w , req )
2017-04-12 10:10:36 +00:00
}
`
2017-04-13 10:55:51 +00:00
write_file ( "./gen_router.go" , fdata )
2017-04-12 10:10:36 +00:00
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 ( )
}