Skip to content

Commit 6a8074b

Browse files
authored
Merge pull request #1 from blevinscm/dev
Dev
2 parents 9fc6eb5 + 9e065ba commit 6a8074b

File tree

13 files changed

+102
-13
lines changed

13 files changed

+102
-13
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"python.pythonPath": "/usr/bin/python3.9"
2+
"python.pythonPath": "/home/blevinscm/src/fastapi-scaffold-base/env/bin/python3.8"
33
}

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import typing
2+
from odmantic import AIOEngine, Model, ObjectId
3+
4+
5+
class Hug(Model):
6+
kind: str
7+
hug_size: float
8+
hugger: str

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from fastapi import APIRouter, HTTPException
2+
from app.data import models
3+
from typing import List
4+
from odmantic import AIOEngine, Model, ObjectId
5+
6+
router = APIRouter()
7+
8+
Hug = models.Hug
9+
engine = AIOEngine()
10+
11+
@router.put("/hugs/", response_model=Hug, tags=["hugs"])
12+
async def new_hug(hug: Hug):
13+
await engine.save(hug)
14+
return hug
15+
16+
17+
@router.get("/hugs/", response_model=List[Hug], tags=["hugs"])
18+
async def get_hugs():
19+
trees = await engine.find(Hug)
20+
return trees
21+
22+
23+
@router.get("/hugs/count", response_model=int, tags=["hugs"])
24+
async def count_hugs():
25+
count = await engine.count(Hug)
26+
return count
27+
28+
29+
@router.get("/hugs/{id}", response_model=Hug, tags=["hugs"])
30+
async def get_tree_by_id(id: ObjectId):
31+
hug = await engine.find_one(Hug, Hug.id == id)
32+
if hug is None:
33+
raise HTTPException(404)
34+
return hug

app/routers/routers.py

Whitespace-only changes.

dockerfile

Whitespace-only changes.

0 commit comments

Comments
 (0)