-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (31 loc) · 856 Bytes
/
main.py
File metadata and controls
47 lines (31 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
class Fruit(BaseModel):
name: str
class Fruits(BaseModel):
fruits: List[Fruit]
app = FastAPI(debug=True)
origins = [
"http://localhost:8000",
# Add more origins here
]
app.add_middleware (
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
memory_db = {"fruits": []}
@app.get("/fruits", response_model=Fruits)
def get_fruits():
return Fruits(fruits=memory_db["fruits"])
@app.post("/fruits")
def add_fruit(fruit: Fruit):
memory_db["fruits"].append(fruit)
return fruit
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)