Skip to content

Commit a673cd3

Browse files
committed
Establishing Dev Branch
1 parent 9fc6eb5 commit a673cd3

File tree

11 files changed

+121
-12
lines changed

11 files changed

+121
-12
lines changed

README.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,54 @@ This uis how I set up a new FastAPI project. I hope this is helpful.
2525
|[virtualenv](https://virtualenv.pypa.io/en/stable/installation.html) | Again I am not trying to get fancy. ```py -m pip install virtualenv```|
2626
|[FastAPI](https://github.com/tiangolo/fastapi) | I usally install all related packages in my venv. ```pip install fastapi[all]``` |
2727
|[Docker](https://www.docker.com/products/docker-desktop) | (Optional) I use it to startup DBs instead of installing directly. Will make your life easier. There are also images for bundled FastAPI environmets on Docker Hub.
28-
|[ODMantic](https://art049.github.io/odmantic/) | ORM for MongoDB with great model and document support. |
28+
|[ODMantic](https://art049.github.io/odmantic/) | (Optional) ORM for MongoDB with great model and document support. |
2929
|[TortoiseORM](https://tortoise-orm.readthedocs.io/en/latest/) | (Optional) ASYNC ORM that works well with FastAPI and Postgres/MySQL
3030

3131

32-
## Setup virtual environment (Windows)
32+
## Get Started
3333

34-
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
34+
1. Install Python if not already installed
3535

36-
1. Make sure venv is installed globally
37-
```bash
36+
2. Clone this repository:
37+
```git
38+
git clone https://github.com/blevinscm/fastapi-scaffold-base.git
39+
```
40+
41+
3. Install virtualenv
42+
```zsh
3843
py -m pip install --user virtualenv
3944
```
40-
2. cd into workspace
41-
3. Create virtual environment
45+
4. CD into project directory
46+
```zsh
47+
cd dir
48+
```
49+
5. Create virtual environment
4250
```bash
4351
py -m venv env
4452
```
45-
4. Activate virtual environment
53+
6. Activate virtual environment
4654
```bash
47-
.\env\Scripts\activate
55+
source .\env\bin\activate
4856
```
49-
5. Install pip dependencies
57+
7. Install pip dependencies (This will install all FastAPI Modules. If you do not want [all] plese adjust the req file.)
5058
```bash
5159
pip install -r requirements.txt
5260
```
53-
6. Now you can run the ```__main__.py``` script from a virtual context ```python lib```
54-
7. To get out of venv
61+
8. (Optional) Pull latest Mongo image and create mongo container for dev purposes
62+
```docker
63+
docker pull mongo:latest
64+
docker run --rm --net=host mongo
65+
```
66+
9. Set execute permissions on either win-start-FastApi.ps1 or nix-start-FastAPI
67+
68+
10. Run the start-FastAPI for your system
69+
70+
11. Go to https://localhost:8000/docs to see your FastAPI documentation and explore the sample docs.
71+
72+
73+
12. Close your FastAPI terminal and your Mongo Terminal.
74+
75+
11. To get out of venv
5576
```bash
5677
deactivate
5778
```

app/data/data.py

Whitespace-only changes.

app/data/models.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
3+
from fastapi import FastAPI, HTTPException
4+
5+
from odmantic import AIOEngine, Model, ObjectId
6+
7+
8+
class Hug(Model):
9+
kind: str
10+
hug_size: float
11+
hugger: str
12+
13+
14+
engine = AIOEngine()
15+
16+
17+
@app.put("/hugs/", response_model=Hug)
18+
async def new_hug(hug: Hug):
19+
await engine.save(hug)
20+
return hug
21+
22+
23+
@app.get("/hugs/", response_model=List[Hug])
24+
async def get_hugs():
25+
trees = await engine.find(Hug)
26+
return trees
27+
28+
29+
@app.get("/hugs/count", response_model=int)
30+
async def count_hugs():
31+
count = await engine.count(Hug)
32+
return count
33+
34+
35+
@app.get("/hugs/{id}", response_model=Hug)
36+
async def get_tree_by_id(id: ObjectId):
37+
hug = await engine.find_one(Hug, Hug.id == id)
38+
if hug is None:
39+
raise HTTPException(404)
40+
return hug

app/data/models/__init__.py

Whitespace-only changes.

app/data/models/models.py

Whitespace-only changes.

app/main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fastapi import Depends, FastAPI, Header, HTTPException
2+
3+
from .routers import hugs
4+
5+
app = FastAPI()
6+
7+
8+
async def get_token_header(x_token: str = Header(...)):
9+
if x_token != "fake-super-secret-token":
10+
raise HTTPException(status_code=400, detail="X-Token header invalid")
11+
12+
13+
app.include_router(hugs.router)

app/routers/hugs.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from fastapi import APIRouter
2+
from data import models
3+
4+
router = APIRouter()
5+
6+
7+
@router.get("/hugs/", tags=["Friendly_Hugs"])
8+
async def read_hugs():
9+
"""
10+
Returns all huggers available
11+
"""
12+
return [{"hugs": "Pooh"}, {"username": "Bear"}]
13+
14+
15+
@router.get("/hugs/me", tags=["Friendly_Hugs"])
16+
async def read_huggee_me():
17+
"""
18+
Set to return user context. In this case the hugee.
19+
"""
20+
return {"username": "Huggable Developer"}
21+
22+
23+
@router.get("/hugs/{type}", tags=["Friendly_Hugs"])
24+
async def read_hug_type(hug_type: str):
25+
"""
26+
Allows user to enter the type of hug they want.
27+
Replace with any single non-enum type.
28+
"""
29+
return {"username": hug_type}

app/routers/routers.py

Whitespace-only changes.

nix-start-FastAPI

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
uvicorn app.main:app --reload

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FastAPI[all]
2+
ODMantic

0 commit comments

Comments
 (0)