Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Netlify server less function to fetch Weather Data #114

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

krtEngineer
Copy link

@krtEngineer krtEngineer commented Oct 19, 2024

Issue
Issue: #108

Changes

  1. Creating function to fetch weather data via Netlify serverless function. (netlify/functions/getWeatherData.js).
  2. Placing variables like API_TOKEN, BASE_URL and METRIC in .env file. (netlify/functions/.env). This file is ignored in git.
  3. Added .gitignore file to ignore unnecessary files and directories from git version control.
  4. Added local setup guide in README.md.

Improvement

  1. Security: Prevented API key from being exposed in client-side code, by placing API keys in serverless functions, which can be viewed by anyone who inspects the network traffic or source code.
  2. Environment Variables: Netlify allows to use environment variables in serverless functions, making it easier to manage and secure API keys.
  3. Centralized Logic: Serverless functions allow to centralise logic and manage API interactions in one place, making it easier to update and maintain.

Future Enhancements [Depends on maintainer's choice]

  1. Access Control: Authentication and authorisation logic can be implemented in the serverless function to allow who can access API.
  2. Rate Limiting and Monitoring: Usage monitoring and rate limiting can be implemented to prevent abuse of your API keys.

Netlify function usage
In this PR, Netlify function is used only at one place in code base i.e. to fetch weather data for any city. After merge of this PR, Netlify function can be used to fetch other weather related data. This is intentionally done to keep this PR simple.

Netlify function demo deployment
The code base with Netlify function is deployed on Netlify.
Demo URL: https://krt-test-deploy-weather-now-delete.netlify.app/

Apologises for deploying code without permission. This deployment is only to show the changes made in PR. It will be deleted permanently after this PR gets merged.

Demo Image
image

Steps for Netlify Deployment

After this PR gets merged follow these steps to deploy site on Netlify.

  1. Login to Netlify with Github account.
  2. Authorise Netlify to access your Github account.
  3. Click on Add new site button on top right portion.
  4. Choose option Import an existing project.
  5. Choose Github as git provider.
  6. Choose WeatherNow repository.
  7. Type site name. (It should be unique, check its availability).
  8. Choose branch to deploy i.e. main.
  9. Leave other deploy options empty.
  10. Add env variables.
 OPENWEATHERMAP_TOKEN=your_open_weather_api_key
 WEATHER_API_BASE_URL=https://api.openweathermap.org/data/2.5/weather
 WEATHER_DATA_UNIT=metric
  1. Deploy your site.
  2. Open production deploy after successful deployment.
  3. If any changes happen in future in main branch, Netlify will trigger deploy automatically.

Signed-off-by: Kushagra Raj Tiwari <[email protected]>
Signed-off-by: Kushagra Raj Tiwari <[email protected]>
Signed-off-by: Kushagra Raj Tiwari <[email protected]>
@krtEngineer krtEngineer changed the title Adding Netlify server less function to fetch Weather Data Feature: Adding Netlify server less function to fetch Weather Data Oct 19, 2024
@krtEngineer krtEngineer changed the title Feature: Adding Netlify server less function to fetch Weather Data Feature: Netlify server less function to fetch Weather Data Oct 19, 2024
@R0Y15
Copy link
Owner

R0Y15 commented Oct 19, 2024

this version throws an error while fetching the API
image

@krtEngineer
Copy link
Author

krtEngineer commented Oct 19, 2024

@R0Y15 I implemented this serverless function for Netlify. The path where function is stored: netlify/functions/getWeatherData.js is identified by Netlify. I don't know how it behaves with Github. You can check here: https://krt-test-deploy-weather-now-delete.netlify.app/ I typed Kolkata and it gave me expected response. No Error.

image

You can check the API response here as well. https://krt-test-deploy-weather-now-delete.netlify.app/.netlify/functions/getWeatherData?city=kolkata

Suggestion: Please use Netlify, it is best free platform to deploy static sites. I tried both Github and Netlify, but found Netlify better than Github. It provides good CI/CD integrations than Github. If you want we can connect on video call to discuss the deployment part. Thanks.

@krtEngineer
Copy link
Author

@R0Y15 any update on this PR?

@R0Y15
Copy link
Owner

R0Y15 commented Oct 22, 2024

Hi, sorry for the delay. As per your contribution, we have to switch the whole deployment from GitHub pages to Netlify services, which is not recommended. However, please allow me sometime to further review and take a suitable action.

warm regards,
Roy

@krtEngineer
Copy link
Author

krtEngineer commented Oct 22, 2024

Yes. You are correct. I understand your concern. But deployment on Netlify is very easy step (2 minutes, only). Also, it provides with continous deployment and integration. I have several projects on Netlify, they are running fine. I also use Netlify for my website as well. https://kurati.dev/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants