[Feature] - Backend Locations API

by ADMIN 34 views

Re-approaching Issue #2 with a Serverless API and SQL DB

In this article, we will explore the development of a backend locations API using CloudFlare workers and D1. This API will serve as a central hub for storing and retrieving location data, enabling the creation of a more engaging and dynamic game experience.

How Locations are Served Right Now

Currently, the locations are pulled from a JSON file on the frontend. This JSON file contains 100 total locations, which are cities in Europe. However, this approach has several limitations:

  • Limited number of locations: With only 100 locations, the game can start to feel samey after a few rounds.
  • Future multiplayer functionality: The current approach is limiting for adding future multiplayer functionality, which is the end goal.

New Locations

To address these limitations, we need to introduce more locations to the game. We can start by adding cities in Europe, but also consider including other types of locations such as:

  • Mountain peaks
  • Famous attractions (e.g. Eiffel tower)
  • Religious sites

To achieve this, we need to have at least 500 locations in the game. However, it's essential to ensure that the locations are not too difficult to get, such as the 4th biggest city in Slovenia.

Locations Shape

Currently, each location is represented by a JSON object with the following properties:

{
  "location": "Edinburgh",
  "coordinates": [-3.1883, 55.9533]
},

However, in the future, locations might have additional properties, such as:

{
  "location": "Edinburgh",
  "coordinates": [-3.1883, 55.9533],
  "difficulty": "easy",
  "continent": "Europe",
  "locationType": "city",
},

Endpoints

The API will have the following endpoints:

  • GET /locations?continent=Europe&difficulty=medium&limit=5: Returns an array of 5 locations that match the specified continent and difficulty level.
  • GET /locations/random: Returns an array of 1 random location.
  • GET /locations/random?limit=5: Returns an array of 5 random locations.
  • GET /locations: Returns all locations.

Todo List

To implement the backend locations API, we need to complete the following tasks:

  • Research the best way to set up the API and DB.
  • Create an initial very basic CloudFlare workers API.
  • Serve locations from the CloudFlare workers API using a JSON file.
  • Hook the API into the frontend and write tests for it.
  • Create a DB for holding all the locations and integrate it into the API.
  • Create scripts and documentation around updating the database (from an admin POV).
  • Add some sort of rate limiting.
  • Add a great test suite for this.

Benefits of a Serverless API

Using a serverless API has several benefits, including:

  • Cost-effective: We only pay for the resources we use.
  • Scalability: The API can scale to handle a large number of requests.
  • Flexibility: We can easily add or remove features as needed.


In this article, we explored the development of a backend locations API using CloudFlare workers and D1. This API will serve as a central hub for storing and retrieving location data, enabling the creation of a more engaging and dynamic game experience. By completing the todo list, we can ensure that the API is robust, scalable, and cost-effective.

Researching the Best Way to Set Up the API and DB

To start, we need to research the best way to set up the API and DB. This involves considering factors such as:

  • CloudFlare workers: We need to determine the best way to use CloudFlare workers to create the API.
  • D1: We need to decide how to use D1 to create the DB.
  • SQL DB: We need to determine the best way to use a SQL DB to store the location data.

Creating an Initial Very Basic CloudFlare Workers API

Once we have determined the best way to set up the API and DB, we can create an initial very basic CloudFlare workers API. This will involve:

  • Creating a new CloudFlare workers project: We need to create a new project in CloudFlare workers.
  • Writing the API code: We need to write the code for the API.
  • Testing the API: We need to test the API to ensure it is working correctly.

Serving Locations from the CloudFlare Workers API Using a JSON File

Next, we need to serve locations from the CloudFlare workers API using a JSON file. This will involve:

  • Creating a new JSON file: We need to create a new JSON file that contains the location data.
  • Updating the API code: We need to update the API code to use the new JSON file.
  • Testing the API: We need to test the API to ensure it is working correctly.

Hooking the API into the Frontend and Writing Tests for It

Once we have served locations from the CloudFlare workers API using a JSON file, we need to hook the API into the frontend and write tests for it. This will involve:

  • Updating the frontend code: We need to update the frontend code to use the new API.
  • Writing tests: We need to write tests to ensure the API is working correctly.

Creating a DB for Holding All the Locations and Integrating It into the API

Next, we need to create a DB for holding all the locations and integrate it into the API. This will involve:

  • Creating a new DB: We need to create a new DB using D1.
  • Updating the API code: We need to update the API code to use the new DB.
  • Testing the API: We need to test the API to ensure it is working correctly.

Creating Scripts and Documentation Around Updating the Database (from an Admin POV)

Once we have created a DB for holding all the locations and integrated it into the API, we need to create scripts and documentation around updating the database (from an admin POV). This will involve:

  • Creating scripts: We need to create scripts that can be used to update the database.
  • Creating documentation: We need to create documentation that explains how to use the scripts.

Adding Some Sort of Rate Limiting

Finally, we need to add some sort of rate limiting to the API. This will involve:

  • Researching rate options: We need to research the different rate limiting options available.
  • Implementing rate limiting: We need to implement rate limiting using the chosen option.
  • Testing the API: We need to test the API to ensure rate limiting is working correctly.

Adding a Great Test Suite for This

To ensure the API is robust and scalable, we need to add a great test suite for this. This will involve:

  • Writing tests: We need to write tests to ensure the API is working correctly.
  • Running tests: We need to run the tests to ensure they are working correctly.
  • Updating tests: We need to update the tests as needed to ensure they continue to work correctly.
    Q&A: Backend Locations API =============================

Frequently Asked Questions

In this article, we will answer some of the most frequently asked questions about the backend locations API.

Q: What is the purpose of the backend locations API?

A: The purpose of the backend locations API is to provide a central hub for storing and retrieving location data. This will enable the creation of a more engaging and dynamic game experience.

Q: Why are we using CloudFlare workers and D1?

A: We are using CloudFlare workers and D1 because they provide a cost-effective and scalable solution for creating a serverless API. This will enable us to handle a large number of requests without incurring significant costs.

Q: What is the current state of the API?

A: The current state of the API is that we have created an initial very basic CloudFlare workers API. We are currently working on serving locations from the API using a JSON file.

Q: How will the API be used in the game?

A: The API will be used in the game to provide location data to the frontend. This will enable the game to display locations on a map and provide a more engaging and dynamic experience.

Q: What are the benefits of using a serverless API?

A: The benefits of using a serverless API include:

  • Cost-effective: We only pay for the resources we use.
  • Scalability: The API can scale to handle a large number of requests.
  • Flexibility: We can easily add or remove features as needed.

Q: How will the API be secured?

A: The API will be secured using a combination of authentication and authorization mechanisms. This will ensure that only authorized users can access the API and that sensitive data is protected.

Q: What is the plan for updating the database?

A: The plan for updating the database is to create scripts and documentation around updating the database (from an admin POV). This will enable administrators to easily update the database and ensure that the API continues to function correctly.

Q: How will the API be tested?

A: The API will be tested using a combination of automated and manual testing techniques. This will ensure that the API is robust and scalable and that it continues to function correctly over time.

Q: What is the timeline for completing the API?

A: The timeline for completing the API is currently unknown. However, we are working hard to complete the API as soon as possible and ensure that it meets the requirements of the game.

Q: How can I get involved in the development of the API?

A: If you are interested in getting involved in the development of the API, please contact us. We are always looking for talented developers to join our team and help us complete the API.

Additional Resources

Conclusion

In this article, we have answered some of the most frequently asked questions about the backend locations API. We hope that this information has been helpful and that you have a better understanding of the API and its purpose. If you have any further questions, please do not hesitate to contact us.