From e812c78d52726b57d47ebf228edc605a11cf17d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Aritz?= Date: Sun, 30 Jun 2024 18:46:35 +0200 Subject: [PATCH] env vars --- README.md | 2 ++ asset-api/.env.example | 2 ++ asset-api/.gitignore | 4 +++- asset-api/package.json | 5 +++-- asset-api/src/consts.js | 38 ++++++++++++++++++++++++++++++++++++ asset-api/{ => src}/index.js | 7 +------ asset-api/yarn.lock | 8 ++++++++ identity-api/.env.example | 2 +- identity-api/src/consts.ts | 2 +- 9 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 asset-api/.env.example create mode 100644 asset-api/src/consts.js rename asset-api/{ => src}/index.js (96%) diff --git a/README.md b/README.md index 48abd54..f71dabc 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ for conditions such as dementia. Over time, the idea evolved and is now a genera The Identity project is composed by a web-app and two servers. In the future, Docker containers may be built to ease the installation of this project. +The recommended Node.JS version is v21.5.0, others should also work. + ### Development and local testing #### `identity-web` diff --git a/asset-api/.env.example b/asset-api/.env.example new file mode 100644 index 0000000..970627e --- /dev/null +++ b/asset-api/.env.example @@ -0,0 +1,2 @@ +ASSET_API_ASSETS_FOLDER = "./.assets/" +ASSET_API_IDENTITY_API_ENDPOINT = "http://localhost:3000/" \ No newline at end of file diff --git a/asset-api/.gitignore b/asset-api/.gitignore index cef240f..de6cc60 100644 --- a/asset-api/.gitignore +++ b/asset-api/.gitignore @@ -13,4 +13,6 @@ .pnp.* .keys -.assets \ No newline at end of file +.assets + +.env \ No newline at end of file diff --git a/asset-api/package.json b/asset-api/package.json index 16b87ff..2a4aa89 100644 --- a/asset-api/package.json +++ b/asset-api/package.json @@ -1,18 +1,19 @@ { "name": "asset-api", - "main": "index.js", + "main": "src/index.js", "type": "module", "license": "AGPL-3.0-only", "packageManager": "yarn@4.3.0", "dependencies": { "@fastify/cors": "^9.0.1", "@fastify/multipart": "^8.3.0", + "dotenv": "^16.4.5", "fastify": "^4.28.0", "mime": "^4.0.3", "uuid": "^10.0.0" }, "scripts": { - "start": "node index.js", + "start": "node src/index.js", "lint:fix": "eslint --fix && prettier . --write", "lint": "eslint && prettier . --check" }, diff --git a/asset-api/src/consts.js b/asset-api/src/consts.js new file mode 100644 index 0000000..0f08e7d --- /dev/null +++ b/asset-api/src/consts.js @@ -0,0 +1,38 @@ +// Identity. Store your memories and mental belongings +// Copyright (C) 2024 SofĂ­a Aritz +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +import "dotenv/config"; + +const REQUIRED_VARS = [ + "ASSET_API_ASSETS_FOLDER", + "ASSET_API_IDENTITY_API_ENDPOINT" +]; + +REQUIRED_VARS.forEach((element) => { + if ( + process.env[element] == null || + (typeof process.env[element] === "string" && process.env[element].length === 0) + ) { + console.error(`Required environment variable was not set: ${element}`); + process.exit(1); + } +}); + +export const ASSET_API_LANDING_MESSAGE = process.env["ASSET_API_LANDING_MESSAGE"] || "asset-api v0.0.1"; +export const LISTEN_PORT = Number(process.env["ASSET_API_LISTEN_PORT"]) || 3001; +export const ASSETS_FOLDER = process.env["ASSET_API_ASSETS_FOLDER"]; +export const IDENTITY_API_ENDPOINT = process.env["ASSET_API_IDENTITY_API_ENDPOINT"]; +export const M2M_ALGORITHM = process.env["ASSET_API_M2M_ALGORITHM"] || "RSA-SHA512"; diff --git a/asset-api/index.js b/asset-api/src/index.js similarity index 96% rename from asset-api/index.js rename to asset-api/src/index.js index 43b8816..7d7f51e 100644 --- a/asset-api/index.js +++ b/asset-api/src/index.js @@ -24,19 +24,14 @@ import mime from "mime"; import { promisify } from "node:util"; import { pipeline } from "node:stream"; import cors from "@fastify/cors"; +import { M2M_ALGORITHM, ASSETS_FOLDER, ASSET_API_LANDING_MESSAGE, IDENTITY_API_ENDPOINT } from "./consts.js"; -const M2M_ALGORITHM = "RSA-SHA512"; const { private: M2M_PRIVATE_KEY, public: M2M_PUBLIC_KEY } = loadM2MKeys(); if (M2M_PRIVATE_KEY == null || M2M_PUBLIC_KEY == null) { console.error("Couldn't load keys"); process.exit(1); } -const ASSETS_FOLDER = "./.assets/"; -const ASSET_API_LANDING_MESSAGE = "asset-api v1.0.0"; - -const IDENTITY_API_ENDPOINT = "http://localhost:3000"; - const app = new Fastify({ logger: true, }); diff --git a/asset-api/yarn.lock b/asset-api/yarn.lock index f2924c9..d314340 100644 --- a/asset-api/yarn.lock +++ b/asset-api/yarn.lock @@ -296,6 +296,7 @@ __metadata: "@eslint/js": "npm:^9.5.0" "@fastify/cors": "npm:^9.0.1" "@fastify/multipart": "npm:^8.3.0" + dotenv: "npm:^16.4.5" eslint: "npm:9.x" fastify: "npm:^4.28.0" globals: "npm:^15.5.0" @@ -433,6 +434,13 @@ __metadata: languageName: node linkType: hard +"dotenv@npm:^16.4.5": + version: 16.4.5 + resolution: "dotenv@npm:16.4.5" + checksum: 10c0/48d92870076832af0418b13acd6e5a5a3e83bb00df690d9812e94b24aff62b88ade955ac99a05501305b8dc8f1b0ee7638b18493deb6fe93d680e5220936292f + languageName: node + linkType: hard + "escape-string-regexp@npm:^4.0.0": version: 4.0.0 resolution: "escape-string-regexp@npm:4.0.0" diff --git a/identity-api/.env.example b/identity-api/.env.example index e1d5eec..6134807 100644 --- a/identity-api/.env.example +++ b/identity-api/.env.example @@ -1,4 +1,4 @@ -IDENTITY_API_LANDING_MESSAGE = "identity-api v1.0.0" +IDENTITY_API_LANDING_MESSAGE = "identity-api v0.0.1" IDENTITY_API_JWT_SECRET = "cc7e0d44fd473002f1c42167459001140ec6389b7353f8088f4d9a95f2f596f2" IDENTITY_API_JWT_ALG = "HS256" IDENTITY_API_ASSET_API_ENDPOINT = "http://localhost:3001" diff --git a/identity-api/src/consts.ts b/identity-api/src/consts.ts index fb56113..3dec0d5 100644 --- a/identity-api/src/consts.ts +++ b/identity-api/src/consts.ts @@ -34,7 +34,7 @@ REQUIRED_VARS.forEach((element) => { } }); -export const IDENTITY_API_LANDING_MESSAGE = process.env["IDENTITY_API_LANDING_MESSAGE"] || "identity-api v1.0.0"; +export const IDENTITY_API_LANDING_MESSAGE = process.env["IDENTITY_API_LANDING_MESSAGE"] || "identity-api v0.0.1"; export const JWT_SECRET = new TextEncoder().encode(process.env["IDENTITY_API_JWT_SECRET"]); export const JWT_ALG = process.env["IDENTITY_API_JWT_ALG"]; export const LISTEN_PORT = Number(process.env["IDENTITY_API_LISTEN_PORT"]) || 3000;