Tuesday, September 28, 2021

fastapi

 Reference:

 https://github.com/nofoobar/fastapi-course/tree/main/backend

#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 API"}

Now, let's understand what we did, We are creating an instance of FastAPI and initializing 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. 
  • 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 of this file named config.py.

#config.py class Settings: PROJECT_NAME:str = "Job Board" PROJECT_VERSION: str = "1.0.0" settings = Settings()















No comments:

Post a Comment