diff --git a/app/backend/weather.ts b/app/backend/weather.ts index 66b861a..7ca070f 100644 --- a/app/backend/weather.ts +++ b/app/backend/weather.ts @@ -1,23 +1,29 @@ import axios from "axios"; -const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather") +// Initialize the API URL for the weather service +const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather"); if (typeof window !== 'undefined') { - apiURL.hostname = window.location.hostname; + apiURL.hostname = window.location.hostname; // Use current hostname in the browser } else { - apiURL.hostname = "localhost" + apiURL.hostname = "localhost"; // Fallback for server-side } +// Function to get weather data export const getWeather = async (data: object): Promise => { try { + // Make a POST request to the weather API with the provided data const response = await axios.post(apiURL.href, data); - const status = response.data.status; - const success = response.data.response; + const status = response.data.status; // Extract the status from the response + const success = response.data.response; // Extract the actual weather data from the response + + // Send the status and success response back to the worker postMessage({ status, success }); - console.log(JSON.stringify(success)) - return JSON.stringify(success); + console.log(JSON.stringify(success)); // Log the successful response for debugging + return JSON.stringify(success); // Return the weather data as a JSON string } catch (error) { + // Handle any errors that occur during the request postMessage({ status: 500, success: false }); - console.log(error) - return ""; + console.log(error); // Log the error for debugging + return ""; // Return an empty string in case of an error } -}; \ No newline at end of file +};