Skip to content

Commit

Permalink
refactor: run container as non-root and minimize copies
Browse files Browse the repository at this point in the history
  • Loading branch information
LeslieLeung committed Jan 11, 2024
1 parent c9899fb commit 983e545
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 74 deletions.
14 changes: 12 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
FROM python:3.11.6-slim
WORKDIR /app

RUN adduser --disabled-password --gecos '' appuser

RUN pip install poetry && poetry config virtualenvs.create false

ENV PORT=9000

# dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-dev
COPY . .
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]

USER appuser
COPY heimdallr ./heimdallr
COPY env.py main.py ./

CMD python -m uvicorn main:app --host 0.0.0.0 --port $PORT
70 changes: 0 additions & 70 deletions heimdallr/api/api.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
from fastapi import APIRouter

from env import get_env
from heimdallr.api import push, webhook
from heimdallr.channel import (
Bark,
BarkMessage,
Chanify,
ChanifyMessage,
Channel,
Email,
EmailMessage,
Message,
PushDeer,
PushDeerMessage,
Pushover,
PushoverMessage,
WecomApp,
WecomMessage,
WecomWebhook,
)
from heimdallr.response import Response, success

router = APIRouter()
router.include_router(push.push_router)
router.include_router(webhook.webhook_router)


def serve(
channel: str, title: str = "", body: str = "", key: str = "", jump_url: str = ""
):
env = get_env()
if env.key != "" and key != env.key:
return {"code": -1, "message": "key not authorized"}
channels = channel.split("+")
senders = []
for chan in channels:
message: Message
sender: Channel
match chan:
case "bark":
message = BarkMessage(title, body, jump_url)
sender = Bark(message)
case "wecom-webhook":
message = WecomMessage(title, body)
sender = WecomWebhook(message)
case "wecom-app":
message = WecomMessage(title, body)
sender = WecomApp(message)
case "pushdeer":
message = PushDeerMessage(title, body)
sender = PushDeer(message)
case "pushover":
message = PushoverMessage(title, body)
sender = Pushover(message)
case "chanify":
message = ChanifyMessage(title, body)
sender = Chanify(message)
case "email":
message = EmailMessage(title, body)
sender = Email(message)
case _:
return {"code": 2, "message": f"{chan} is not supported"}
senders.append(sender)

errors = {}
for sender in senders:
rs, msg = sender.send()
if not rs:
errors[sender.get_name()] = msg

if len(errors) == 0:
return success()
err_msg = ""
for err in errors.items():
err_msg += f"{err[0]} return: {err[1]}."
return Response(1, err_msg).render()
70 changes: 70 additions & 0 deletions heimdallr/api/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from env import get_env
from heimdallr.channel import (
Bark,
BarkMessage,
Chanify,
ChanifyMessage,
Channel,
Email,
EmailMessage,
Message,
PushDeer,
PushDeerMessage,
Pushover,
PushoverMessage,
WecomApp,
WecomMessage,
WecomWebhook,
)
from heimdallr.response import Response, success


def serve(
channel: str, title: str = "", body: str = "", key: str = "", jump_url: str = ""
):
env = get_env()
if env.key != "" and key != env.key:
return {"code": -1, "message": "key not authorized"}
channels = channel.split("+")
senders = []
for chan in channels:
message: Message
sender: Channel
match chan:
case "bark":
message = BarkMessage(title, body, jump_url)
sender = Bark(message)
case "wecom-webhook":
message = WecomMessage(title, body)
sender = WecomWebhook(message)
case "wecom-app":
message = WecomMessage(title, body)
sender = WecomApp(message)
case "pushdeer":
message = PushDeerMessage(title, body)
sender = PushDeer(message)
case "pushover":
message = PushoverMessage(title, body)
sender = Pushover(message)
case "chanify":
message = ChanifyMessage(title, body)
sender = Chanify(message)
case "email":
message = EmailMessage(title, body)
sender = Email(message)
case _:
return {"code": 2, "message": f"{chan} is not supported"}
senders.append(sender)

errors = {}
for sender in senders:
rs, msg = sender.send()
if not rs:
errors[sender.get_name()] = msg

if len(errors) == 0:
return success()
err_msg = ""
for err in errors.items():
err_msg += f"{err[0]} return: {err[1]}."
return Response(1, err_msg).render()
2 changes: 1 addition & 1 deletion heimdallr/api/push.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, Form, Request
from pydantic import BaseModel

from heimdallr.api.api import serve
from heimdallr.api.base import serve
from heimdallr.channel import Channel, WecomApp, WecomMessage, WecomWebhook
from heimdallr.exception import WecomException

Expand Down
2 changes: 1 addition & 1 deletion heimdallr/api/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter, Request

from heimdallr.api.api import serve
from heimdallr.api.base import serve
from heimdallr.webhook.github_star import GithubStarWebhook

webhook_router = APIRouter()
Expand Down

0 comments on commit 983e545

Please sign in to comment.