From f1248b67f46a6f47d7ead9cb2e791bcf2f8dcfe3 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Sat, 29 Oct 2022 00:27:06 +0300 Subject: [PATCH 01/11] update config-loader and remove unnecessary packages --- package.json | 4 +--- yarn.lock | 21 +++++---------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 8995853b..cbeeaadb 100644 --- a/package.json +++ b/package.json @@ -26,18 +26,16 @@ }, "dependencies": { "@aws-sdk/client-s3": "^3.181.0", - "@codex-team/config-loader": "0.0.1-alpha.2", + "@codex-team/config-loader": "0.1.0-rc1", "@codexteam/shortcuts": "^1.2.0", "@hawk.so/javascript": "^3.0.1", "@hawk.so/nodejs": "^3.1.4", "@types/multer-s3": "^3.0.0", "@types/yargs": "^17.0.13", "arg": "^5.0.2", - "config": "^3.3.6", "cookie-parser": "^1.4.5", "csurf": "^1.11.0", "debug": "^4.3.2", - "dotenv": "^16.0.0", "express": "^4.17.1", "file-type": "^16.5.4", "fs-extra": "^10.1.0", diff --git a/yarn.lock b/yarn.lock index 6c6e5854..38be1d17 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1809,12 +1809,11 @@ "@babel/helper-validator-identifier" "^7.19.1" to-fast-properties "^2.0.0" -"@codex-team/config-loader@0.0.1-alpha.2": - version "0.0.1-alpha.2" - resolved "https://registry.yarnpkg.com/@codex-team/config-loader/-/config-loader-0.0.1-alpha.2.tgz#1852feef1cb7bea2bd530fd55a0a1a7d8ab75cef" - integrity sha512-RNf53ttDwOUCKaMfJM/X4y7+gYtzHmmIQ0np9W2chpFxhFdNJxZB6SPF3y3Et7Xe6damOrGl5P46+W5R/5iToA== +"@codex-team/config-loader@0.1.0-rc1": + version "0.1.0-rc1" + resolved "https://registry.yarnpkg.com/@codex-team/config-loader/-/config-loader-0.1.0-rc1.tgz#f4adf2553e97933b029982622ed29ef667cded3f" + integrity sha512-dHII0e2L3QsSs77zn1KLz+PIuVCYTqSUPAPgk4UiT5MUA1lNi/6smJ5A7+QEcbBnKaHVmRtvhHGR9ahfJ5ZhIQ== dependencies: - eslint-plugin-n "^15.2.5" js-yaml "^4.1.0" lodash.isarray "^4.0.0" lodash.merge "^4.6.2" @@ -3299,12 +3298,6 @@ concurrently@^7.1.0: tree-kill "^1.2.2" yargs "^17.3.1" -config@^3.3.6: - version "3.3.7" - resolved "https://registry.yarnpkg.com/config/-/config-3.3.7.tgz#4310410dc2bf4e0effdca21a12a4035860a24ee4" - dependencies: - json5 "^2.1.1" - content-disposition@0.5.4: version "0.5.4" resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe" @@ -3718,10 +3711,6 @@ domutils@^3.0.1: domelementtype "^2.3.0" domhandler "^5.0.1" -dotenv@^16.0.0: - version "16.0.1" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.1.tgz#8f8f9d94876c35dac989876a5d3a82a267fdce1d" - ecdsa-sig-formatter@1.0.11: version "1.0.11" resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf" @@ -4969,7 +4958,7 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" -json5@^2.1.1, json5@^2.1.2, json5@^2.2.1: +json5@^2.1.2, json5@^2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" From 9f72b324fa651330d3ba5ae0199602da349ae59d Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Sat, 29 Oct 2022 00:27:29 +0300 Subject: [PATCH 02/11] implement default config && move password to auth section --- src/backend/routes/auth.ts | 6 ++-- src/backend/routes/middlewares/token.ts | 4 +-- src/backend/utils/appConfig.ts | 44 +++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/backend/routes/auth.ts b/src/backend/routes/auth.ts index 9b69c9a2..bc316f41 100644 --- a/src/backend/routes/auth.ts +++ b/src/backend/routes/auth.ts @@ -22,7 +22,7 @@ router.get('/auth', csrfProtection, function (req: Request, res: Response) { */ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Response) => { try { - if (!appConfig.password) { + if (!appConfig.auth.password) { res.render('auth', { title: 'Login page', header: 'Password not set', @@ -32,7 +32,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon return; } - if (req.body.password !== appConfig.password) { + if (req.body.password !== appConfig.auth.password) { res.render('auth', { title: 'Login page', header: 'Wrong password', @@ -46,7 +46,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon iss: 'Codex Team', sub: 'auth', iat: Date.now(), - }, appConfig.password + appConfig.auth.secret); + }, appConfig.auth.password + appConfig.auth.secret); res.cookie('authToken', token, { httpOnly: true, diff --git a/src/backend/routes/middlewares/token.ts b/src/backend/routes/middlewares/token.ts index d57169e8..a48a4751 100644 --- a/src/backend/routes/middlewares/token.ts +++ b/src/backend/routes/middlewares/token.ts @@ -14,14 +14,14 @@ export default async function verifyToken(req: Request, res: Response, next: Nex const token = req.cookies.authToken; try { - if (!appConfig.password) { + if (!appConfig.auth.password) { res.locals.isAuthorized = false; next(); return; } - const decodedToken = jwt.verify(token, appConfig.password + appConfig.auth.secret); + const decodedToken = jwt.verify(token, appConfig.auth.password + appConfig.auth.secret); res.locals.isAuthorized = !!decodedToken; diff --git a/src/backend/utils/appConfig.ts b/src/backend/utils/appConfig.ts index 7d90d9e5..d9e646b3 100644 --- a/src/backend/utils/appConfig.ts +++ b/src/backend/utils/appConfig.ts @@ -65,6 +65,7 @@ const MongoDatabaseConfig = z.object({ */ const AuthConfig = z.object({ secret: z.string(), // Secret for JWT + password: z.string(), // Password for admin panel }); /** @@ -103,7 +104,6 @@ const AppConfig = z.object({ favicon: z.string().optional(), // Path or URL to favicon uploads: z.union([LocalUploadsConfig, S3UploadsConfig]), // Uploads configuration hawk: HawkConfig.optional().nullable(), // Hawk configuration - password: z.string(), // Password for admin panel frontend: FrontendConfig, // Frontend configuration auth: AuthConfig, // Auth configuration database: z.union([LocalDatabaseConfig, MongoDatabaseConfig]), // Database configuration @@ -112,6 +112,46 @@ const AppConfig = z.object({ export type AppConfig = z.infer; +const defaultConfig: AppConfig = { + 'port': 3000, + 'host': 'localhost', + 'uploads': { + 'driver': 'local', + 'local': { + 'path': './uploads', + }, + }, + 'frontend': { + 'title': 'CodeX Docs', + 'description': 'A block-styled editor with clean JSON output', + 'startPage': '', + 'misprintsChatId': '12344564', + 'yandexMetrikaId': '', + 'carbon': { + 'serve': '', + 'placement': '', + }, + 'menu': [ + 'Guides', + { + 'title': 'CodeX', + 'uri': 'https://codex.so', + }, + ], + }, + 'auth': { + 'secret': 'supersecret', + 'password': 'secretpassword', + }, + 'hawk': null, + 'database': { + 'driver': 'local', + 'local': { + 'path': './db', + }, + }, +}; + const args = arg({ /* eslint-disable @typescript-eslint/naming-convention */ '--config': [ String ], '-c': '--config', @@ -126,7 +166,7 @@ const paths = (args['--config'] || [ './docs-config.yaml' ]).map((configPath) => return path.join(cwd, configPath); }); -const loadedConfig = loadConfig(...paths); +const loadedConfig = loadConfig(...[defaultConfig, ...paths]); const appConfig = AppConfig.parse(loadedConfig); From 1a6850427c5639537d8c1c4e85b890a208aa654d Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Sat, 29 Oct 2022 12:32:16 +0300 Subject: [PATCH 03/11] add 'v' to the start of image name --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bb7b76fc..5393e5cc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -35,8 +35,8 @@ jobs: type=ref,event=branch type=ref,event=pr type=raw,value={{branch}}-{{sha}}-{{date 'X'}},enable=${{ startsWith(github.ref, 'refs/heads') }} - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} + type=semver,pattern=v{{version}} + type=semver,pattern=v{{major}}.{{minor}} - name: Build and push image uses: docker/build-push-action@v3 From 59d804ac83fef23e99c7dc0dfaf408173df7b824 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 2 Nov 2022 23:13:40 +0200 Subject: [PATCH 04/11] fix dockerfile --- docker/Dockerfile.prod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile.prod b/docker/Dockerfile.prod index b9a25067..2a9bca7e 100644 --- a/docker/Dockerfile.prod +++ b/docker/Dockerfile.prod @@ -30,4 +30,4 @@ COPY --from=build /usr/src/app/public ./public ENV NODE_ENV=production -CMD ["node", "dist/backend/server.js"] +CMD ["node", "dist/backend/app.js"] From 3fa07b682aca83702f528f6a0dd3dd1e4a661cf4 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 2 Nov 2022 23:24:09 +0200 Subject: [PATCH 05/11] test --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5393e5cc..212d4e10 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,12 +10,14 @@ on: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} + IMAGE_NAME_TEST: fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[job.status == 'success'] jobs: build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: + - run: echo "$IMAGE_NAME_TEST" - name: Checkout repository uses: actions/checkout@v3 From 7eb2d1e183357ccede37713f6947e9d5c5f9d5e8 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 2 Nov 2022 23:28:20 +0200 Subject: [PATCH 06/11] test pipeline --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 212d4e10..dd77e1f9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ on: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} - IMAGE_NAME_TEST: fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[job.status == 'success'] + IMAGE_NAME_TEST: ${{ fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[job.status == 'success'] }} jobs: build: From a180f6c64989e06c8d8e470fb3df4afdf20d5f84 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 2 Nov 2022 23:34:15 +0200 Subject: [PATCH 07/11] test again --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dd77e1f9..1994a917 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ on: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} - IMAGE_NAME_TEST: ${{ fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[job.status == 'success'] }} + IMAGE_NAME_TEST: ${{ fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[endsWith(github.ref, '/stage')] }} jobs: build: From eea82fee8fbab5bd12aa0f78c512c43882cc1e46 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Thu, 3 Nov 2022 01:02:34 +0200 Subject: [PATCH 08/11] fixes --- .github/workflows/build.yml | 1 - src/backend/server.ts | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1994a917..41fa249f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,6 @@ on: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} - IMAGE_NAME_TEST: ${{ fromJSON('[${{github.repository}}, ${{github.repository}}-stage]')[endsWith(github.ref, '/stage')] }} jobs: build: diff --git a/src/backend/server.ts b/src/backend/server.ts index c3331f4d..2269aae5 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -34,6 +34,7 @@ function createApp(): express.Express { */ // eslint-disable-next-line @typescript-eslint/naming-convention const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const cwd = process.cwd(); const app = express(); const localConfig = appConfig.frontend; @@ -85,7 +86,10 @@ function createApp(): express.Express { app.use(express.static(path.join(__dirname, '../../public'))); if (appConfig.uploads.driver === 'local') { - app.use('/uploads', express.static(appConfig.uploads.local.path)); + const uploadsPath = path.join(cwd, appConfig.uploads.local.path); + + console.log(uploadsPath); + app.use('/uploads', express.static(uploadsPath)); } app.use('/favicon', express.static(downloadedFaviconFolder)); From c53200775557e3538712da77270a09fd723c1464 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Thu, 3 Nov 2022 01:07:50 +0200 Subject: [PATCH 09/11] remove test step --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 41fa249f..2769509d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,6 @@ jobs: runs-on: ubuntu-22.04 steps: - - run: echo "$IMAGE_NAME_TEST" - name: Checkout repository uses: actions/checkout@v3 From a3c87bb2c755a57c18983ac1922b2c0db27404d6 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Thu, 3 Nov 2022 01:08:11 +0200 Subject: [PATCH 10/11] remove console log --- src/backend/server.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/server.ts b/src/backend/server.ts index 2269aae5..ce48e493 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -88,7 +88,6 @@ function createApp(): express.Express { if (appConfig.uploads.driver === 'local') { const uploadsPath = path.join(cwd, appConfig.uploads.local.path); - console.log(uploadsPath); app.use('/uploads', express.static(uploadsPath)); } From 2c0477c97d9cc9f7d697fab3e38b60629944bef3 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Thu, 3 Nov 2022 11:46:04 +0200 Subject: [PATCH 11/11] fix default config --- src/backend/utils/appConfig.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/backend/utils/appConfig.ts b/src/backend/utils/appConfig.ts index d9e646b3..c824d49f 100644 --- a/src/backend/utils/appConfig.ts +++ b/src/backend/utils/appConfig.ts @@ -123,21 +123,13 @@ const defaultConfig: AppConfig = { }, 'frontend': { 'title': 'CodeX Docs', - 'description': 'A block-styled editor with clean JSON output', + 'description': 'Free Docs app powered by Editor.js ecosystem', 'startPage': '', - 'misprintsChatId': '12344564', - 'yandexMetrikaId': '', 'carbon': { 'serve': '', 'placement': '', }, - 'menu': [ - 'Guides', - { - 'title': 'CodeX', - 'uri': 'https://codex.so', - }, - ], + 'menu': [], }, 'auth': { 'secret': 'supersecret',