From 72f0405bb3a33efef3827dd667384b92a4e828d4 Mon Sep 17 00:00:00 2001 From: Glowstudent777 <55334764+Glowstudent777@users.noreply.github.com> Date: Sun, 5 Jan 2025 16:12:46 -0600 Subject: [PATCH 1/4] Initial Changes --- babel.config.js | 11 --- gulpfile.js | 8 -- package.json | 19 ++--- pnpm-lock.yaml | 172 +++++++++++++++++++++++++++++++++++++++ src/api/v1/votd/index.ts | 24 ++++-- src/index.ts | 15 ++-- src/redis.ts | 32 ++++++++ tsconfig.json | 4 +- 8 files changed, 242 insertions(+), 43 deletions(-) delete mode 100644 babel.config.js delete mode 100644 gulpfile.js create mode 100644 src/redis.ts diff --git a/babel.config.js b/babel.config.js deleted file mode 100644 index 7234043..0000000 --- a/babel.config.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = function (api) { - api.cache(true); - - const presets = ["@babel/preset-env", "@babel/preset-typescript"]; - const ignore = ["**/__tests__", "**/*.test.ts", "!src/db/**"]; - - return { - presets, - ignore - }; -} \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 0547cb4..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,8 +0,0 @@ -const gulp = require('gulp'); -const babel = require('gulp-babel'); - -gulp.task('default', () => - gulp.src('src') - .pipe(babel()) - .pipe(gulp.dest('dist')) -); \ No newline at end of file diff --git a/package.json b/package.json index 9757e0a..25cb169 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,11 @@ "types": "./dist/index.d.ts", "main": "./dist/index.js", "scripts": { - "build": "npm run babel && npx tsc", + "build": "npm run clean && npx tsc && npm run copyfiles", "clean": "npx rimraf --glob dist/", "copyfiles": "cp ./src/api/v1/core/db/ ./dist/api/v1/core/db/ -r", - "babel": "npm run clean && npx babel src -d dist --extensions .ts --no-copy-ignored && npm run copyfiles", - "start": "npm run build && node ./dist/index.js", - "dev": "tsc-watch --onSuccess \"node ./dist/index.js\"", + "dev": "npm run copyfiles && tsc-watch --onSuccess \"node ./dist/index.js\"", + "start": "node ./dist/index.js", "test": "vitest", "test:coverage": "vitest run --coverage" }, @@ -33,24 +32,22 @@ "dotenv-extended": "^2.9.0", "express": "^4.18.2", "fs": "0.0.1-security", + "ioredis": "^5.4.2", "path": "^0.12.7", + "redis": "^4.7.0", "swagger-jsdoc": "^6.2.8", "swagger-ui-express": "^5.0.1" }, "devDependencies": { - "@babel/cli": "^7.26.4", - "@babel/core": "^7.26.0", - "@babel/preset-env": "^7.26.0", - "@babel/preset-typescript": "^7.26.0", "@types/express": "^4.17.17", + "@types/ioredis": "^5.0.0", "@types/node": "^18.13.0", + "@types/redis": "^4.0.11", "@types/swagger-jsdoc": "^6.0.4", "@types/swagger-ui-express": "^4.1.6", - "gulp": "^5.0.0", - "gulp-babel": "^8.0.0", "rimraf": "^6.0.1", "tsc-watch": "^6.0.0", "typescript": "^4.9.5", "vitest": "^1.6.0" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bbdfd52..2fed21d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,9 +29,15 @@ importers: fs: specifier: 0.0.1-security version: 0.0.1-security + ioredis: + specifier: ^5.4.2 + version: 5.4.2 path: specifier: ^0.12.7 version: 0.12.7 + redis: + specifier: ^4.7.0 + version: 4.7.0 swagger-jsdoc: specifier: ^6.2.8 version: 6.2.8(openapi-types@12.1.3) @@ -54,9 +60,15 @@ importers: '@types/express': specifier: ^4.17.17 version: 4.17.17 + '@types/ioredis': + specifier: ^5.0.0 + version: 5.0.0 '@types/node': specifier: ^18.13.0 version: 18.14.6 + '@types/redis': + specifier: ^4.0.11 + version: 4.0.11 '@types/swagger-jsdoc': specifier: ^6.0.4 version: 6.0.4 @@ -790,6 +802,9 @@ packages: resolution: {integrity: sha512-kjotm7XJrJ6v+7knhPaRgaT6q8F8K2jiafwYdNHLzmV0uGLuZY43FK6smNSHUPrhq5kX2slCUy+RGG/xGqmIKA==} engines: {node: '>=10.13.0'} + '@ioredis/commands@1.2.0': + resolution: {integrity: sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg==} + '@isaacs/cliui@8.0.2': resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} @@ -836,6 +851,35 @@ packages: '@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3': resolution: {integrity: sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ==} + '@redis/bloom@1.2.0': + resolution: {integrity: sha512-HG2DFjYKbpNmVXsa0keLHp/3leGJz1mjh09f2RLGGLQZzSHpkmZWuwJbAvo3QcRY8p80m5+ZdXZdYOSBLlp7Cg==} + peerDependencies: + '@redis/client': ^1.0.0 + + '@redis/client@1.6.0': + resolution: {integrity: sha512-aR0uffYI700OEEH4gYnitAnv3vzVGXCFvYfdpu/CJKvk4pHfLPEy/JSZyrpQ+15WhXe1yJRXLtfQ84s4mEXnPg==} + engines: {node: '>=14'} + + '@redis/graph@1.1.1': + resolution: {integrity: sha512-FEMTcTHZozZciLRl6GiiIB4zGm5z5F3F6a6FZCyrfxdKOhFlGkiAqlexWMBzCi4DcRoyiOsuLfW+cjlGWyExOw==} + peerDependencies: + '@redis/client': ^1.0.0 + + '@redis/json@1.0.7': + resolution: {integrity: sha512-6UyXfjVaTBTJtKNG4/9Z8PSpKE6XgSyEb8iwaqDcy+uKrd/DGYHTWkUdnQDyzm727V7p21WUMhsqz5oy65kPcQ==} + peerDependencies: + '@redis/client': ^1.0.0 + + '@redis/search@1.2.0': + resolution: {integrity: sha512-tYoDBbtqOVigEDMAcTGsRlMycIIjwMCgD8eR2t0NANeQmgK/lvxNAvYyb6bZDD4frHRhIHkJu2TBRvB0ERkOmw==} + peerDependencies: + '@redis/client': ^1.0.0 + + '@redis/time-series@1.1.0': + resolution: {integrity: sha512-c1Q99M5ljsIuc4YdaCwfUEXsofakb9c8+Zse2qxTadu8TalLXuAESzLvFAvNVbkmSlvlzIQOLpBCmWI9wTOt+g==} + peerDependencies: + '@redis/client': ^1.0.0 + '@rollup/rollup-android-arm-eabi@4.18.0': resolution: {integrity: sha512-Tya6xypR10giZV1XzxmH5wr25VcZSncG0pZIjfePT0OVBvqNEurzValetGNarVrGiq66EBVAFn15iYX4w6FKgQ==} cpu: [arm] @@ -934,6 +978,10 @@ packages: '@types/express@4.17.17': resolution: {integrity: sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==} + '@types/ioredis@5.0.0': + resolution: {integrity: sha512-zJbJ3FVE17CNl5KXzdeSPtdltc4tMT3TzC6fxQS0sQngkbFZ6h+0uTafsRqu+eSLIugf6Yb0Ea0SUuRr42Nk9g==} + deprecated: This is a stub types definition. ioredis provides its own type definitions, so you do not need this installed. + '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} @@ -949,6 +997,10 @@ packages: '@types/range-parser@1.2.4': resolution: {integrity: sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==} + '@types/redis@4.0.11': + resolution: {integrity: sha512-bI+gth8La8Wg/QCR1+V1fhrL9+LZUSWfcqpOj2Kc80ZQ4ffbdL173vQd5wovmoV9i071FU9oP2g6etLuEwb6Rg==} + deprecated: This is a stub types definition. redis provides its own type definitions, so you do not need this installed. + '@types/serve-static@1.15.1': resolution: {integrity: sha512-NUo5XNiAdULrJENtJXZZ3fHtfMolzZwczzBbnAeBbqBwG+LaG6YaJtuwzwGSQZ2wsCrxjEhNNjAkKigy3n8teQ==} @@ -1192,6 +1244,10 @@ packages: resolution: {integrity: sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==} engines: {node: '>=0.8'} + cluster-key-slot@1.1.2: + resolution: {integrity: sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==} + engines: {node: '>=0.10.0'} + color-convert@2.0.1: resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==} engines: {node: '>=7.0.0'} @@ -1281,6 +1337,10 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} + denque@2.1.0: + resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} + engines: {node: '>=0.10'} + depd@2.0.0: resolution: {integrity: sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==} engines: {node: '>= 0.8'} @@ -1507,6 +1567,10 @@ packages: function.name@1.0.13: resolution: {integrity: sha512-mVrqdoy5npWZyoXl4DxCeuVF6delDcQjVS9aPdvLYlBxtMTZDR2B5GVEQEoM1jJyspCqg3C0v4ABkLE7tp9xFA==} + generic-pool@3.9.0: + resolution: {integrity: sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==} + engines: {node: '>= 4'} + gensync@1.0.0-beta.2: resolution: {integrity: sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==} engines: {node: '>=6.9.0'} @@ -1648,6 +1712,10 @@ packages: resolution: {integrity: sha512-6xwYfHbajpoF0xLW+iwLkhwgvLoZDfjYfoFNu8ftMoXINzwuymNLd9u/KmwtdT2GbR+/Cz66otEGEVVUHX9QLQ==} engines: {node: '>=10.13.0'} + ioredis@5.4.2: + resolution: {integrity: sha512-0SZXGNGZ+WzISQ67QDyZ2x0+wVxjjUndtD8oSeik/4ajifeiRufed8fCb8QW8VMyi4MXcS+UO1k/0NGhvq1PAg==} + engines: {node: '>=12.22.0'} + ipaddr.js@1.9.1: resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==} engines: {node: '>= 0.10'} @@ -1777,9 +1845,15 @@ packages: lodash.debounce@4.0.8: resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==} + lodash.defaults@4.2.0: + resolution: {integrity: sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==} + lodash.get@4.4.2: resolution: {integrity: sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==} + lodash.isarguments@3.1.0: + resolution: {integrity: sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==} + lodash.isequal@4.5.0: resolution: {integrity: sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==} @@ -2082,6 +2156,17 @@ packages: resolution: {integrity: sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ==} engines: {node: '>= 10.13.0'} + redis-errors@1.2.0: + resolution: {integrity: sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==} + engines: {node: '>=4'} + + redis-parser@3.0.0: + resolution: {integrity: sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==} + engines: {node: '>=4'} + + redis@4.7.0: + resolution: {integrity: sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==} + regenerate-unicode-properties@10.1.0: resolution: {integrity: sha512-d1VudCLoIGitcU/hEg2QqvyGZQmdC0Lf8BqdOMXGFSvJP4bNV1+XqbPQeHHLD51Jh4QJJ225dlIFvY4Ly6MXmQ==} engines: {node: '>=4'} @@ -2239,6 +2324,9 @@ packages: stackback@0.0.2: resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==} + standard-as-callback@2.1.0: + resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} + statuses@2.0.1: resolution: {integrity: sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==} engines: {node: '>= 0.8'} @@ -2562,6 +2650,9 @@ packages: yallist@3.1.1: resolution: {integrity: sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==} + yallist@4.0.0: + resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} + yaml@2.0.0-1: resolution: {integrity: sha512-W7h5dEhywMKenDJh2iX/LABkbFnBxasD27oyXWDS/feDsxiw0dD5ncXdYXgkvAsXIY2MpW/ZKkr9IU30DBdMNQ==} engines: {node: '>= 6'} @@ -3393,6 +3484,8 @@ snapshots: dependencies: is-negated-glob: 1.0.0 + '@ioredis/commands@1.2.0': {} + '@isaacs/cliui@8.0.2': dependencies: string-width: 5.1.2 @@ -3442,6 +3535,32 @@ snapshots: '@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3': optional: true + '@redis/bloom@1.2.0(@redis/client@1.6.0)': + dependencies: + '@redis/client': 1.6.0 + + '@redis/client@1.6.0': + dependencies: + cluster-key-slot: 1.1.2 + generic-pool: 3.9.0 + yallist: 4.0.0 + + '@redis/graph@1.1.1(@redis/client@1.6.0)': + dependencies: + '@redis/client': 1.6.0 + + '@redis/json@1.0.7(@redis/client@1.6.0)': + dependencies: + '@redis/client': 1.6.0 + + '@redis/search@1.2.0(@redis/client@1.6.0)': + dependencies: + '@redis/client': 1.6.0 + + '@redis/time-series@1.1.0(@redis/client@1.6.0)': + dependencies: + '@redis/client': 1.6.0 + '@rollup/rollup-android-arm-eabi@4.18.0': optional: true @@ -3516,6 +3635,12 @@ snapshots: '@types/qs': 6.9.7 '@types/serve-static': 1.15.1 + '@types/ioredis@5.0.0': + dependencies: + ioredis: 5.4.2 + transitivePeerDependencies: + - supports-color + '@types/json-schema@7.0.15': {} '@types/mime@3.0.1': {} @@ -3526,6 +3651,10 @@ snapshots: '@types/range-parser@1.2.4': {} + '@types/redis@4.0.11': + dependencies: + redis: 4.7.0 + '@types/serve-static@1.15.1': dependencies: '@types/mime': 3.0.1 @@ -3839,6 +3968,8 @@ snapshots: clone@2.1.2: {} + cluster-key-slot@1.1.2: {} + color-convert@2.0.1: dependencies: color-name: 1.1.4 @@ -3911,6 +4042,8 @@ snapshots: delayed-stream@1.0.0: {} + denque@2.1.0: {} + depd@2.0.0: {} destroy@1.2.0: {} @@ -4181,6 +4314,8 @@ snapshots: dependencies: noop6: 1.0.9 + generic-pool@3.9.0: {} + gensync@1.0.0-beta.2: {} get-caller-file@2.0.5: {} @@ -4354,6 +4489,20 @@ snapshots: interpret@3.1.1: {} + ioredis@5.4.2: + dependencies: + '@ioredis/commands': 1.2.0 + cluster-key-slot: 1.1.2 + debug: 4.3.4 + denque: 2.1.0 + lodash.defaults: 4.2.0 + lodash.isarguments: 3.1.0 + redis-errors: 1.2.0 + redis-parser: 3.0.0 + standard-as-callback: 2.1.0 + transitivePeerDependencies: + - supports-color + ipaddr.js@1.9.1: {} is-absolute@1.0.0: @@ -4452,8 +4601,12 @@ snapshots: lodash.debounce@4.0.8: {} + lodash.defaults@4.2.0: {} + lodash.get@4.4.2: {} + lodash.isarguments@3.1.0: {} + lodash.isequal@4.5.0: {} lodash.mergewith@4.6.2: {} @@ -4735,6 +4888,21 @@ snapshots: dependencies: resolve: 1.22.1 + redis-errors@1.2.0: {} + + redis-parser@3.0.0: + dependencies: + redis-errors: 1.2.0 + + redis@4.7.0: + dependencies: + '@redis/bloom': 1.2.0(@redis/client@1.6.0) + '@redis/client': 1.6.0 + '@redis/graph': 1.1.1(@redis/client@1.6.0) + '@redis/json': 1.0.7(@redis/client@1.6.0) + '@redis/search': 1.2.0(@redis/client@1.6.0) + '@redis/time-series': 1.1.0(@redis/client@1.6.0) + regenerate-unicode-properties@10.1.0: dependencies: regenerate: 1.4.2 @@ -4909,6 +5077,8 @@ snapshots: stackback@0.0.2: {} + standard-as-callback@2.1.0: {} + statuses@2.0.1: {} std-env@3.7.0: {} @@ -5238,6 +5408,8 @@ snapshots: yallist@3.1.1: {} + yallist@4.0.0: {} + yaml@2.0.0-1: {} yargs-parser@20.2.9: {} diff --git a/src/api/v1/votd/index.ts b/src/api/v1/votd/index.ts index f5ec057..3f67066 100644 --- a/src/api/v1/votd/index.ts +++ b/src/api/v1/votd/index.ts @@ -1,7 +1,6 @@ import express, { Request, Response, Router } from "express"; -import axios from "axios"; -import * as cheerio from "cheerio"; import { getVotd } from "../core/functions/votd"; +import { getVotdExpireTime, redis } from "../../../redis"; // Router const router: Router = express.Router(); @@ -32,9 +31,24 @@ const router: Router = express.Router(); * example: OK */ router.get("/", async (req: Request, res: Response) => { - const lang = (req.query.lang as string) || "en"; - const data = await getVotd(lang); - res.status(200).send(data); + try { + redis.get("votd", async (err, data) => { + if (data) { + console.log("Verse of the day fetched from Redis"); + res.status(200).send(JSON.parse(data)); + } else { + const lang = (req.query.lang as string) || "en"; + const data = await getVotd(lang); + + redis.set("votd", JSON.stringify(data), "EX", getVotdExpireTime()); + + console.log("Verse of the day fetched from API"); + res.status(200).send(data); + } + }); + } catch (err: Error | any) { + res.status(500).send("Error getting verse of the day: " + err.message); + } }); module.exports = router; diff --git a/src/index.ts b/src/index.ts index 1580c52..2401cc8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,23 @@ -import express, {Express} from 'express' -import dotEnvExtended from 'dotenv-extended' -import api from './api/index' -import { addSwagger } from './swagger' +import express, { Express } from "express"; +import dotEnvExtended from "dotenv-extended"; +import api from "./api/index"; +import { addSwagger } from "./swagger"; +import { clearVotdCache } from "./redis"; -dotEnvExtended.load() +dotEnvExtended.load(); const app: Express = express(); const port = process.env.PORT ?? 3000; +clearVotdCache(); + app.use(express.json()); app.use("/api", api); addSwagger(app); app.listen(port, () => { - console.log(`⚡️[Server]: Server is running at http://localhost:${port}`); + console.log(`⚡️[Server]: Server is running at http://localhost:${port}`); }); export default app; diff --git a/src/redis.ts b/src/redis.ts new file mode 100644 index 0000000..a9cab7b --- /dev/null +++ b/src/redis.ts @@ -0,0 +1,32 @@ +import Redis from "ioredis"; + +export const redis = new Redis( + "redis://default:SecurePassword@10.77.1.114:6379" +); + +export function connectRedis() { + redis + .connect() + .then(() => { + console.log("Connected to Redis"); + }) + .catch((err) => { + console.error("Error connecting to Redis", err); + }); +} + +export function getVotdExpireTime(): number { + const tomorrow = new Date(); + tomorrow.setDate(tomorrow.getDate() + 1); + tomorrow.setHours(6, 0, 0, 0); + + const now = new Date(); + const diff = tomorrow.getTime() - now.getTime(); + const seconds = Math.floor(diff / 1000); + + return seconds; +} + +export function clearVotdCache() { + redis.del("votd"); +} diff --git a/tsconfig.json b/tsconfig.json index 0c81638..f6cd2e9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "declaration": true, "declarationMap": true, "esModuleInterop": true, - "emitDeclarationOnly": true, + "emitDeclarationOnly": false, "target": "ES2021", "module": "CommonJS", "outDir": "./dist", @@ -26,4 +26,4 @@ "fallbackPolling": "dynamicPriority", "synchronousWatchDirectory": true } -} \ No newline at end of file +} From f7f13c585ec65082d16e5185827f30a5873499de Mon Sep 17 00:00:00 2001 From: Glowstudent777 <55334764+Glowstudent777@users.noreply.github.com> Date: Sun, 5 Jan 2025 16:18:59 -0600 Subject: [PATCH 2/4] docker compose? --- docker-compose.yml | 16 ++++++++++++++++ docker-redis.yml | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 docker-compose.yml create mode 100644 docker-redis.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b5a3520 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +services: + api: + build: . + ports: + - "3000:3000" + + redis: + image: redis/redis-stack-server:latest + networks: + - dokploy-network + expose: + - "6379" + +networks: + dokploy-network: + external: true diff --git a/docker-redis.yml b/docker-redis.yml new file mode 100644 index 0000000..c441a7e --- /dev/null +++ b/docker-redis.yml @@ -0,0 +1,5 @@ +services: + redis: + image: redis/redis-stack-server:latest + ports: + - "6379:6379" From 3a6909b9880f672b2e847563027947eb1a6ccb69 Mon Sep 17 00:00:00 2001 From: Glowstudent <55334764+Glowstudent777@users.noreply.github.com> Date: Sun, 5 Jan 2025 16:21:42 -0600 Subject: [PATCH 3/4] Update .env.schema --- .env.schema | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.schema b/.env.schema index 8f55cbd..c1cbfe6 100644 --- a/.env.schema +++ b/.env.schema @@ -1 +1,2 @@ -PORT= \ No newline at end of file +API_PORT= +REDIS_URL= From b8bd2091ed51628957e44bf7e421d21b8741d3bb Mon Sep 17 00:00:00 2001 From: Glowstudent777 <55334764+Glowstudent777@users.noreply.github.com> Date: Sun, 5 Jan 2025 16:24:38 -0600 Subject: [PATCH 4/4] Use .ENV --- .env.schema | 3 ++- src/redis.ts | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.env.schema b/.env.schema index 8f55cbd..c1cbfe6 100644 --- a/.env.schema +++ b/.env.schema @@ -1 +1,2 @@ -PORT= \ No newline at end of file +API_PORT= +REDIS_URL= diff --git a/src/redis.ts b/src/redis.ts index a9cab7b..1f15cdd 100644 --- a/src/redis.ts +++ b/src/redis.ts @@ -1,7 +1,12 @@ import Redis from "ioredis"; export const redis = new Redis( - "redis://default:SecurePassword@10.77.1.114:6379" + process.env.REDIS_URL ?? "redis://default:SecurePassword@localhost:6379", + { + tls: { + rejectUnauthorized: false, + }, + } ); export function connectRedis() {