Skip to content

Commit

Permalink
chore:init
Browse files Browse the repository at this point in the history
  • Loading branch information
remiallain committed Mar 26, 2024
0 parents commit b1b8bfe
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Create and publish a Docker image

on:
push:
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=schedule
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM python:3.9-alpine
WORKDIR /app
COPY server.py /app/
CMD ["python", "/app/server.py"]
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Catch-all Server

This is a catch-all server that could be used to return a custom response for any request.

## Example

```yaml
services:
not-found:
image: ghcr.io/remiallain/catch-all-server:latest
environment:
SERVER_NAME: "Not Found"
STATUS_CODE: 404
PORT: 8000
TYPE: text/html
CONTENT: >
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Not Found</title>
</head>
<body>
<h1>Not Found</h1>
</body>
</html>
ports:
- "8000:8000"
```
You can serve a JSON instead of HTML
```yaml
TYPE: application/json
CONTENT: '{"error": "not-found"}'
```
## Use with Traefik redirect
This server can be used with Traefik to redirect to a custom 404 page when there is no route to a service (wildcard routing).
```yaml
services:
not-found:
image: ghcr.io/remiallain/catch-all-server:latest
environment:
CONTENT: "<h1>Oops, this service does not exist</h1>"
labels:
# Middleware redirect to not-found.$DOMAIN
- "traefik.http.middlewares.catch-all-redirectregex.redirectregex.regex=.*"
- "traefik.http.middlewares.catch-all-redirectregex.redirectregex.replacement=http://not-found.${DOMAIN}"
- "traefik.http.middlewares.catch-all-redirectregex.redirectregex.permanent=false"
# Catch-all route -> Non-existing service (Priority low = last rule to execute)
- "traefik.http.routers.catch-all-not-found.rule=HostRegexp(`{host:.+}`)"
- "traefik.http.routers.catch-all-not-found.priority=1"
- "traefik.http.routers.catch-all-not-found.service=noop@internal"
- "traefik.http.routers.catch-all-not-found.entrypoints=http"
- "traefik.http.routers.catch-all-not-found.middlewares=catch-all-redirectregex"
# Router not-found.$DOMAIN
- "traefik.enable=true"
- "traefik.http.services.not-found-svc.loadbalancer.server.port=8000"
- "traefik.http.routers.not-found.rule=Host(`not-found.${DOMAIN}`)"
- "traefik.http.routers.not-found.service=not-found-svc"
- "traefik.http.routers.not-found.entrypoints=http"
```
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
not-found:
build: .
environment:
PORT: 8000
TYPE: text/html
CONTENT: >
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body>
</html>
SERVER_NAME: "Not Found"
STATUS_CODE: 404
ports:
- "8000:8000"
34 changes: 34 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import http.server
import socketserver
import os

class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def handle_one_request(self):
try:
# Reading the request line
self.raw_requestline = self.rfile.readline(65537)
self.requestline = ''
self.request_version = ''
self.command = ''
self.server_version = os.environ.get("SERVER_NAME", "Dude, where's my server ?")
self.sys_version = ''

# Sending an '200 OK' response
self.send_response(int(os.environ.get('STATUS_CODE', '200')))
self.send_header("Content-type", os.environ.get("TYPE", "text/html"))
self.end_headers()

# Custom HTML code
html = os.environ.get('CONTENT', '<h1>Not found</h1>')

# Writing the HTML contents with UTF-8
self.wfile.write(bytes(html, "utf8"))
self.wfile.flush() #actually send the response if not already done.
except Exception as e:
self.close_connection = True
return

server = socketserver.TCPServer(("", int(os.environ.get('PORT', "8000"))), HttpRequestHandler)

# Start the server
server.serve_forever()

0 comments on commit b1b8bfe

Please sign in to comment.