Skip to content

Commit 5476b22

Browse files
committed
Show IP & details on endpoints
1 parent 0f025f6 commit 5476b22

File tree

6 files changed

+118
-35
lines changed

6 files changed

+118
-35
lines changed

.gitignore

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6-
# C extensions
6+
# C extensions & Java Class files
77
*.so
8+
*.class
89

910
# Distribution / packaging
1011
.Python
@@ -55,9 +56,14 @@ coverage.xml
5556
*.mo
5657
*.pot
5758

59+
# Docker
60+
docker-compose-*.yml
61+
*.swp*
62+
*.save*
63+
*cache
64+
5865
# Django stuff:
59-
*.log
60-
local_settings.py
66+
local*.py
6167
db.sqlite3
6268
db.sqlite3-journal
6369

@@ -71,6 +77,11 @@ instance/
7177
# Sphinx documentation
7278
docs/_build/
7379

80+
# Log files
81+
*.log
82+
log/
83+
logs/
84+
7485
# PyBuilder
7586
target/
7687

@@ -96,19 +107,25 @@ __pypackages__/
96107

97108
# Celery stuff
98109
celerybeat-schedule
99-
celerybeat.pid
110+
*.pid
111+
*.bak
112+
*.dat
113+
*.dir
100114

101115
# SageMath parsed files
102116
*.sage.py
103117

104118
# Environments
105-
.env
106-
.venv
119+
.env*
120+
.venv*
107121
env/
108122
venv/
123+
venv*/
124+
env*/
109125
ENV/
110-
env.bak/
111-
venv.bak/
126+
*old
127+
*env
128+
virtualenv.bat
112129

113130
# Spyder project settings
114131
.spyderproject
@@ -128,5 +145,28 @@ dmypy.json
128145
# Pyre type checker
129146
.pyre/
130147

131-
# Jetbeans IDE
132-
.idea/
148+
# Code Editor & IDE
149+
.vscode/
150+
.idea/
151+
152+
# Applications
153+
*.app
154+
*.exe
155+
*.war
156+
157+
# Large media files
158+
*.mp4
159+
*.tiff
160+
*.avi
161+
*.flv
162+
*.mov
163+
*.wmv
164+
165+
# Generated by MacOS
166+
.DS_Store
167+
168+
# Generated by Windows
169+
Thumbs.db
170+
171+
# Customer
172+
core/

README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,15 @@
77

88

99
## Initial Set-up ##
10-
### Virtual Environment ###
11-
```
12-
virtualenv .env
13-
```
14-
```
15-
source .env/bin/activate
16-
```
17-
1810
### Run using docker-compose ###
1911
* Make sure docker is running on your hardware
2012
```
2113
docker-compose up -d --build
2214
```
23-
* Go to [localhost](http://localhost:8000)
24-
25-
### Install & Run Fast Api ###
26-
```
27-
pip install fastapi
28-
```
15+
or
2916
```
30-
pip install "uvicorn[standard]"
17+
docker-compose -f docker-compose-local.yml up -d --build
3118
```
32-
```
33-
uvicorn main:app --reload
34-
```
19+
* Go to [localhost](http://localhost:8000)
20+
21+
### Install Ngrok for testing ###

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ services:
44
web:
55
build: .
66
command: bash -c "python main.py"
7+
environment:
8+
- IPDATA_API_KEY=test
79
volumes:
810
- .:/code
911
ports:
1012
- "8000:8000"
11-
1213
depends_on:
1314
- db
1415

@@ -25,7 +26,6 @@ services:
2526
- MYSQL_PORT=3307
2627
ports:
2728
- "3307:3306"
28-
2929
volumes:
3030
- mysql_data:/var/lib/mysql/
3131

main.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
import os, time
1+
import json
2+
import time
23
from typing import List
34

5+
import aiohttp
46
import uvicorn
5-
from fastapi import Depends, FastAPI, HTTPException
7+
from environs import Env
68

7-
from sql_app import crud, schemas, database, models
9+
# Environment
10+
env = Env()
11+
env.read_env()
12+
13+
14+
from fastapi import (
15+
FastAPI,
16+
HTTPException,
17+
Depends,
18+
Request
19+
)
20+
21+
from sql_app import (
22+
crud,
23+
schemas,
24+
database,
25+
models
26+
)
827
from sql_app.database import db_state_default
28+
from sql_app.utils import json_prettify
929

1030
database.db.connect()
1131
database.db.create_tables([models.User, models.Item])
1232
database.db.close()
1333

14-
1534
app = FastAPI()
1635
sleep_time = 10
1736

@@ -41,6 +60,23 @@ async def say_hello(name: str):
4160
return {"message": f"Hello {name}"}
4261

4362

63+
@app.get("/my-ip")
64+
async def show_ip(request: Request):
65+
client_host = request.client.host
66+
return {"client_host": client_host}
67+
68+
69+
@app.get("/my-info")
70+
async def slow_route(request: Request):
71+
client_ip = request.client.host
72+
ipdata_key = env("IPDATA_API_KEY", default="test") # Your Key Here
73+
ipdata_url = f"https://api.ipdata.co/{client_ip}?api-key={ipdata_key}"
74+
async with aiohttp.ClientSession() as session:
75+
async with session.get(ipdata_url) as response:
76+
json_dict = await response.json()
77+
return json_prettify(json_dict)
78+
79+
4480
@app.post("/users/", response_model=schemas.User, dependencies=[Depends(get_db)])
4581
def create_user(user: schemas.UserCreate):
4682
db_user = crud.get_user_by_email(email=user.email)
@@ -93,4 +129,10 @@ def read_slow_users(skip: int = 0, limit: int = 100):
93129

94130
# Run Fast API app
95131
if __name__ == "__main__":
96-
uvicorn.run(app, host="0.0.0.0", port=8000)
132+
uvicorn.run(
133+
app,
134+
host="0.0.0.0",
135+
port=8000,
136+
proxy_headers=True,
137+
forwarded_allow_ips='*',
138+
)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ contextvars~=2.4.0
66
pymysql~=1.0.2
77
cryptography~=36.0.1
88
environs~=9.5.0
9+
aiohttp~=3.8.1

sql_app/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import json
2+
3+
4+
def json_prettify(json_dict):
5+
return json.loads(
6+
json.dumps(
7+
json_dict,
8+
ensure_ascii=False,
9+
allow_nan=False,
10+
indent=4,
11+
separators=(", ", ": "),
12+
).encode("utf-8")
13+
)

0 commit comments

Comments
 (0)