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
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 {
2017-04-12 13:39:03 +00:00
out += "\n\t\tcase \"" + route . Path + "\": " + route . Name + "(w,req/*"
2017-04-12 10:10:36 +00:00
for _ , item := range route . Vars {
out += ", " + item
}
2017-04-12 13:39:03 +00:00
out += "*/)"
2017-04-12 10:10:36 +00:00
}
fdata += ` package main
2017-04-12 13:39:03 +00:00
import "sync"
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
}
func NewGenRouter ( ) * GenRouter {
fs_u := http . FileServer ( http . Dir ( "./uploads" ) )
2017-04-12 13:39:03 +00:00
return & GenRouter {
UploadHandler : http . StripPrefix ( "/uploads/" , fs_u ) . ServeHTTP ,
old_routes : make ( map [ string ] func ( http . ResponseWriter , * http . Request ) ) ,
}
2017-04-12 10:10:36 +00:00
}
2017-04-12 13:39:03 +00:00
func ( router * GenRouter ) Handle ( pattern string , handle http . Handler ) {
router . Lock ( )
router . old_routes [ pattern ] = handle . ServeHTTP
router . Unlock ( )
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-12 13:39:03 +00:00
if req . URL . Path [ 0 ] != '/' {
w . WriteHeader ( 405 )
w . Write ( [ ] byte ( "" ) )
return
}
2017-04-12 10:10:36 +00:00
var extra_data string
2017-04-12 13:39:03 +00:00
/ * 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-12 13:39:03 +00:00
} * /
2017-04-12 10:10:36 +00:00
switch ( req . URL . Path ) { ` + out + `
2017-04-12 13:39:03 +00:00
case "/uploads/" : router . UploadHandler ( w , req )
//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 ]
if ok {
router . RUnlock ( )
req . URL . Path = req . URL . Path + extra_data
handle ( w , req )
return
}
router . RUnlock ( )
NotFound ( w , req )
2017-04-12 10:10:36 +00:00
}
`
2017-04-12 13:39:03 +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 ( )
}