From 5dedcaa47b4e6506d52878a86ce7e0f6e772b06e Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 4 Apr 2024 19:03:58 +0200 Subject: [PATCH 01/10] Remove devenv-up --- flake.nix | 3 --- 1 file changed, 3 deletions(-) diff --git a/flake.nix b/flake.nix index 7fc98b5..c5283e1 100644 --- a/flake.nix +++ b/flake.nix @@ -24,9 +24,6 @@ poetry2nix = inputs.poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }; in { - packages = { - devenv-up = self.devShells.${system}.default.config.procfileScript; - }; devShells = let config = self.devShells.${system}.default.config; From c204ac6ea3c53b40b3fd243023832da212e1b8ed Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 4 Apr 2024 19:04:06 +0200 Subject: [PATCH 02/10] Reorder variables --- flake.nix | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index c5283e1..4b245db 100644 --- a/flake.nix +++ b/flake.nix @@ -22,20 +22,20 @@ let pkgs = nixpkgs.legacyPackages.${system}; poetry2nix = inputs.poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }; + env = poetry2nix.mkPoetryEnv { + projectDir = ./.; + python = pkgs.python312; + }; + tests = pkgs.writeShellScriptBin "python-test" '' + trap "process-compose down &> /dev/null" EXIT + process-compose up --tui=false & + pytest --cov=src tests.py + ''; in { devShells = let config = self.devShells.${system}.default.config; - env = poetry2nix.mkPoetryEnv { - projectDir = ./.; - python = pkgs.python312; - }; - tests = pkgs.writeShellScriptBin "python-test" '' - trap "process-compose down &> /dev/null" EXIT - process-compose up --tui=false & - pytest --cov=src tests.py - ''; in { default = devenv.lib.mkShell { From ea4750a0239c4bc6aea74e4a76786b73decace84 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 18:52:50 +0200 Subject: [PATCH 03/10] Rename directory --- conftest.py | 2 +- flake.nix | 4 ++-- {src => snapbin}/__init__.py | 0 {src => snapbin}/main.py | 0 {src => snapbin}/utils.py | 0 tests.py | 2 +- 6 files changed, 4 insertions(+), 4 deletions(-) rename {src => snapbin}/__init__.py (100%) rename {src => snapbin}/main.py (100%) rename {src => snapbin}/utils.py (100%) diff --git a/conftest.py b/conftest.py index 68536bd..b974c7a 100644 --- a/conftest.py +++ b/conftest.py @@ -1,6 +1,6 @@ import pytest -import src.main as snapbin +import snapbin.main as snapbin @pytest.fixture() diff --git a/flake.nix b/flake.nix index 4b245db..1186124 100644 --- a/flake.nix +++ b/flake.nix @@ -29,7 +29,7 @@ tests = pkgs.writeShellScriptBin "python-test" '' trap "process-compose down &> /dev/null" EXIT process-compose up --tui=false & - pytest --cov=src tests.py + pytest --cov=snapbin tests.py ''; in { @@ -57,7 +57,7 @@ processes = { webserver = { process-compose.depends_on.redis.condition = "process_started"; - exec = "gunicorn src.main:app"; + exec = "gunicorn snapbin.main:app"; }; }; services.redis.enable = true; diff --git a/src/__init__.py b/snapbin/__init__.py similarity index 100% rename from src/__init__.py rename to snapbin/__init__.py diff --git a/src/main.py b/snapbin/main.py similarity index 100% rename from src/main.py rename to snapbin/main.py diff --git a/src/utils.py b/snapbin/utils.py similarity index 100% rename from src/utils.py rename to snapbin/utils.py diff --git a/tests.py b/tests.py index b233528..8f555f6 100644 --- a/tests.py +++ b/tests.py @@ -8,7 +8,7 @@ from freezegun import freeze_time from werkzeug.exceptions import BadRequest # noinspection PyPep8Naming -import src.main as snapbin +import snapbin.main as snapbin def test_get_password(): From ba9a491ac049027ef8c6724cbfa1966c48e394ce Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 18:53:12 +0200 Subject: [PATCH 04/10] Correct path to README --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f6b1fb5..3d7898b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ version = "0.1.0" description = "A little service to store and encrypt information, deleting it once it's retrieved." authors = ["Andreas Zweili "] license = "GPL3.0+" -readme = "README.md" +readme = "README.org" [tool.pylint."MESSAGES CONTROL"] disable = [ From 64d70e1a076d37321c82d617fc097fa4fb74a056 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 19:26:14 +0200 Subject: [PATCH 05/10] Build a container image with poetry2nix --- flake.nix | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 1186124..09ceb2a 100644 --- a/flake.nix +++ b/flake.nix @@ -22,7 +22,7 @@ let pkgs = nixpkgs.legacyPackages.${system}; poetry2nix = inputs.poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }; - env = poetry2nix.mkPoetryEnv { + application = poetry2nix.mkPoetryApplication { projectDir = ./.; python = pkgs.python312; }; @@ -33,6 +33,23 @@ ''; in { + packages = { + snapbin-image = pkgs.dockerTools.buildImage { + name = "snapbin"; + tag = "latest"; + copyToRoot = pkgs.buildEnv { + name = "image-root"; + paths = [ application.dependencyEnv ]; + }; + config = { + Cmd = [ + "${application.dependencyEnv}/bin/gunicorn" + "--bind=0.0.0.0" + "snapbin.main:app" + ]; + }; + }; + }; devShells = let config = self.devShells.${system}.default.config; From 138e239b8aed580c5fd39c05cad3fe1436e1b22a Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 19:40:50 +0200 Subject: [PATCH 06/10] Update README --- README.org | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.org b/README.org index b636d8d..d8b64d6 100644 --- a/README.org +++ b/README.org @@ -10,21 +10,24 @@ This project was inspired by Snappass and was previously licensed under MIT. ** Tests :verb: -*** Store secret -POST http://localhost:8000 +# To run this tests you need to have Emacs and the package [[https://github.com/federicotdn/verb][verb]] installed. +# Then you can press the following key combination on the headings {C-c C-r C-r}. + +template http://localhost:8000 Accept: application/json Content-Type: application/json +*** Store secret +POST / + { "ttl": 604800, "password": "Foo" } *** Retrieve secret -POST http://localhost:8000/get-secret -Accept: application/json -Content-Type: application/json +POST /get-secret { -"key": "snappass941ad23f9e4b4e7fa699854e48f83ff9~tQVYlz56h1bzXVlrCY5W8rYER1sXGtvYtRHrMyXLhSc=" +"key": "{{(verb-json-get (oref verb-last body) "key")}}" } From 369ec3508319234db52239fe553abcb7a3301703 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 19:42:41 +0200 Subject: [PATCH 07/10] Add matching redis image --- flake.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/flake.nix b/flake.nix index 09ceb2a..4b3aa68 100644 --- a/flake.nix +++ b/flake.nix @@ -49,6 +49,17 @@ ]; }; }; + redis-image = pkgs.dockerTools.buildImage { + name = "redis"; + tag = "latest"; + copyToRoot = pkgs.buildEnv { + name = "image-root"; + paths = [ pkgs.redis ]; + }; + config = { + Cmd = [ "${pkgs.redis}/bin/redis-server" ]; + }; + }; }; devShells = let From dcc40fe8832c07941ae37f3d8a2894aee7cc6924 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 19 Apr 2024 19:50:27 +0200 Subject: [PATCH 08/10] Add docker-compose.yml --- docker-compose.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4e11c20 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +services: + app: + image: snapbin + ports: + - "8000:8000" + redis: + image: redis From 77fbcb3945ea1c7e238bd9787ac122eb0cdb8540 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 20 Apr 2024 12:30:04 +0200 Subject: [PATCH 09/10] Add poetry2nix env --- flake.nix | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flake.nix b/flake.nix index 4b3aa68..aa098b5 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,14 @@ projectDir = ./.; python = pkgs.python312; }; + env = poetry2nix.mkPoetryEnv { + projectDir = ./.; + python = pkgs.python312; + groups = [ "dev" ]; + editablePackageSources = { + snapbin = ./snapbin; + }; + }; tests = pkgs.writeShellScriptBin "python-test" '' trap "process-compose down &> /dev/null" EXIT process-compose up --tui=false & From ccb80f0afac8fc12532174ed7114027e3fef1b50 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 20 Apr 2024 12:33:48 +0200 Subject: [PATCH 10/10] Build and publish containers --- .github/workflows/build_containers.yml | 65 ++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 .github/workflows/build_containers.yml diff --git a/.github/workflows/build_containers.yml b/.github/workflows/build_containers.yml new file mode 100644 index 0000000..9ede22d --- /dev/null +++ b/.github/workflows/build_containers.yml @@ -0,0 +1,65 @@ +on: + push: + branches: [ "main" ] + workflow_dispatch: + +jobs: + build-snapbin-image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.1 + - uses: cachix/install-nix-action@v26 + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Build snapbin image + run: nix build .#snapbin-image && docker load < ./result + + - name: Log into registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Push image + env: + IMAGE_NAME: snapbin + run: | + IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + # Use Docker `latest` tag convention + [ "$VERSION" == "main" ] && VERSION=latest + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION + + build-redis-image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.1 + - uses: cachix/install-nix-action@v26 + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Build redis image + run: nix build .#redis-image && docker load < ./result + + - name: Log into registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Push image + env: + IMAGE_NAME: redis + run: | + IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + # Use Docker `latest` tag convention + [ "$VERSION" == "main" ] && VERSION=latest + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION