-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: run container as non-root and minimize copies
- Loading branch information
1 parent
c9899fb
commit 983e545
Showing
5 changed files
with
84 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters