From 2414e01f8b683c9aa042c2b37e1d3987fddedae5 Mon Sep 17 00:00:00 2001 From: Kristian Krsnik Date: Sat, 4 Jan 2025 20:05:01 +0100 Subject: [PATCH] added docker as an example --- Dockerfile | 28 ++++++++++++++++++++++++++++ docker-compose.yaml | 15 +++++++++++++++ pyproject.toml | 4 ++-- src/testdata/main.py | 19 ++++++++++++++++--- 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..769cc4a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# Nix builder +FROM nixos/nix:latest AS builder + +# Copy our source and setup our working dir. +COPY . /tmp/build +WORKDIR /tmp/build + +# Build our Nix environment +RUN nix \ + --extra-experimental-features "nix-command flakes" \ + --option filter-syscalls false \ + build + +# Copy the Nix store closure into a directory. The Nix store closure is the +# entire set of Nix store values that we need for our build. +RUN mkdir /tmp/nix-store-closure +RUN cp -r $(nix-store -qR result/) /tmp/nix-store-closure + +# Final image is based on scratch. We copy a bunch of Nix dependencies +# but they're fully self-contained so we don't need Nix anymore. +FROM scratch + +WORKDIR /app + +# Copy /nix/store +COPY --from=builder /tmp/nix-store-closure /nix/store +COPY --from=builder /tmp/build/result /app +CMD ["/app/bin/testdata"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..b11ba5f --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,15 @@ +services: + testdata: + image: result/latest + + build: + dockerfile: ./Dockerfile + + environment: + TESTDATA_HOST: 0.0.0.0 + TESTDATA_PORT: 1234 + TESTDATA_CONFIG: ./config.json + volumes: + - ./config.json:/app/config.json + - ./db.json:/app/db.json + - ./log.jsonl:/app/log.jsonl diff --git a/pyproject.toml b/pyproject.toml index 8e161b8..4a13832 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "testdata" -version = "1.1.0" +version = "1.2.0" requires-python = "~=3.12, <4" dependencies = [ "fastapi~=0.115.3", @@ -31,7 +31,7 @@ where = ["src"] testdata = ["py.typed"] [tool.autopep8] -max_line_length = 175 +max_line_length = 150 [tool.pylint.'MESSAGES CONTROL'] disable = [ diff --git a/src/testdata/main.py b/src/testdata/main.py index 7861a20..9ccb874 100644 --- a/src/testdata/main.py +++ b/src/testdata/main.py @@ -1,3 +1,4 @@ +import os import sys import argparse import asyncio @@ -11,9 +12,21 @@ def parse_args(args: list[str]): parser = argparse.ArgumentParser(formatter_class=formatter) - parser.add_argument('-c', '--config', type=argparse.FileType('r'), default='./config.json', help='Path to config file in JSON format.', ) - parser.add_argument('-l', '--listen', type=str, default='0.0.0.0', help='IP on which to listen.') - parser.add_argument('-p', '--port', type=int, default='8080', help='Port on which to serve the webserver.') + parser.add_argument( + '-c', '--config', type=argparse.FileType('r'), + default=os.environ['TESTDATA_CONFIG'] if 'TESTDATA_CONFIG' in os.environ else './config.json', + help='Path to config file in JSON format.' + ) + parser.add_argument( + '-l', '--listen', type=str, + default=os.environ['TESTDATA_HOST'] if 'TESTDATA_HOST' in os.environ else '0.0.0.0', + help='IP on which to listen.' + ) + parser.add_argument( + '-p', '--port', type=int, + default=os.environ['TESTDATA_PORT'] if 'TESTDATA_PORT' in os.environ else 8080, + help='Port on which to serve the webserver.' + ) return parser.parse_args(args)