This commit is contained in:
Peder Bergebakken Sundt 2022-08-27 22:27:55 +02:00
parent 3e69ac3b91
commit 1497e3e679
6 changed files with 172 additions and 6 deletions

15
.envrc
View File

@ -2,7 +2,20 @@
# This file is loaded with `direnv`.
# It enters you into the poetry venv, removing the need for `poetry run`.
poetry run true # create venv if it doesn't exist
if command -v nix >/dev/null; then
use nix -p poetry
fi
# Instead of using the flake, we use poetry to manage a development venv
# We only use poetry2nix for deployment
# create venv if it doesn't exist
poetry run true
# enter venv
export VIRTUAL_ENV=$(poetry env info --path)
export POETRY_ACTIVE=1
PATH_add "$VIRTUAL_ENV/bin"
if ! command -v sanic >/dev/null; then
poetry install
fi

View File

@ -14,7 +14,7 @@ The API is described and can be tested at `http:/localhost:8080/swagger` while t
Gregorz manages a MPV process, meaning you need to have MPV installed on your system. Look for it in your package manager.
sudo pip install git+https://github.com/Programvareverkstedet/grzegorz#master
sanic grzegorz.app --host :: --port 80 --fast
sanic grzegorz.app --host :: --port 8080
Details are over [here](https://sanic.dev/en/guide/deployment/running.html#running-via-command).

92
flake.lock Normal file
View File

@ -0,0 +1,92 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1661621241,
"narHash": "sha256-qUsCgbZNM9b8yOV2XWB//0/qUduDAnbB9WFXr8XzWB0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f214afa5fb6452900e27776735db21f5092261b8",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1661621241,
"narHash": "sha256-qUsCgbZNM9b8yOV2XWB//0/qUduDAnbB9WFXr8XzWB0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f214afa5fb6452900e27776735db21f5092261b8",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"poetry2nix": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1661298951,
"narHash": "sha256-R4zRiXIYic14aNJGuWvBZBblyt6I0GNzgBEQAXhchf4=",
"owner": "nix-community",
"repo": "poetry2nix",
"rev": "e1e95bbf61be2d150613f5e0560d70c4b316ba4a",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "poetry2nix",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"poetry2nix": "poetry2nix"
}
}
},
"root": "root",
"version": 7
}

55
flake.nix Normal file
View File

@ -0,0 +1,55 @@
{
description = "A REST API for managing a MPV instance over via a RPC socket";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
inputs.poetry2nix.url = "github:nix-community/poetry2nix";
outputs = { self, nixpkgs, flake-utils, poetry2nix }:
{
# Nixpkgs overlay providing the application
overlay = nixpkgs.lib.composeManyExtensions [
poetry2nix.overlay
(final: prev: {
# The application
grzegorz = prev.poetry2nix.mkPoetryApplication {
projectDir = ./.;
};
})
];
} // (flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlay ];
};
entrypoint = pkgs.writeShellApplication {
name = "grzegorz-run";
runtimeInputs = [
pkgs.grzegorz
pkgs.mpv
];
text = ''
SPIS_MEG=()
if test -z "$*"; then
>&2 echo "DEBUG: No args provided, running with defaults...."
SPIS_MEG=("--host" "::" "--port" "8080")
fi
(
set -x
sanic grzegorz.app "''${SPIS_MEG[@]}" "$@"
)
'';
};
in {
packages = {
inherit (pkgs) grzegorz;
grzegorz-run = entrypoint;
};
apps = {
default.type = "app";
default.program = "${entrypoint}/bin/grzegorz-run";
};
}));
}

View File

@ -1,6 +1,7 @@
from sanic import Sanic
from sanic_openapi import swagger_blueprint#, openapi2_blueprint
from pathlib import Path
import traceback
from . import mpv
from . import api
@ -31,18 +32,22 @@ app.blueprint(swagger_blueprint)
# mpv:
app.config["mpv_control"] = mpv.MPVControl()
async def runMPVControl():
app.config["mpv_control"] = mpv.MPVControl()
try:
await app.config["mpv_control"].run()
except:
traceback.print_exc()
finally:
app.stop()
pass #app.stop()
app.add_task(runMPVControl())
# populate playlist
async def ensure_splash():
here = Path(__file__).parent.resolve()
while not "mpv_control" in app.config:
await None
mpv_control: mpv.MPVControl = app.config["mpv_control"]
playlist = await mpv_control.playlist_get()
if len(playlist) == 0:

View File

@ -13,7 +13,7 @@ from . import nyasync
class MPV:
# TODO: move this to /tmp or /var/run ?
# TODO: make it configurable with an env variable?
_ipc_endpoint = Path("mpv_ipc.socket")
_ipc_endpoint = Path(f"mpv_ipc.socket")
def __init__(self):
self.requests = nyasync.Queue()
@ -48,6 +48,7 @@ class MPV:
)
async def connect(self, *, timeout=10):
await asyncio.sleep(0.5)
t = time.time()
while self.is_running and time.time() - t < timeout:
try:
@ -154,7 +155,7 @@ class MPVControl:
"force",
"on"
)
code = await process.wait()
code = await p.wait()
#Shorthand command requests: