Merge pull request #5 from Nebucatnetzer/outputs

Outputs
This commit is contained in:
Andreas Zweili 2024-04-20 13:30:41 +02:00 committed by GitHub
commit e90f34b1b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 128 additions and 20 deletions

65
.github/workflows/build_containers.yml vendored Normal file
View File

@ -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

View File

@ -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")}}"
}

View File

@ -1,6 +1,6 @@
import pytest
import src.main as snapbin
import snapbin.main as snapbin
@pytest.fixture()

7
docker-compose.yml Normal file
View File

@ -0,0 +1,7 @@
services:
app:
image: snapbin
ports:
- "8000:8000"
redis:
image: redis

View File

@ -22,23 +22,56 @@
let
pkgs = nixpkgs.legacyPackages.${system};
poetry2nix = inputs.poetry2nix.lib.mkPoetry2Nix { inherit pkgs; };
application = poetry2nix.mkPoetryApplication {
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 &
pytest --cov=snapbin tests.py
'';
in
{
packages = {
devenv-up = self.devShells.${system}.default.config.procfileScript;
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"
];
};
};
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
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 {
@ -60,7 +93,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;

View File

@ -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 <andreas@zweili.ch>"]
license = "GPL3.0+"
readme = "README.md"
readme = "README.org"
[tool.pylint."MESSAGES CONTROL"]
disable = [

View File

@ -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():