FastAPI

Building and shipping a Python web application with FastAPI.

A step-by-step build of a FastAPI web application, from the first endpoint to deployment. The chapters below are written up; the rest of the course is outlined at the bottom of this page.

Still to be written up#

These chapters were planned and introduced on the old site, but never filled in.

Web pages#

It’s time to start working on building web pages and turning this project into an actual web application. Why not use Django? And sure, Django is a good choice. However, it is very opinionated, and arguably bloated. Moreover, deploying a Django application is always challenging and problematic in my experience.

So, using the FastAPI web framework, it’s possible to build a project but with more control and autonomy, paving the way for a microservice architecture, and as you’ll see later, have simple deployments using containers.

Authorisation#

Authorization for websites is a crucial aspect of online security and privacy. It involves granting or denying access to specific resources, functionalities, or data on a website to users or systems based on their identity, roles, or permissions. This mechanism ensures that only authorized individuals or entities can interact with certain parts of a website, safeguarding sensitive information and maintaining the site’s integrity.

Authorization typically works hand in hand with authentication, which verifies the identity of users, and together, they form a robust defense against unauthorized access and potential security breaches. Whether it’s controlling who can access premium content, editing user profiles, or making administrative changes, effective authorization strategies are essential for maintaining a secure and user-friendly web experience.

User registration#

At this stage we have all the know-how to complete the project. User registration will leverage the concept of adding a form, including a CAPTCHA, creating a view the with render the form with the GET method, and process the form using a POST method.

The POST method function will include the sending of an email, with an activation link. Said link with have a predefined expiry time. We also need a way of checking that the email address submitted hasn’t already being used.

Quite a bit to work through, but nothing radically new.

Site admin#

This section covers how to add a site administration area. We will use a new CSS template and restrict access to users who only have is_superuserset to True.

Super users will have the ability to modify all users, blog posts and comments. The examples provided are using this template https://www.free-css.com/free-css-templates/page208/simple-admin.

Payments#

If you want to add the ability to sell products and therefore take payments via a web project, this section walks through how to do that with Stripe.

Building on with our example project, lets use a scenario where user have to pay £1 per month to be a member.

This premium membership could be used to only allow paying members to write blogpost content on the site, with regular members only able to leave comments.

AWS deployment#

If you’ve made it this far, you should have a pretty impression and a functional web application ready for the world to see. I mentioned at the very start that one of my key motivations for adopting FastAPI as my web framework of choice is the simplicity of containerizing the application and deploying applications.

In this final section, I’ll demonstrate how to use Podman to build and deploy the example application. Then, using the same method, spin up a cloud instance in Amazon Web Services using infrastructure as code with Terraform.

To make the web application publicly accessible, I’ll demonstrate how to use NGINX and Let’s Encrypt as a reverse proxy over HTTPS.

OpenShift#

The following section covers deploying your application on OpenShift.