Skip to content

Commit

Permalink
Merge pull request #2 from jag-k/release/0.2.0
Browse files Browse the repository at this point in the history
Add support for Basic HTTP Auth
  • Loading branch information
jag-k authored Nov 30, 2023
2 parents bffe1ca + ec95ae2 commit b629f17
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 646 deletions.
19 changes: 19 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
changelog:
exclude:
labels:
- ignore-for-release
authors:
- octocat
categories:
- title: Breaking Changes 🛠
labels:
- breaking-change
- title: Exciting New Features 🎉
labels:
- enhancement
- title: Bug Fixes 🐛
labels:
- bug
- title: Other Changes
labels:
- "*"
13 changes: 12 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
workflow_dispatch:

permissions:
contents: read
contents: write

jobs:
build:
Expand Down Expand Up @@ -53,3 +53,14 @@ jobs:
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Release
uses: softprops/action-gh-release@v1
with:
body: |
Release ${{ steps.get_version.outputs.version }}
Docker image: `${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.version }}`
tag_name: ${{ steps.get_version.outputs.version }}
generate_release_notes: true
append_body: true
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@ app/config/local.py
NPSdir/
TORRENTdir/

docker-compose.yml
docker-compose.local.yml
wiki/
20 changes: 9 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/floatingpurr/sync_with_poetry
rev: 1.1.0
hooks:
- id: sync_with_poetry

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.260'
rev: 'v0.1.6'
hooks:
# Run the linter.
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
Expand All @@ -15,13 +23,3 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: "23.3.0"
hooks:
- id: black

- repo: https://github.com/floatingpurr/sync_with_poetry
rev: 0.2.1
hooks:
- id: sync_with_poetry
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
FROM python:3.11-slim

LABEL org.opencontainers.image.source = "https://github.com/jag-k/owntinfoil"

WORKDIR /app

ENV NPS_DIR=/nps
ENV TORRENT_DIR=/torrents
ENV PYTHONPATH=/app
ENV POETRY_VERSION=1.4.1
ENV POETRY_VERSION=1.7.1

RUN pip install "poetry==$POETRY_VERSION"
COPY pyproject.toml poetry.lock ./
Expand Down
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
# OwnTinfoil in Docker

[![GitHub Container Registry Build](https://img.shields.io/github/actions/workflow/status/jag-k/owntinfoil/deploy.yml?logo=github&label=Container%20Registry
)](https://github.com/users/jag-k/packages/container/package/owntinfoil)


Port on Python of the OwnTinfoil project.

## How to ...
More info you can find in [project's Wiki](https://github.com/jag-k/owntinfoil/wiki)

## Usage

### Requirements:

* [Docker](https://www.docker.com/)
* [Docker Compose](https://docs.docker.com/compose/)

### How to use
### Install:

1. Copy the [docker-compose.yml](docker-compose.yml) file in your own directory
2. Edit the file and change the `BOT_URL` and `BOT_KEY` (if you have it) variables.
3. Run `docker-compose up -d`
4. Enjoy!

### How to update

Maybe you need to be authorized in the GitHub Container Registry.
More about [👉 here 👈](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-with-a-personal-access-token-classic).

1. Run `docker-compose pull`
2. Run `docker-compose up -d`
3. Enjoy!

### How to stop

1. Run `docker-compose down`
2. Enjoy!
## How to ...

### How to remove
You can read the [Wiki page](https://github.com/jag-k/owntinfoil/wiki/How-to) for more information.

1. Run `docker-compose down --rmi all`
2. Enjoy!
Also, you can read the [FAQ page](https://github.com/jag-k/owntinfoil/wiki/FAQ) for more information.

## Development

### Requirements
### Requirements:

* [Python 3.11](https://www.python.org/)
* [Poetry 1.4.1](https://python-poetry.org/)
* [Poetry 1.7.1](https://python-poetry.org/)
* [Docker](https://www.docker.com/) (optional)

### How to install
Expand Down
15 changes: 13 additions & 2 deletions app/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# flake8: noqa: F403

import os
from pathlib import Path

from dotenv import load_dotenv

Expand All @@ -24,5 +23,17 @@
if not isinstance(TORRENT_DIR, Path):
TORRENT_DIR = Path(TORRENT_DIR)

# noinspection PyUnboundLocalVariable
NPS_DIR.mkdir(parents=True, exist_ok=True)
# noinspection PyUnboundLocalVariable
TORRENT_DIR.mkdir(parents=True, exist_ok=True)

HTTP_AUTH_USERS = {}
for key, user_and_password in os.environ.items():
if not key.startswith("HTTP_AUTH_USER_"):
continue
try:
user, password = user_and_password.split(":", 1)
HTTP_AUTH_USERS[user] = password
except ValueError:
continue
12 changes: 12 additions & 0 deletions app/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@

HOST: str = "0.0.0.0"
PORT: int = 8080

__all__ = (
"BASE_PATH",
"NPS_DIR",
"TORRENT_DIR",
"BOT_KEY",
"BOT_URL",
"OVERRIDE_TORRENTS",
"SUCCESS_MESSAGE",
"HOST",
"PORT",
)
19 changes: 17 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import warnings

from aiohttp import web
from config import HOST, NPS_DIR, PORT
from aiohttp_basicauth import BasicAuthMiddleware

from app.client import Client
from app.config import HOST, HTTP_AUTH_USERS, NPS_DIR, PORT
from app.generate_tfl import generate_tfl_file


class BasicAuth(BasicAuthMiddleware):
async def check_credentials(self, username: str, password: str, request):
return password == HTTP_AUTH_USERS.get(username)


async def redirect(_):
return web.HTTPFound("/shop.tfl")

Expand All @@ -29,7 +35,15 @@ async def periodic_request():

async def prepare_server(host: str, port: int) -> tuple[web.TCPSite, web.AppRunner]:
app = web.Application()
app.add_routes([web.get("/", redirect), web.get("/shop.tfl", handler), web.static("/", NPS_DIR)])
app.add_routes(
[
web.get("/", redirect),
web.get("/shop.tfl", handler),
web.static("/", NPS_DIR, follow_symlinks=True),
]
)
if HTTP_AUTH_USERS:
app.middlewares.append(BasicAuth())
warnings.filterwarnings("ignore", category=DeprecationWarning)
runner = web.AppRunner(app)
await runner.setup()
Expand All @@ -54,6 +68,7 @@ async def run():

def main():
loop = asyncio.get_event_loop()
# noinspection HttpUrlsUsage
print(f"Server started at http://{HOST}:{PORT}", file=sys.stderr)
print("Press CTRL+C to exit", file=sys.stderr)
try:
Expand Down
Loading

0 comments on commit b629f17

Please sign in to comment.