Try reducing boilerplate in the generated router.
This commit is contained in:
parent
033f4624c8
commit
146e5cff0d
375
gen_router.go
375
gen_router.go
File diff suppressed because it is too large
Load Diff
|
@ -75,7 +75,7 @@ func main() {
|
||||||
end := len(route.Path) - 1
|
end := len(route.Path) - 1
|
||||||
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
|
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
|
||||||
out += runBefore(route.RunBefore, 4)
|
out += runBefore(route.RunBefore, 4)
|
||||||
out += "\n\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
|
out += "\n\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
|
||||||
if !route.Action && !route.NoHead {
|
if !route.Action && !route.NoHead {
|
||||||
out += "\n\t\t\thead, err := c.UserCheck(w,req,&user)"
|
out += "\n\t\t\thead, err := c.UserCheck(w,req,&user)"
|
||||||
out += "\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}"
|
out += "\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}"
|
||||||
|
@ -136,7 +136,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out += "\n\t\t\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
|
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
|
||||||
if !route.Action && !route.NoHead && !group.NoHead {
|
if !route.Action && !route.NoHead && !group.NoHead {
|
||||||
out += "\n\t\t\t\thead, err := c.UserCheck(w,req,&user)"
|
out += "\n\t\t\t\thead, err := c.UserCheck(w,req,&user)"
|
||||||
out += "\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}"
|
out += "\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}"
|
||||||
|
@ -155,7 +155,7 @@ func main() {
|
||||||
mapIt(defaultRoute.Name)
|
mapIt(defaultRoute.Name)
|
||||||
out += "\n\t\t\t\tdefault:"
|
out += "\n\t\t\t\tdefault:"
|
||||||
out += runBefore(defaultRoute.RunBefore, 4)
|
out += runBefore(defaultRoute.RunBefore, 4)
|
||||||
out += "\n\t\t\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ")"
|
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ")"
|
||||||
if !defaultRoute.Action && !defaultRoute.NoHead && !group.NoHead {
|
if !defaultRoute.Action && !defaultRoute.NoHead && !group.NoHead {
|
||||||
out += "\n\t\t\t\t\thead, err := c.UserCheck(w,req,&user)"
|
out += "\n\t\t\t\t\thead, err := c.UserCheck(w,req,&user)"
|
||||||
out += "\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}"
|
out += "\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}"
|
||||||
|
@ -326,7 +326,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
c "github.com/Azareal/Gosora/common"
|
c "github.com/Azareal/Gosora/common"
|
||||||
"github.com/Azareal/Gosora/common/counters"
|
co "github.com/Azareal/Gosora/common/counters"
|
||||||
"github.com/Azareal/Gosora/routes"
|
"github.com/Azareal/Gosora/routes"
|
||||||
"github.com/Azareal/Gosora/routes/panel"
|
"github.com/Azareal/Gosora/routes/panel"
|
||||||
)
|
)
|
||||||
|
@ -367,12 +367,12 @@ var markToAgent = map[string]string{ {{range $index, $element := .AllAgentMarkNa
|
||||||
|
|
||||||
// TODO: Stop spilling these into the package scope?
|
// TODO: Stop spilling these into the package scope?
|
||||||
func init() {
|
func init() {
|
||||||
counters.SetRouteMapEnum(routeMapEnum)
|
co.SetRouteMapEnum(routeMapEnum)
|
||||||
counters.SetReverseRouteMapEnum(reverseRouteMapEnum)
|
co.SetReverseRouteMapEnum(reverseRouteMapEnum)
|
||||||
counters.SetAgentMapEnum(agentMapEnum)
|
co.SetAgentMapEnum(agentMapEnum)
|
||||||
counters.SetReverseAgentMapEnum(reverseAgentMapEnum)
|
co.SetReverseAgentMapEnum(reverseAgentMapEnum)
|
||||||
counters.SetOSMapEnum(osMapEnum)
|
co.SetOSMapEnum(osMapEnum)
|
||||||
counters.SetReverseOSMapEnum(reverseOSMapEnum)
|
co.SetReverseOSMapEnum(reverseOSMapEnum)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WriterIntercept struct {
|
type WriterIntercept struct {
|
||||||
|
@ -398,7 +398,7 @@ type HTTPSRedirect struct {}
|
||||||
|
|
||||||
func (red *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (red *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Header().Set("Connection", "close")
|
w.Header().Set("Connection", "close")
|
||||||
counters.RouteViewCounter.Bump({{ index .AllRouteMap "routes.HTTPSRedirect" }})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.HTTPSRedirect"}})
|
||||||
dest := "https://" + req.Host + req.URL.String()
|
dest := "https://" + req.Host + req.URL.String()
|
||||||
http.Redirect(w, req, dest, http.StatusTemporaryRedirect)
|
http.Redirect(w, req, dest, http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
@ -431,7 +431,6 @@ func (r *GenRouter) handleError(err c.RouteError, w http.ResponseWriter, req *ht
|
||||||
if err.Handled() {
|
if err.Handled() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err.Type() == "system" {
|
if err.Type() == "system" {
|
||||||
c.InternalErrorJSQ(err, w, req, err.JSON())
|
c.InternalErrorJSQ(err, w, req, err.JSON())
|
||||||
return
|
return
|
||||||
|
@ -482,7 +481,7 @@ func (r *GenRouter) SuspiciousRequest(req *http.Request, prepend string) {
|
||||||
prepend += "\n"
|
prepend += "\n"
|
||||||
}
|
}
|
||||||
r.DumpRequest(req,prepend+"Suspicious Request")
|
r.DumpRequest(req,prepend+"Suspicious Request")
|
||||||
counters.AgentViewCounter.Bump({{.AllAgentMap.suspicious}})
|
co.AgentViewCounter.Bump({{.AllAgentMap.suspicious}})
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLocalHost(host string) bool {
|
func isLocalHost(host string) bool {
|
||||||
|
@ -493,11 +492,11 @@ func isLocalHost(host string) bool {
|
||||||
// TODO: SetDefaultPath
|
// TODO: SetDefaultPath
|
||||||
// TODO: GetDefaultPath
|
// TODO: GetDefaultPath
|
||||||
func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
var malformedRequest = func(typ int) {
|
malformedRequest := func(typ int) {
|
||||||
w.WriteHeader(200) // 400
|
w.WriteHeader(200) // 400
|
||||||
w.Write([]byte(""))
|
w.Write([]byte(""))
|
||||||
r.DumpRequest(req,"Malformed Request T"+strconv.Itoa(typ))
|
r.DumpRequest(req,"Malformed Request T"+strconv.Itoa(typ))
|
||||||
counters.AgentViewCounter.Bump({{.AllAgentMap.malformed}})
|
co.AgentViewCounter.Bump({{.AllAgentMap.malformed}})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the Host and Port string
|
// Split the Host and Port string
|
||||||
|
@ -604,10 +603,10 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
r.DumpRequest(req,"before routes.StaticFile")
|
r.DumpRequest(req,"before routes.StaticFile")
|
||||||
}
|
}
|
||||||
// Increment the request counter
|
// Increment the request counter
|
||||||
counters.GlobalViewCounter.Bump()
|
co.GlobalViewCounter.Bump()
|
||||||
|
|
||||||
if prefix == "/s" { //old prefix: /static
|
if prefix == "/s" { //old prefix: /static
|
||||||
counters.RouteViewCounter.Bump({{ index .AllRouteMap "routes.StaticFile" }})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.StaticFile"}})
|
||||||
req.URL.Path += extraData
|
req.URL.Path += extraData
|
||||||
routes.StaticFile(w, req)
|
routes.StaticFile(w, req)
|
||||||
return
|
return
|
||||||
|
@ -626,7 +625,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
ua := strings.TrimSpace(strings.Replace(strings.TrimPrefix(req.UserAgent(),"Mozilla/5.0 ")," Safari/537.36","",-1)) // Noise, no one's going to be running this and it would require some sort of agent ranking system to determine which identifier should be prioritised over another
|
ua := strings.TrimSpace(strings.Replace(strings.TrimPrefix(req.UserAgent(),"Mozilla/5.0 ")," Safari/537.36","",-1)) // Noise, no one's going to be running this and it would require some sort of agent ranking system to determine which identifier should be prioritised over another
|
||||||
var agent string
|
var agent string
|
||||||
if ua == "" {
|
if ua == "" {
|
||||||
counters.AgentViewCounter.Bump({{.AllAgentMap.blank}})
|
co.AgentViewCounter.Bump({{.AllAgentMap.blank}})
|
||||||
if c.Dev.DebugMode {
|
if c.Dev.DebugMode {
|
||||||
var prepend string
|
var prepend string
|
||||||
for _, char := range req.UserAgent() {
|
for _, char := range req.UserAgent() {
|
||||||
|
@ -716,7 +715,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if agent == "" {
|
if agent == "" {
|
||||||
counters.AgentViewCounter.Bump({{.AllAgentMap.unknown}})
|
co.AgentViewCounter.Bump({{.AllAgentMap.unknown}})
|
||||||
if c.Dev.DebugMode {
|
if c.Dev.DebugMode {
|
||||||
var prepend string
|
var prepend string
|
||||||
for _, char := range req.UserAgent() {
|
for _, char := range req.UserAgent() {
|
||||||
|
@ -725,9 +724,9 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
r.DumpRequest(req,"Blank UA: " + prepend)
|
r.DumpRequest(req,"Blank UA: " + prepend)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
counters.AgentViewCounter.Bump(agentMapEnum[agent])
|
co.AgentViewCounter.Bump(agentMapEnum[agent])
|
||||||
}
|
}
|
||||||
counters.OSViewCounter.Bump(osMapEnum[os])
|
co.OSViewCounter.Bump(osMapEnum[os])
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Do we want to track missing language headers too? Maybe as it's own type, e.g. "noheader"?
|
// TODO: Do we want to track missing language headers too? Maybe as it's own type, e.g. "noheader"?
|
||||||
|
@ -747,26 +746,26 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
c.DebugDetail("llLang:", llLang)
|
c.DebugDetail("llLang:", llLang)
|
||||||
if llLang == "" {
|
if llLang == "" {
|
||||||
counters.LangViewCounter.Bump("none")
|
co.LangViewCounter.Bump("none")
|
||||||
} else {
|
} else {
|
||||||
validCode := counters.LangViewCounter.Bump(llLang)
|
validCode := co.LangViewCounter.Bump(llLang)
|
||||||
if !validCode {
|
if !validCode {
|
||||||
r.DumpRequest(req,"Invalid ISO Code")
|
r.DumpRequest(req,"Invalid ISO Code")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
counters.LangViewCounter.Bump("none")
|
co.LangViewCounter.Bump("none")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Config.RefNoTrack {
|
if !c.Config.RefNoTrack {
|
||||||
referrer := req.Header.Get("Referer") // Check the 'referrer' header too? :P
|
ref := req.Header.Get("Referer") // Check the 'referrer' header too? :P
|
||||||
if referrer != "" {
|
if ref != "" {
|
||||||
// ? Optimise this a little?
|
// ? Optimise this a little?
|
||||||
referrer = strings.TrimPrefix(strings.TrimPrefix(referrer,"http://"),"https://")
|
ref = strings.TrimPrefix(strings.TrimPrefix(ref,"http://"),"https://")
|
||||||
referrer = strings.Split(referrer,"/")[0]
|
ref = strings.Split(ref,"/")[0]
|
||||||
portless := strings.Split(referrer,":")[0]
|
portless := strings.Split(ref,":")[0]
|
||||||
if portless != "localhost" && portless != "127.0.0.1" && portless != c.Site.Host {
|
if portless != "localhost" && portless != "127.0.0.1" && portless != c.Site.Host {
|
||||||
counters.ReferrerTracker.Bump(referrer)
|
co.ReferrerTracker.Bump(ref)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -827,7 +826,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
|
||||||
h.Del("Content-Type")
|
h.Del("Content-Type")
|
||||||
h.Del("Content-Encoding")
|
h.Del("Content-Encoding")
|
||||||
}
|
}
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile" }})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile"}})
|
||||||
req.URL.Path += extraData
|
req.URL.Path += extraData
|
||||||
// TODO: Find a way to propagate errors up from this?
|
// TODO: Find a way to propagate errors up from this?
|
||||||
r.UploadHandler(w,req) // TODO: Count these views
|
r.UploadHandler(w,req) // TODO: Count these views
|
||||||
|
@ -837,7 +836,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
|
||||||
// TODO: Add support for favicons and robots.txt files
|
// TODO: Add support for favicons and robots.txt files
|
||||||
switch(extraData) {
|
switch(extraData) {
|
||||||
case "robots.txt":
|
case "robots.txt":
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.RobotsTxt"}})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.RobotsTxt"}})
|
||||||
return routes.RobotsTxt(w,req)
|
return routes.RobotsTxt(w,req)
|
||||||
case "favicon.ico":
|
case "favicon.ico":
|
||||||
gzw, ok := w.(c.GzipResponseWriter)
|
gzw, ok := w.(c.GzipResponseWriter)
|
||||||
|
@ -851,10 +850,10 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
|
||||||
routes.StaticFile(w,req)
|
routes.StaticFile(w,req)
|
||||||
return nil
|
return nil
|
||||||
case "opensearch.xml":
|
case "opensearch.xml":
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.OpenSearchXml"}})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.OpenSearchXml"}})
|
||||||
return routes.OpenSearchXml(w,req)
|
return routes.OpenSearchXml(w,req)
|
||||||
/*case "sitemap.xml":
|
/*case "sitemap.xml":
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.SitemapXml"}})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.SitemapXml"}})
|
||||||
return routes.SitemapXml(w,req)*/
|
return routes.SitemapXml(w,req)*/
|
||||||
}
|
}
|
||||||
return c.NotFound(w,req,nil)
|
return c.NotFound(w,req,nil)
|
||||||
|
@ -865,7 +864,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
|
||||||
r.RUnlock()
|
r.RUnlock()
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.DynamicRoute" }}) // TODO: Be more specific about *which* dynamic route it is
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.DynamicRoute"}}) // TODO: Be more specific about *which* dynamic route it is
|
||||||
req.URL.Path += extraData
|
req.URL.Path += extraData
|
||||||
return handle(w,req,user)
|
return handle(w,req,user)
|
||||||
}
|
}
|
||||||
|
@ -876,7 +875,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
|
||||||
} else {
|
} else {
|
||||||
r.DumpRequest(req,"Bad Route")
|
r.DumpRequest(req,"Bad Route")
|
||||||
}
|
}
|
||||||
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.BadRoute" }})
|
co.RouteViewCounter.Bump({{index .AllRouteMap "routes.BadRoute"}})
|
||||||
return c.NotFound(w,req,nil)
|
return c.NotFound(w,req,nil)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue