summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-12 20:01:20 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-12 20:01:20 -0400
commit59916c9462552a1826eeaddd62063f28716a1e30 (patch)
tree136903699bf6b49f970e87e78e78a81b67be3f24
parent276b82f0d49d52ca02a7a51f7acb27b8cbc8381b (diff)
peach: update default handler
Move it into its own function.
-rw-r--r--main.go62
1 files changed, 32 insertions, 30 deletions
diff --git a/main.go b/main.go
index 9e1a6f7..36c4548 100644
--- a/main.go
+++ b/main.go
@@ -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)