summaryrefslogtreecommitdiffstats
path: root/main.go
blob: d1eb88a10b17c33dfe04c040398a1743c67b37c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package main

import (
	"embed"
	"flag"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"regexp"
	"strconv"

	"ricketyspace.net/peach/search"
	"ricketyspace.net/peach/version"
	"ricketyspace.net/peach/weather"
)

// Peach port. Defaults to 8151
var peachPort = flag.Int("p", 8151, "Port to run peach on")

// Peach listen address. Set during init.
var peachAddr = ""

// Holds static content.
//go:embed templates static/peach.min.css
//go:embed static/font/roboto-flex.ttf
//go:embed static/logo/peach-*.png
var peachFS embed.FS

// HTML templates.
var peachTemplates = template.Must(template.ParseFS(peachFS, "templates/*.tmpl"))

// Lat,Long regex.
var latLngRegex = regexp.MustCompile(`/(-?[0-9]+\.?[0-9]+?),(-?[0-9]+\.?[0-9]+)`)

func init() {
	flag.Parse()
	if *peachPort < 80 {
		log.Fatalf("port number is invalid: %v", *peachPort)
	}
	peachAddr = fmt.Sprintf(":%d", *peachPort)
}

func main() {
	// Default handler.
	http.HandleFunc("/", defaultHandler)

	// Static files handler.
	http.HandleFunc("/static/", serveStaticFile)

	// Search handler.
	http.HandleFunc("/search", showSearch)

	// Meta handler.
	http.HandleFunc("/about", showMeta)

	// Start server
	log.Fatal(http.ListenAndServe(peachAddr, nil))
}

func defaultHandler(w http.ResponseWriter, r *http.Request) {
	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)
	if err != nil {
		http.Error(w, err.Error(), status)
		return
	}

	// Render.
	err = peachTemplates.ExecuteTemplate(w, "weather.tmpl", weather)
	if err != nil {
		log.Printf("weather: template: %v", err)
		return
	}
}

func showMeta(w http.ResponseWriter, r *http.Request) {
	// Make meta info.
	type Meta struct {
		Version string
		Title   string
	}
	m := new(Meta)
	m.Version = version.Version
	m.Title = "about"

	// Render.
	err := peachTemplates.ExecuteTemplate(w, "about.tmpl", m)
	if err != nil {
		log.Printf("weather: template: %v", err)
		return
	}
}

func serveStaticFile(w http.ResponseWriter, r *http.Request) {
	// Add Cache-Control header
	w.Header().Set("Cache-Control", "max-age=604800")

	// Serve.
	server := http.FileServer(http.FS(peachFS))
	server.ServeHTTP(w, r)
}

func showSearch(w http.ResponseWriter, r *http.Request) {
	search, err, status := search.NewSearch(r)
	if err != nil && status == 404 {
		http.NotFound(w, r)
		return
	}
	if err != nil {
		http.Error(w, err.Error(), status)
		return
	}
	err = peachTemplates.ExecuteTemplate(w, "search.tmpl", search)
	if err != nil {
		log.Printf("search: template: %v", err)
		return
	}
}