diff --git a/Dockerfile b/Dockerfile index 1e5396e..96ec1d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,10 @@ FROM node:22.17.1-alpine -RUN mkdir -p /home/app +WORKDIR /usr/local/lib/task-service -COPY . /home/app +COPY package.json package-lock.json /usr/local/lib/task-service/ +RUN npm install -WORKDIR /home/app +COPY app/ . -CMD ["node", "/home/app/index.js", "--ConfigurationFile=/home/app/tasks.json"] +CMD ["node", "/usr/local/lib/task-service/index.js", "--ConfigurationFile=/etc/task-service/tasks.json"] diff --git a/Readme.md b/README.md similarity index 71% rename from Readme.md rename to README.md index 41b683c..0c7e461 100644 --- a/Readme.md +++ b/README.md @@ -6,8 +6,7 @@ This is project is meant for learning, practicing and trying out things, not for ## Requirements -- Node 18.15.0 -- ~~Node 0.10.48 - Ancient, but this version runs on my NAS, which is ancient as well.~~ +- Node 22.17.1 ## Setup @@ -44,13 +43,43 @@ This repository contains a Dockerfile to build a container with the task service To run the container, use the following command: - docker run -p 8000:1337 task-service +In Bash + +```bash +docker run \ + --rm \ + --detach \ + --name task-service \ + --publish 3000:3000 \ + --volume tasks:/usr/local/lib/task-service/data \ + --volume ${PWD}/tasks.json:/etc/task-service/tasks.json:ro \ + --volume ${PWD}/localhost.key:/usr/local/lib/task-service/localhost.key:ro \ + --volume ${PWD}/localhost.crt:/usr/local/lib/task-service/localhost.crt:ro \ + task-service +``` + +In PowerShell + +```powershell +docker run ` + --rm ` + --detach ` + --name task-service ` + --publish 3000:3000 ` + --volume tasks:/usr/local/lib/task-service/data ` + --volume ${PWD}/tasks.json:/etc/task-service/tasks.json:ro ` + --volume ${PWD}/localhost.key:/usr/local/lib/task-service/localhost.key:ro ` + --volume ${PWD}/localhost.crt:/usr/local/lib/task-service/localhost.crt:ro ` + task-service +``` -The container will be available on port 8000. +The container will be available on port 3000. ## Features - Supports HTTPS +- Authentication + - Basic - Not much more yet 😃 ## Testing @@ -99,5 +128,5 @@ Invoke-WebRequest "http://localhost:3000/task/99e587e6-6550-4601-9e2a-d40d2a2dce Delete a task ```powershell -Invoke-WebRequest "http://localhost:1337/task/99e587e6-6550-4601-9e2a-d40d2a2dce7b" -Credential $credential -AllowUnencryptedAuthentication -Method Delete +Invoke-WebRequest "http://localhost:3000/task/99e587e6-6550-4601-9e2a-d40d2a2dce7b" -Credential $credential -AllowUnencryptedAuthentication -Method Delete ``` diff --git a/app/config.js b/app/config.js index 402a584..cdf0def 100644 --- a/app/config.js +++ b/app/config.js @@ -1,4 +1,5 @@ const fs = require("fs"); +const persistence = require("./persistence/file/task.js") const configFileOptions = { encoding: "utf8" }; @@ -25,4 +26,10 @@ function getConfig() { } } } + +function setup() { + persistence.setup(); +} + +exports.setup = setup; exports.getConfig = getConfig; diff --git a/app/handler.js b/app/handler.js index 289abab..6497352 100644 --- a/app/handler.js +++ b/app/handler.js @@ -1,4 +1,4 @@ -const task = require("../task/task.js"); +const task = require("./task/task.js"); const { authenticate } = require("./auth/auth.js"); function postTask(req, res) { diff --git a/index.js b/app/index.js similarity index 86% rename from index.js rename to app/index.js index c7fff9c..49b0219 100644 --- a/index.js +++ b/app/index.js @@ -1,10 +1,11 @@ "use strict"; const fs = require("fs"); -const { getConfig } = require("./app/config.js"); -const { app } = require("./app/app.js"); +const { getConfig, setup } = require("./config.js"); +const { app } = require("./app.js"); const config = getConfig(); +setup(); let httpOptions, server; if (config !== undefined && config["HttpsOptions"] !== undefined) { diff --git a/persistence/file/task.js b/app/persistence/file/task.js similarity index 92% rename from persistence/file/task.js rename to app/persistence/file/task.js index 57ae83f..59cec18 100644 --- a/persistence/file/task.js +++ b/app/persistence/file/task.js @@ -3,6 +3,10 @@ const fs = require("node:fs"); const defaultDataDir = "./data/task/" const extension = ".json" +function setup() { + fs.mkdirSync(defaultDataDir, {recursive: true}); +} + function save(task, callback) { fs.writeFile( defaultDataDir + task.id + extension, @@ -55,3 +59,4 @@ exports.save = save exports.load = load exports.remove = remove exports.list = list +exports.setup = setup diff --git a/task/task.js b/app/task/task.js similarity index 100% rename from task/task.js rename to app/task/task.js diff --git a/docs/bruno/environments/.gitignore b/docs/bruno/environments/.gitignore new file mode 100644 index 0000000..b698f76 --- /dev/null +++ b/docs/bruno/environments/.gitignore @@ -0,0 +1 @@ +*.local.bru diff --git a/task/task.test.js b/tests/task/task.test.js similarity index 100% rename from task/task.test.js rename to tests/task/task.test.js