@@ -9,15 +9,18 @@ let router = AutoRouter();
9
9
// Route ordering matters, the first route that matches will be used
10
10
// Any route that does not return will be treated as a middleware
11
11
// Any unmatched route will return a 404
12
- router
13
- . get ( "/" , getWeather ) ;
12
+ router . get ( "/" , ( request , { token} ) => getWeather ( request , token ) )
14
13
15
14
//@ts -ignore
16
15
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} ) ) ;
18
21
} ) ;
19
22
20
- async function getWeather ( request : Request ) : Promise < Response > {
23
+ async function getWeather ( request : Request , token : string ) : Promise < Response > {
21
24
console . log ( "Request received" , request . headers . get ( "spin-client-addr" ) ) ;
22
25
23
26
const clientAddress = getClientAddressFromRequest ( request ) ;
@@ -41,7 +44,6 @@ async function getWeather(request: Request): Promise<Response> {
41
44
42
45
43
46
let endpoint = "https://api.waqi.info/feed/geo:" ;
44
- let token = Variables . get ( "waqi_api_token" ) ; //Use a token from https://aqicn.org/api/
45
47
let html_style = `body{padding:6em; font-family: sans-serif;} h1{color:#f6821f}` ;
46
48
47
49
let html_content = "<h1>Weather 🌦</h1>" ;
@@ -54,7 +56,6 @@ async function getWeather(request: Request): Promise<Response> {
54
56
} ;
55
57
56
58
const response = await fetch ( endpoint , init ) ;
57
- console . log ( "response" , response . status ) ;
58
59
if ( response . status !== 200 ) {
59
60
return new Response ( "Failed to get weather info" , { status : 500 } ) ;
60
61
}
0 commit comments