diff --git a/main.go b/main.go index 763cd34..a897b3e 100644 --- a/main.go +++ b/main.go @@ -22,10 +22,18 @@ func main() { 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) }) + 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.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")) - // TODO: /favicon.ico - switch r.URL.Path { - case "/": - if isHeadless { - fmt.Fprintf(w, "%s\n", sourceIP) - } else { - csp := []string{ - "default-src 'none'", - "img-src 'self'", - "script-src-elem 'self'", - "style-src-elem 'self' fonts.googleapis.com", - "font-src fonts.gstatic.com", - "connect-src api.ip.sb api-v3.speedtest.cn api.ipapi.is", - } - w.Header().Set("Content-Security-Policy", strings.Join(csp, "; ")) + if isHeadless { + fmt.Fprintf(w, "%s\n", sourceIP) + } else { + csp := []string{ + "default-src 'none'", + "img-src 'self'", + "script-src-elem 'self'", + "style-src-elem 'self' fonts.googleapis.com", + "font-src fonts.gstatic.com", + "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{ - "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 - } + data := map[string]string{ + "sourceIP": sourceIP, } - case "/robots.txt": - fmt.Fprintf(w, `User-Agent: * -Disallow: /harming/humans -Disallow: /ignoring/human/orders -Disallow: /harm/to/self -`) - - default: - http.NotFound(w, r) + 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 + } } }