Skip to content

Commit 2005e66

Browse files
authored
Merge pull request #2 from blevinscm/dev
Dev merge back into master
2 parents 6a8074b + c6852b9 commit 2005e66

20 files changed

+619
-27
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": "/home/blevinscm/src/fastapi-scaffold-base/env/bin/python3.8"
2+
"python.pythonPath": "/home/blevinscm/src/fastapi-scaffold-base/env/bin/python3.9"
33
}
File renamed without changes.

app/data/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import typing
2-
from odmantic import AIOEngine, Model, ObjectId
2+
from odmantic import Model
3+
from pydantic import BaseModel
34

45

6+
#MongodDB - uncomment to use odmantic and MongoDB
57
class Hug(Model):
68
kind: str
79
hug_size: float
8-
hugger: str
10+
hugger: str
11+
12+
# POSTGRES - uncomment to use Pydantic and PG
13+
# class Hug(BaseModel):
14+
# hug: str
15+
# hug_size: float
16+
# hugger: str

app/main.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1-
from fastapi import Depends, FastAPI, Header, HTTPException
2-
1+
from fastapi import Request, FastAPI, Header, HTTPException
2+
from fastapi.templating import Jinja2Templates
3+
from fastapi.responses import HTMLResponse
4+
from fastapi.staticfiles import StaticFiles
35
from .routers import hugs
46

5-
app = FastAPI()
7+
8+
templates = Jinja2Templates(directory="templates")
9+
10+
tags_metadata = [
11+
{
12+
"name": "hugs",
13+
"description": "The **hug** is _wonderful_.",
14+
"externalDocs": {
15+
"description": "Hugs external docs. Use to provide a link.",
16+
"url": "https://en.wikipedia.org/wiki/Hug",
17+
},
18+
},
19+
20+
]
21+
app = FastAPI(
22+
openapi_tags=tags_metadata,
23+
title="The super duper Hugs API",
24+
description="This is an example for the base project Generator",
25+
version="0.0.0.0.0.0.1",
26+
27+
)
628

729

830
async def get_token_header(x_token: str = Header(...)):
931
if x_token != "fake-super-secret-token":
1032
raise HTTPException(status_code=400, detail="X-Token header invalid")
1133

1234

13-
app.include_router(hugs.router)
35+
app.include_router(hugs.router)
36+
app.mount("/static", StaticFiles(directory="static"), name="static")
37+
38+
@app.get("/")
39+
async def serve_home(request: Request):
40+
return templates.TemplateResponse("index.html", {"request": request})
41+
42+
@app.get("/my")
43+
async def serve_my(request: Request):
44+
return templates.TemplateResponse("my.html", {"request": request})

app/routers/hugs.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from fastapi import APIRouter, HTTPException
22
from app.data import models
33
from typing import List
4-
from odmantic import AIOEngine, Model, ObjectId
4+
from odmantic import AIOEngine, ObjectId
55

66
router = APIRouter()
77

@@ -10,24 +10,42 @@
1010

1111
@router.put("/hugs/", response_model=Hug, tags=["hugs"])
1212
async def new_hug(hug: Hug):
13+
'''
14+
Add a hug to the Database.
15+
Delete the objectID from the example if using
16+
swaggerUI
17+
Use to add or edit an item in the DB. Do not use Post
18+
when using MongoDB.
19+
'''
1320
await engine.save(hug)
1421
return hug
1522

1623

1724
@router.get("/hugs/", response_model=List[Hug], tags=["hugs"])
1825
async def get_hugs():
19-
trees = await engine.find(Hug)
20-
return trees
26+
'''
27+
Get all the hugs from the database.
28+
Used to get a list of the items in the database.
29+
'''
30+
hugs = await engine.find(Hug)
31+
return hugs
2132

2233

2334
@router.get("/hugs/count", response_model=int, tags=["hugs"])
2435
async def count_hugs():
36+
'''
37+
Count all the hugs in the database.
38+
Useful for determining pagination, optimizations.
39+
'''
2540
count = await engine.count(Hug)
2641
return count
2742

2843

2944
@router.get("/hugs/{id}", response_model=Hug, tags=["hugs"])
30-
async def get_tree_by_id(id: ObjectId):
45+
async def get_hug_by_id(id: ObjectId):
46+
'''
47+
Find a specific hug by ID.
48+
'''
3149
hug = await engine.find_one(Hug, Hug.id == id)
3250
if hug is None:
3351
raise HTTPException(404)

html/layout.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

images/fast-api-logo.png:Zone.Identifier

Lines changed: 0 additions & 4 deletions
This file was deleted.

nix-start-FastAPI

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
uvicorn app.main:app --reload
3+
uvicorn app.main:app --reload

requirements.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,26 @@ pydantic==1.7.2
77
pymongo==3.11.0
88
starlette==0.13.6
99
uvicorn==0.12.2
10+
click==7.1.2
11+
fastapi==0.61.2
12+
h11==0.11.0
13+
motor==2.3.0
14+
multipart==0.2.2
15+
odmantic==0.3.0
16+
pydantic==1.7.2
17+
pymongo==3.11.0
18+
starlette==0.13.6
19+
uvicorn==0.12.2
20+
aiofiles==0.6.0
21+
click==7.1.2
22+
fastapi==0.61.2
23+
h11==0.11.0
24+
Jinja2==2.11.2
25+
MarkupSafe==1.1.1
26+
motor==2.3.0
27+
multipart==0.2.2
28+
odmantic==0.3.0
29+
pydantic==1.7.2
30+
pymongo==3.11.0
31+
starlette==0.13.6
32+
uvicorn==0.12.2

static/css/FAVICON.ico

9.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)