Skip to content

Commit 1114c8e

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 1114c8e

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

samples/geolocation-weather/src/index.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ 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+
event.respondWith(new Response("Internal Server Error", {status: 500}));
19+
}
20+
event.respondWith(router.fetch(event.request, {token}));
1821
});
1922

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

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

4245

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

4749
let html_content = "<h1>Weather 🌦</h1>";
@@ -54,7 +56,6 @@ async function getWeather(request: Request): Promise<Response> {
5456
};
5557

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

0 commit comments

Comments
 (0)