@@ -9,15 +9,19 @@ 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
+ 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} ) ) ;
18
22
} ) ;
19
23
20
- async function getWeather ( request : Request ) : Promise < Response > {
24
+ async function getWeather ( request : Request , token : string ) : Promise < Response > {
21
25
console . log ( "Request received" , request . headers . get ( "spin-client-addr" ) ) ;
22
26
23
27
const clientAddress = getClientAddressFromRequest ( request ) ;
@@ -41,7 +45,6 @@ async function getWeather(request: Request): Promise<Response> {
41
45
42
46
43
47
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
48
let html_style = `body{padding:6em; font-family: sans-serif;} h1{color:#f6821f}` ;
46
49
47
50
let html_content = "<h1>Weather 🌦</h1>" ;
@@ -54,7 +57,6 @@ async function getWeather(request: Request): Promise<Response> {
54
57
} ;
55
58
56
59
const response = await fetch ( endpoint , init ) ;
57
- console . log ( "response" , response . status ) ;
58
60
if ( response . status !== 200 ) {
59
61
return new Response ( "Failed to get weather info" , { status : 500 } ) ;
60
62
}
0 commit comments