diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-06-12 20:01:20 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-06-12 20:01:20 -0400 |
commit | 59916c9462552a1826eeaddd62063f28716a1e30 (patch) | |
tree | 136903699bf6b49f970e87e78e78a81b67be3f24 | |
parent | 276b82f0d49d52ca02a7a51f7acb27b8cbc8381b (diff) |
peach: update default handler
Move it into its own function.
-rw-r--r-- | main.go | 62 |
1 files changed, 32 insertions, 30 deletions
@@ -43,45 +43,47 @@ func init() { } func main() { - // Search handler. - http.HandleFunc("/search", showSearch) - // Default handler. - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) - - if r.URL.Path == "/" { - http.Redirect(w, r, "/41.115,-83.177", 302) - return - } - if r.URL.Path == "/version" { - fmt.Fprintf(w, "v%s\n", version.Version) - return - } - - m := latLngRegex.FindStringSubmatch(r.URL.Path) - if len(m) != 3 || m[0] != r.URL.Path { - http.NotFound(w, r) - return - } - lat, err := strconv.ParseFloat(m[1], 32) - if err != nil { - http.Error(w, err.Error(), 400) - } - lng, err := strconv.ParseFloat(m[2], 32) - if err != nil { - http.Error(w, err.Error(), 400) - } - showWeather(w, float32(lat), float32(lng)) - }) + http.HandleFunc("/", defaultHandler) // Static files handler. http.HandleFunc("/static/", serveStaticFile) + // Search handler. + http.HandleFunc("/search", showSearch) + // Start server log.Fatal(http.ListenAndServe(peachAddr, nil)) } +func defaultHandler(w http.ResponseWriter, r *http.Request) { + logRequest(r) + + if r.URL.Path == "/" { + http.Redirect(w, r, "/41.115,-83.177", 302) + return + } + if r.URL.Path == "/version" { + fmt.Fprintf(w, "v%s\n", version.Version) + return + } + + m := latLngRegex.FindStringSubmatch(r.URL.Path) + if len(m) != 3 || m[0] != r.URL.Path { + http.NotFound(w, r) + return + } + lat, err := strconv.ParseFloat(m[1], 32) + if err != nil { + http.Error(w, err.Error(), 400) + } + lng, err := strconv.ParseFloat(m[2], 32) + if err != nil { + http.Error(w, err.Error(), 400) + } + showWeather(w, float32(lat), float32(lng)) +} + func showWeather(w http.ResponseWriter, lat, lng float32) { // Make weather weather, err, status := weather.NewWeather(lat, lng) |