refactor(go): simplify URL path logic
This commit is contained in:
parent
49ca0945fb
commit
dd807cb0d3
1 changed files with 29 additions and 35 deletions
64
main.go
64
main.go
|
@ -22,10 +22,18 @@ func main() {
|
||||||
|
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static"))))
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static"))))
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
handleRequest(w, r, tmpl)
|
handleRequest(w, r, tmpl)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, `User-Agent: *
|
||||||
|
Disallow: /harming/humans
|
||||||
|
Disallow: /ignoring/human/orders
|
||||||
|
Disallow: /harm/to/self
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
log.Printf("Starting server on %s", config.listen)
|
log.Printf("Starting server on %s", config.listen)
|
||||||
log.Fatal(http.ListenAndServe(config.listen, nil))
|
log.Fatal(http.ListenAndServe(config.listen, nil))
|
||||||
}
|
}
|
||||||
|
@ -36,43 +44,29 @@ func handleRequest(w http.ResponseWriter, r *http.Request, tmpl *template.Templa
|
||||||
|
|
||||||
log.Printf("r.URL.Path: %s, sourceIP: %s, isHeadless: %t, User-Agent: %s", r.URL.Path, sourceIP, isHeadless, r.Header.Get("User-Agent"))
|
log.Printf("r.URL.Path: %s, sourceIP: %s, isHeadless: %t, User-Agent: %s", r.URL.Path, sourceIP, isHeadless, r.Header.Get("User-Agent"))
|
||||||
|
|
||||||
// TODO: /favicon.ico
|
if isHeadless {
|
||||||
switch r.URL.Path {
|
fmt.Fprintf(w, "%s\n", sourceIP)
|
||||||
case "/":
|
} else {
|
||||||
if isHeadless {
|
csp := []string{
|
||||||
fmt.Fprintf(w, "%s\n", sourceIP)
|
"default-src 'none'",
|
||||||
} else {
|
"img-src 'self'",
|
||||||
csp := []string{
|
"script-src-elem 'self'",
|
||||||
"default-src 'none'",
|
"style-src-elem 'self' fonts.googleapis.com",
|
||||||
"img-src 'self'",
|
"font-src fonts.gstatic.com",
|
||||||
"script-src-elem 'self'",
|
"connect-src api.ip.sb api-v3.speedtest.cn api.ipapi.is",
|
||||||
"style-src-elem 'self' fonts.googleapis.com",
|
}
|
||||||
"font-src fonts.gstatic.com",
|
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
|
||||||
"connect-src api.ip.sb api-v3.speedtest.cn api.ipapi.is",
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
|
|
||||||
|
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"sourceIP": sourceIP,
|
"sourceIP": sourceIP,
|
||||||
}
|
|
||||||
|
|
||||||
err := tmpl.Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Unable to load template", http.StatusInternalServerError)
|
|
||||||
log.Printf("Template execution error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "/robots.txt":
|
err := tmpl.Execute(w, data)
|
||||||
fmt.Fprintf(w, `User-Agent: *
|
if err != nil {
|
||||||
Disallow: /harming/humans
|
http.Error(w, "Unable to load template", http.StatusInternalServerError)
|
||||||
Disallow: /ignoring/human/orders
|
log.Printf("Template execution error: %v", err)
|
||||||
Disallow: /harm/to/self
|
return
|
||||||
`)
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue