-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (120 loc) · 2.82 KB
/
main.go
File metadata and controls
135 lines (120 loc) · 2.82 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
type EnvVars struct {
BaseURL string
ApiPre string
ApiVer string
ApiEndpoint string
ApiKey string
City string
Units string
}
type WeatherData struct {
Weather []struct {
Description string `json:"description"`
} `json:"weather"`
Main struct {
Temp float64 `json:"temp"`
} `json:"main"`
}
func getEnvVars() EnvVars {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
return EnvVars{
BaseURL: os.Getenv("BASE_URL"),
ApiPre: os.Getenv("OWM_API_PRE"),
ApiVer: os.Getenv("OWM_API_VER"),
ApiEndpoint: os.Getenv("OWM_API_ENDPOINT"),
ApiKey: os.Getenv("OWM_API_KEY"),
City: os.Getenv("OWM_CITY"),
Units: os.Getenv("OWM_UNITS"),
}
}
func (e EnvVars) buildFQDN() string {
fqdn := fmt.Sprintf("%s/%s/%s/%s?q=%s&units=%s&appid=%s",
e.BaseURL,
e.ApiPre,
e.ApiVer,
e.ApiEndpoint,
e.City,
e.Units,
e.ApiKey)
return fqdn
}
// TODO: add more weather data and location
// TODO: read from template html file
// TODO: accept input from form
// TODO: tailwindcss
// TODO: get unsplash/cc photo of location
// TODO: use geolocation from browser
// TODO: cache weather data (boltdb?)
// TODO: migrate from make to task
func main() {
envVars := getEnvVars()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `
<!DOCTYPE html>
<html>
<head>
<title>Weather</title>
<script src="https://unpkg.com/htmx.org"></script>
</head>
<body>
<h1>Today's Weather</h1>
<div hx-get="/weather" hx-trigger="load" hx-swap="outerHTML">
<p>Loading...</p>
</div>
</body>
</html>
`)
})
http.HandleFunc("/weather", func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get(envVars.buildFQDN())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var data WeatherData
err = json.Unmarshal(body, &data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var units string
switch envVars.Units {
case "metric":
units = "°C"
case "imperial":
units = "°F"
default:
units = "K"
}
if len(data.Weather) > 0 {
fmt.Fprintf(w, "<p>Description: %s</p><p>Temperature: %.2f %s</p>", data.Weather[0].Description, data.Main.Temp, units)
} else {
http.Error(w, "Weather data is not available", http.StatusInternalServerError)
return
}
})
log.Println("Listening on :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("Server exited with: %v", err)
}
}