Git Commit : commit
I talk a lot😁, and I tell my mistake from time to time but that's how it is going to be, I don't want it to be monotonous and full of instructions. If we are doing something, we should know what happens if we don't do that. Though, I am trying to minimize it to some extent😶.
First of all, create a new folder inside the project directory, named backend.
Inside the backend directory, make a requirements.txt file.
In this requirements.txt file, we are going to keep track of all our project dependencies of external libraries. This is useful in case we want to share our code or We want to deploy it. The new system should know what all libraries our project needs.
This is our current folder structure:
algoholic.io/
├─.gitignore
└─backend/
└─requirements.txt
Inside of requirements.txt file: type in the following:
fastapi==0.95.1
uvicorn==0.22.0
Fastapi you already know, uvicorn, hypercorn, and gunicorn are servers that serve our API. I chose uvicorn because it supports async code. (Its not unicorn
🦄 but uvicorn !)
To install these libraries
type pip install -r requirements.txt
in your terminal
It's time to taste fastapi, create a main.py file inside the backend folder, and type the following code, When I say type, I mean it, these are just 7-10 lines and you should type to get a better understanding of what's going on.
#main.py
from fastapi import FastAPI
from core.config import settings
app = FastAPI(title=settings.PROJECT_NAME,version=settings.PROJECT_VERSION)
@app.get("/")
def hello_api():
return {"msg":"Hello FastAPI🚀"}
Now, let's understand what we did, We are creating an instance of FastAPI and initialized it with a title and a project version. Now, we can reference the 'app' as a fastapi class object and use it to create routes.
@app.get('/')
is called a decorator. A decorator is used to provide extra functionality to a function. '/'
means that this is the home endpoint. Had it been '/about/' It would mean that whenever someone searches for example.com/about/, run this function.get
here is called a verb. There are HTTP verbs that determine the functionality allowed for a request. In this case, get means "A user may connect to this home route to retrieve some information."
More on HTTP verbs:
GET
: Requests using GET
should only retrieve data.
POST
: The POST
method is used to submit an entity to the specified resource, e.g. submitting a form.
PUT
: The PUT
method is used to update a database table record.
DELETE
: The DELETE
method deletes the specified resource.
Okay back to our project. Notice that we are importing something from a config file from a folder named core.
├─.gitignore
└─backend/
├─core/
│ └─config.py
├─main.py
└─requirements.txt
We will store our project settings and configurations inside config.py.
#config.py
class Settings:
PROJECT_NAME:str = "Algoholic 🔥"
PROJECT_VERSION: str = "1.0.0"
settings = Settings()
Ok now this is the moment, let's start the uvicorn server, type uvicorn main:app --reload
(env) nofoobar@fastapi:~/Documents/algoholic.io/backend$ uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['/Documents/algoholic.io/backend']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Application startup complete.
The --reload flag is to tell that 'uvicorn please auto-reload the application, every time I save any file on my project'. Now, visit http://127.0.0.1:8000/ you should see
{"msg":"Hello FastAPI🚀"}
Also, feel free to explore http://127.0.0.1:8000/docs.
Hurreyyy, Nothing big but life is in partying in small things.
Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.
We know, we might make it hard for you but definitely worth the efforts.
© Copyright 2022-23 Team FastAPITutorial