Skip to content

Commit 33b7f00

Browse files
committed
✨ Implement dynamic image path and refactor Docker compose
With these changes, we have made two main modifications. Firstly, we have integrated a dynamic image path based on the environment's port. This ensures that each instance will have its own unique storage location, preventing potential overwrites and conflicts. Secondly, there is a significant refactor of the 'compose.yml'. The 'nginx' configuration is removed, and instead, a new service named 'api2' is added. This change aims to simplify the setup and use a dedicated API instance with its port. The rationale behind these changes is to enhance scalability, maintainability, and reduce complexities associated with the previous 'nginx' setup. Additionally, the dynamic image path will allow better flexibility when deploying multiple instances of the application.
1 parent e2e30f0 commit 33b7f00

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

api/images/.gitkeep

Whitespace-only changes.

api/main.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@
77
from fastapi.responses import HTMLResponse
88

99
app = FastAPI()
10+
image_path = 'images/' + os.environ['PORT'] + '.jpg'
1011

11-
@app.post('/api/upload/')
12+
@app.on_event("startup")
13+
async def startup_event():
14+
# You can load neural networks, etc. here.
15+
if not os.path.exists('images'):
16+
os.makedirs('images')
17+
18+
@app.post('/api/upload')
1219
async def image_processing(file: UploadFile = File(...)):
1320
image_content = await file.read()
1421
print('Received image')
15-
with open('image.jpg', 'wb') as f:
22+
with open(image_path, 'wb') as f:
1623
f.write(image_content)
1724

1825
# Write the code to process the image here.
1926
# For example, read the image using OpenCV and convert it to grayscale.
20-
img = cv2.imread('image.jpg')
27+
img = cv2.imread(image_path)
2128
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
22-
cv2.imwrite('image.jpg', gray)
23-
with open('image.jpg', 'rb') as f:
29+
cv2.imwrite(image_path, gray)
30+
with open(image_path, "rb") as f:
2431
encoded_string = base64.b64encode(f.read()).decode()
25-
print(encoded_string)
26-
27-
response = {'processed_image': encoded_string}
32+
os.remove(image_path)
33+
response = {'processed': encoded_string}
2834
return response
2935

3036
if __name__ == '__main__':

compose.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ services:
4848
- ./api:/usr/src/app
4949
command: bash -c 'python main.py'
5050

51-
nginx:
52-
image: nginx:1.23.1-alpine
51+
api2:
52+
container_name: 'api2'
53+
build:
54+
context: ./api
55+
dockerfile: Dockerfile
56+
environment:
57+
- HOST=0.0.0.0
58+
- PORT=8002
59+
shm_size: '5gb'
60+
tty: true
5361
ports:
54-
- '80:80'
55-
expose:
56-
- 80
62+
- '8002:8002'
5763
volumes:
58-
- ./default.conf:/etc/nginx/conf.d/default.conf
59-
ulimits:
60-
core:
61-
hard: 0
62-
soft: 0
63-
depends_on:
64-
- frontend
65-
- backend
64+
- ./api:/usr/src/app
65+
command: bash -c 'python main.py'
6666

default.conf

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

0 commit comments

Comments
 (0)