From 147c15afe27f392770d0cabb9d76b25f0910c9dc Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 27 Sep 2024 16:34:22 +0200 Subject: [PATCH] All but deletion implemented --- .dockerignore | 9 +++++++++ Dockerfile | 21 +++++++++++++++++++++ Makefile | 32 ++++++++++++++++++++++++++++++++ src/Daemon.php | 2 +- src/bootstrap.php | 9 +++++++++ tests/DaemonTest.php | 27 +++++++++++++++++++++++++-- 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c025afe --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +/Dockerfile +/.git +/.gitea +/var/* +/config/* +/.env +/data/*.db +/wrench.yaml +/.junk diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1eeaa22 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM php:8.3-alpine +ENV TZ=Europe/Stockholm + +ARG USERNAME=app + +RUN apk add --update tini +ENTRYPOINT [ "/sbin/tini", "--" ] + +RUN addgroup -S $USERNAME && adduser -S $USERNAME -G $USERNAME +RUN docker-php-ext-install pcntl + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN printf "[PHP]\ndate.timezone = \"%s\"\n" $TZ > /usr/local/etc/php/conf.d/tzone.ini + +WORKDIR /application +COPY . /application +RUN chown app:app /application/data + +USER $USERNAME + +CMD [ "php", "/application/bin/paramdb" ] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bcbce5e --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +.SILENT: +.PHONY: help + + +COLOR_RESET = \033[0m +COLOR_INFO = \033[32m +COLOR_COMMENT = \033[33m + +### Help +help: + printf "${COLOR_COMMENT}Usage:${COLOR_RESET}\n" + printf " make [target]\n\n" + printf "${COLOR_COMMENT}Available targets:${COLOR_RESET}\n" + awk '/^[a-zA-Z\-_0-9\.@]+:/ { \ + helpMessage = match(lastLine, /^### (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")); \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + printf " ${COLOR_INFO}%-16s${COLOR_RESET} %s\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) + +### Build docker image +build: + docker build --rm \ + -t dev.noccylabs.info/paramdb/paramdb \ + . + +### Push the image +push: + docker push dev.noccylabs.info/paramdb/paramdb diff --git a/src/Daemon.php b/src/Daemon.php index a677ca4..6ec80dc 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -130,7 +130,7 @@ class Daemon foreach ((array)$body as $change) { - echo "Applying change (collection={$collectionName}): ".json_encode($change)."\n"; + // echo "Applying change (collection={$collectionName}): ".json_encode($change)."\n"; $id = $change->id??null; $name = $change->key; $value = $change->value; diff --git a/src/bootstrap.php b/src/bootstrap.php index 3d0af31..cccaff4 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -5,5 +5,14 @@ require_once __DIR__."/../vendor/autoload.php"; define("APP_ROOT", dirname(__DIR__)); define("APP_DATA", APP_ROOT."/var"); +echo "ParamDB v0.0.0 (c) 2024, NoccyLabs\n"; +echo "Licensed under GNU GPL v3 or later\n\n"; + +echo "Application root: ".APP_ROOT."\n"; +echo "Data directory: ".APP_DATA."\n"; +echo "Listen address: 0.0.0.0:8000\n"; + + + $daemon = new NoccyLabs\ParamDb\Daemon(APP_DATA."/data.db"); $daemon->start(); diff --git a/tests/DaemonTest.php b/tests/DaemonTest.php index 39e0e0b..32fbf09 100644 --- a/tests/DaemonTest.php +++ b/tests/DaemonTest.php @@ -42,12 +42,35 @@ class DaemonTest extends \PHPUnit\Framework\TestCase public function testHandlingSetRequest() { - $this->markTestSkipped(); + $body = json_encode([ + [ 'key' => 'third', 'value' => 'd' ] + ]); + + $request = new ServerRequest("POST", "/test", [ "content-type"=>"application/json" ], $body); + $response = $this->daemon->onRequest($request); + + $this->assertEquals(200, $response->getStatusCode()); + + $request = new ServerRequest("GET", "/test"); + $response = $this->daemon->onRequest($request); + + $resolved = json_decode($response->getBody()->getContents(), true); + + $expect = [ + 'first' => 'c', + 'second' => 'c', + 'third' => 'd', + ]; + + $this->assertEquals($expect, $resolved); } public function testHandlingGetAllRequest() { - $this->markTestSkipped(); + $request = new ServerRequest("GET", "/test/all"); + $response = $this->daemon->onRequest($request); + + $this->assertEquals(200, $response->getStatusCode()); } public function testHandlingDeleteRequest()