Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
cff02a574c
|
|||
e95d3ae624 | |||
7750c0d75e | |||
28a33f869b
|
|||
a2a2d008dd
|
40
main.go
40
main.go
@@ -122,6 +122,9 @@ var supportedLocations = map[string]*location{
|
|||||||
"la": {nil, 34.052235, -118.243683, "America/Los_Angeles", "Los Angeles"},
|
"la": {nil, 34.052235, -118.243683, "America/Los_Angeles", "Los Angeles"},
|
||||||
"nyc": {nil, 40.712776, -74.005974, "America/New_York", "New York City"},
|
"nyc": {nil, 40.712776, -74.005974, "America/New_York", "New York City"},
|
||||||
"tokyo": {nil, 35.689487, 139.691711, "Asia/Tokyo", "Tokyo"},
|
"tokyo": {nil, 35.689487, 139.691711, "Asia/Tokyo", "Tokyo"},
|
||||||
|
"singapore": {nil, 1.290270, 103.851959, "Asia/Singapore", "Singapore"},
|
||||||
|
"manila": {nil, 14.599512, 120.984222, "Asia/Manila", "Manila"},
|
||||||
|
"hk": {nil, 22.317053, 114.169547, "Asia/Hong_Kong", "Hong Kong"},
|
||||||
"warsaw": {nil, 52.229675, 21.012230, "Europe/Warsaw", "Warsaw"},
|
"warsaw": {nil, 52.229675, 21.012230, "Europe/Warsaw", "Warsaw"},
|
||||||
"zurich": {nil, 47.369019, 8.538030, "Europe/Zurich", "Zurich"},
|
"zurich": {nil, 47.369019, 8.538030, "Europe/Zurich", "Zurich"},
|
||||||
"berlin": {nil, 52.520008, 13.404954, "Europe/Berlin", "Berlin"},
|
"berlin": {nil, 52.520008, 13.404954, "Europe/Berlin", "Berlin"},
|
||||||
@@ -394,6 +397,39 @@ func handleHTTPRequest(state *state) http.HandlerFunc {
|
|||||||
writer.WriteHeader(http.StatusMethodNotAllowed)
|
writer.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if strings.HasPrefix(path, "api/") {
|
||||||
|
if origin := request.Header.Get("Origin"); origin != "" {
|
||||||
|
writer.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, "api/summary/") {
|
||||||
|
switch request.Method {
|
||||||
|
case "OPTIONS":
|
||||||
|
writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
|
||||||
|
writer.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
case "GET":
|
||||||
|
location := strings.TrimPrefix(path, "api/summary/")
|
||||||
|
summary, ok := state.summaries.Load(location)
|
||||||
|
if !ok {
|
||||||
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response := map[string]string{
|
||||||
|
"summary": strings.TrimSpace(summary.(string)),
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
json.NewEncoder(writer).Encode(response)
|
||||||
|
|
||||||
|
default:
|
||||||
|
writer.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if request.Method != "" && request.Method != "GET" {
|
if request.Method != "" && request.Method != "GET" {
|
||||||
writer.WriteHeader(http.StatusMethodNotAllowed)
|
writer.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
@@ -703,10 +739,12 @@ func updateSummary(ctx context.Context, state *state, opts updateSummaryOptions)
|
|||||||
|
|
||||||
summary := result.Text()
|
summary := result.Text()
|
||||||
|
|
||||||
_, err = state.db.ExecContext(ctx, "INSERT INTO summaries (location, summary) VALUES (?, ?)", locKey, summary)
|
state.dbMutex.Lock()
|
||||||
|
_, err = state.db.ExecContext(ctx, "INSERT OR REPLACE INTO summaries (location, summary) VALUES (?, ?)", locKey, summary)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("unable to cache generated weather summary to db", "location", locKey, "error", err)
|
slog.Warn("unable to cache generated weather summary to db", "location", locKey, "error", err)
|
||||||
}
|
}
|
||||||
|
state.dbMutex.Unlock()
|
||||||
|
|
||||||
state.summaries.Store(locKey, summary)
|
state.summaries.Store(locKey, summary)
|
||||||
|
|
||||||
|
@@ -53,6 +53,9 @@
|
|||||||
<li><a href="/warsaw">Warsaw</a></li>
|
<li><a href="/warsaw">Warsaw</a></li>
|
||||||
<li><a href="/dubai">Dubai</a></li>
|
<li><a href="/dubai">Dubai</a></li>
|
||||||
<li><a href="/tokyo">Tokyo</a></li>
|
<li><a href="/tokyo">Tokyo</a></li>
|
||||||
|
<li><a href="/singapore">Singapore</a></li>
|
||||||
|
<li><a href="/manila">Manila</a></li>
|
||||||
|
<li><a href="/hk">Hong Kong</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user