From cff02a574c175db40aafb6df3b02153c16b95d93 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Fri, 20 Jun 2025 22:04:30 +0100 Subject: [PATCH] add api route for querying summaries --- main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/main.go b/main.go index e3187aa..38cbf9a 100644 --- a/main.go +++ b/main.go @@ -397,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)