From 52127adb08cff9e6bf6bad0125c3b5f7e0d04db2 Mon Sep 17 00:00:00 2001 From: Parker Date: Mon, 11 Nov 2024 23:57:44 -0600 Subject: [PATCH] Merge API+APP in Docker --- Dockerfile | 21 +++++++++++++++++---- api/main.py | 18 ++++-------------- config.py | 6 +++--- docker-compose.yaml | 5 +++-- models.py | 1 - nginx.conf | 24 ++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile index 418ba9d..be8870e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,25 @@ -FROM python:3.11-slim +FROM node:18-slim AS build-ui + +WORKDIR /app +COPY app/ ./ +RUN yarn install +RUN yarn build + +FROM python:3.11-slim AS api LABEL org.opencontainers.image.source="https://github.com/PacketParker/LinkLogger" LABEL maintainer="parker " WORKDIR / - COPY . . RUN pip install -r requirements.txt -ENTRYPOINT [ "python" ] -CMD [ "-u", "linklogger.py" ] \ No newline at end of file +RUN apt-get update && apt-get install -y nginx && \ + rm -rf /var/lib/apt/lists/* + +# Move the built files into the nginx share +COPY --from=build-ui /app/dist /usr/share/nginx/html +# Replace the default site with the LinkLogger config +COPY nginx.conf /etc/nginx/sites-enabled/default + +CMD service nginx start && python -u linklogger.py \ No newline at end of file diff --git a/api/main.py b/api/main.py index 8ce333f..8be260d 100644 --- a/api/main.py +++ b/api/main.py @@ -67,20 +67,10 @@ async def log_redirect( return RedirectResponse(url=link_record.redirect_link) -# Redirect /api -> /api/docs -@app.get("/api") -async def redirect_to_docs(): - return RedirectResponse(url="/docs") - - # Custom handler for 404 errors @app.exception_handler(HTTP_404_NOT_FOUND) async def custom_404_handler(request: Request, exc: HTTPException): - # If the request is from /api, return a JSON response - if request.url.path.startswith("/api"): - return JSONResponse( - status_code=404, - content={"message": "Resource not found"}, - ) - # Otherwise, redirect to the login page - return RedirectResponse(url="/login") + return JSONResponse( + status_code=404, + content={"message": "Resource not found"}, + ) diff --git a/config.py b/config.py index bb878bf..6ed420c 100644 --- a/config.py +++ b/config.py @@ -58,9 +58,9 @@ def load_config(): with open(file_path, "w") as f: f.write( """ - ip_to_location: "" - api_key: "" - """ +config: + ip_to_location: + api_key:""" ) LOG.critical( "`config.yaml` was not found, a template has been created." diff --git a/docker-compose.yaml b/docker-compose.yaml index 0f9f033..6629c61 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -2,7 +2,8 @@ services: linklogger: container_name: linklogger image: ghcr.io/packetparker/linklogger:latest - network_mode: host volumes: - /path/on/system:/data - restart: on-failure \ No newline at end of file + ports: + - 5100:5000 + restart: always \ No newline at end of file diff --git a/models.py b/models.py index 17f0936..6061661 100644 --- a/models.py +++ b/models.py @@ -6,7 +6,6 @@ from sqlalchemy import ( Text, DateTime, ) -import datetime from database import Base diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..c07ddcb --- /dev/null +++ b/nginx.conf @@ -0,0 +1,24 @@ +server { + listen 5000; + + # Serve React static files + location / { + root /usr/share/nginx/html; + try_files $uri $uri/ /index.html; + } + + # Proxy API requests to FastAPI + location /api/ { + proxy_pass http://localhost:5252; + set_real_ip_from 0.0.0.0/0; + real_ip_header X-Forwarded-For; + real_ip_recursive on; + } + # Proxy short link requests to FastAPI + location /c/ { + proxy_pass http://localhost:5252; + set_real_ip_from 0.0.0.0/0; + real_ip_header X-Forwarded-For; + real_ip_recursive on; + } +} \ No newline at end of file