@@ -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
1615addEventListener ( '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>" ;
0 commit comments