5 Commits

Author SHA1 Message Date
cff02a574c add api route for querying summaries 2025-06-20 22:04:30 +01:00
e95d3ae624 Added support for Manila and Hong Kong (#3)
Co-authored-by: MMatthew14 <matthewlorenz@protonmail.com>
Reviewed-on: https://github.com/kennethnym/7am/pull/3
2025-05-14 19:38:48 +01:00
7750c0d75e Add support for Singapore (#2)
Co-authored-by: sevensxyt <sevendotxyt@proton.me>
Reviewed-on: https://github.com/kennethnym/7am/pull/2
2025-05-14 13:16:31 +01:00
28a33f869b use mutex to guard sqlite writes 2025-05-13 00:52:16 +01:00
a2a2d008dd fix sql conflict on summary cache update 2025-05-13 00:51:22 +01:00
2 changed files with 42 additions and 1 deletions

40
main.go
View File

@@ -122,6 +122,9 @@ var supportedLocations = map[string]*location{
"la": {nil, 34.052235, -118.243683, "America/Los_Angeles", "Los Angeles"},
"nyc": {nil, 40.712776, -74.005974, "America/New_York", "New York City"},
"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"},
"zurich": {nil, 47.369019, 8.538030, "Europe/Zurich", "Zurich"},
"berlin": {nil, 52.520008, 13.404954, "Europe/Berlin", "Berlin"},
@@ -394,6 +397,39 @@ func handleHTTPRequest(state *state) http.HandlerFunc {
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 {
if request.Method != "" && request.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
@@ -703,10 +739,12 @@ func updateSummary(ctx context.Context, state *state, opts updateSummaryOptions)
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 {
slog.Warn("unable to cache generated weather summary to db", "location", locKey, "error", err)
}
state.dbMutex.Unlock()
state.summaries.Store(locKey, summary)

View File

@@ -53,6 +53,9 @@
<li><a href="/warsaw">Warsaw</a></li>
<li><a href="/dubai">Dubai</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>
</main>