Skip to content

Files

Latest commit

Jul 14, 2024
37d4417 · Jul 14, 2024

History

History

11_microservice_helloworld

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jul 14, 2024
Jul 14, 2024
Jul 14, 2024

FastAPI Hello World

Follow the first part of this tutorial:

https://neon.tech/blog/deploy-a-serverless-fastapi-app-with-neon-postgres-and-aws-app-runner-at-any-scale

Create Project:

poetry new fastapi-helloworld

Change directory to project:

cd fastapi-helloworld 

Add dependecies:

poetry add fastapi "uvicorn[standard]"

Add poetry virtualenv interpreter in VSCode

Write fastapi_helloworld/main.py

Run project in Poetry Envirnoment:

poetry run uvicorn fastapi_helloworld.main:app --host 0.0.0.0 --port 8000

Open in Browser:

http://0.0.0.0:8000/

http://0.0.0.0:8000/docs

http://0.0.0.0:8000/openapi.json

Publish on the Web while running locally

Install Ngrok:

https://ngrok.com/docs/getting-started/

Run the following command to add your authtoken to the default ngrok.yml:

ngrok config add-authtoken 2bZGNZ3Mj3HdRxPt19defOl_7oFHsvr5AmM9A9UZUyhFn

Deploy with your static domain:

ngrok http --domain=my-static-domain.ngrok-free.app 8000

Open in Browser:

https://my-static-domain.ngrok-free.app/

Open in Browser:

https://my-static-domain.ngrok-free.app/

Open Docs in Browser:

https://my-static-domain.ngrok-free.app/docs

Open OpenAPI specs in Browser:

https://my-static-domain.ngrok-free.app/openapi.json

Now we will use this json to create GPT Action.

Now create a Custom GPT here:

https://chat.openai.com/gpts

Add the following server url in the openapi.json file generated by FastAPI and paste it into action schema:

"servers": [
    {
    "url": "https://my-static-domain.ngrok-free.app/"
    }
],

FastAPI is optimized for microservices development

FastAPI design and features make it a compelling choice for building microservices, particularly in Python. Here's how FastAPI aligns with microservices architecture:

Key Features of FastAPI for Microservices

  1. Performance: FastAPI is built on top of Starlette (for the web parts) and Pydantic (for the data parts), making it one of the fastest Python frameworks available. This is crucial for microservices that need to handle a high number of requests efficiently.

  2. Asynchronous Support: FastAPI has built-in support for asynchronous request handling, which is beneficial for IO-bound and high-level concurrent operations often found in microservices.

  3. Ease of Use and Rapid Development: The framework is designed to be easy to use and quick to learn, allowing for rapid development of APIs. This is particularly useful in a microservices architecture where you might need to quickly develop and deploy multiple services.

  4. Automatic Interactive Documentation: FastAPI automatically generates interactive API documentation (using Swagger UI and ReDoc). This feature is invaluable in microservices architecture for maintaining clear documentation and facilitating service integration.

  5. Data Validation and Serialization: With Pydantic integration, FastAPI simplifies data validation and serialization. This is important for ensuring the integrity of data as it flows between different microservices.

  6. Dependency Injection: FastAPI's dependency injection system is designed to be easy to use and powerful. It’s useful for breaking down an application into smaller, maintainable, and interchangeable components, a core principle of microservices.

  7. Containerization: FastAPI applications are easily containerized, which aligns well with the common practice of deploying microservices using containers.

  8. Scalability: The framework's performance and support for asynchronous programming make it suitable for scalable applications, a key requirement in microservices architecture.

Considerations for Microservices Development

  1. Microservices Complexity: For very complex microservices architectures, the simplicity of FastAPI, while beneficial in many cases, might lack some features of more comprehensive frameworks or require additional tools and integrations.

  2. Ecosystem and Community: While growing, FastAPI's ecosystem and community might not be as extensive as some more established frameworks. However, this is rapidly changing as FastAPI gains popularity.

  3. Operational Complexity: Implementing a microservices architecture introduces operational complexity regardless of the framework used. FastAPI simplifies the development aspect but doesn't inherently solve challenges related to deployment, service discovery, or inter-service communication.

Conclusion

FastAPI is well-optimized for microservices development in Python, offering features like high performance, asynchronous support, easy-to-use data validation, and automatic API documentation. These features make it a strong choice for building efficient, scalable, and maintainable microservices. However, the choice of any framework should be guided by the specific requirements of your project, existing infrastructure, and team expertise.