Skip to content

Commit efa736b

Browse files
Update samples/geolocation-weather/src/index.ts
Co-authored-by: Karthik Ganeshram <[email protected]> Signed-off-by: Michelle Dhanani <[email protected]>
1 parent 1a9e1a2 commit efa736b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

samples/geolocation-weather/src/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ let router = AutoRouter();
99
// Route ordering matters, the first route that matches will be used
1010
// Any route that does not return will be treated as a middleware
1111
// Any unmatched route will return a 404
12-
router
13-
.get("/", getWeather);
12+
router.get("/", (request, {token}) => getWeather(request, token))
1413

1514
//@ts-ignore
1615
addEventListener('fetch', async (event: FetchEvent) => {
17-
event.respondWith(router.fetch(event.request));
16+
let token = Variables.get("waqi_api_token");
17+
if (!token) {
18+
console.log("required waqi_api_token variable not found");
19+
event.respondWith(new Response("Internal Server Error", {status: 500}));
20+
}
21+
event.respondWith(router.fetch(event.request, {token}));
1822
});
1923

20-
async function getWeather(request: Request): Promise<Response> {
24+
async function getWeather(request: Request, token: string): Promise<Response> {
2125
console.log("Request received", request.headers.get("spin-client-addr"));
2226

2327
const clientAddress = getClientAddressFromRequest(request);
@@ -41,7 +45,6 @@ async function getWeather(request: Request): Promise<Response> {
4145

4246

4347
let endpoint = "https://api.waqi.info/feed/geo:";
44-
let token = Variables.get("waqi_api_token"); //Use a token from https://aqicn.org/api/
4548
let html_style = `body{padding:6em; font-family: sans-serif;} h1{color:#f6821f}`;
4649

4750
let html_content = "<h1>Weather 🌦</h1>";
@@ -54,7 +57,6 @@ async function getWeather(request: Request): Promise<Response> {
5457
};
5558

5659
const response = await fetch(endpoint, init);
57-
console.log("response", response.status);
5860
if (response.status !== 200) {
5961
return new Response("Failed to get weather info", { status: 500 });
6062
}

0 commit comments

Comments
 (0)