Skip to content

Commit a5accb9

Browse files
author
Kushagra Singh Bisen
committed
Add cache service and HTTP server implementation
1 parent 775b5c4 commit a5accb9

File tree

7 files changed

+2677
-200
lines changed

7 files changed

+2677
-200
lines changed

package-lock.json

Lines changed: 2491 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
{
2-
"name": "insert-name-of-project-here",
2+
"name": "solid-stream-notifications-cache",
33
"version": "1.0.0",
4-
"description": "insert-description-of-project-here",
4+
"description": "A cache service to store the notification stream from a solid server",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "npx tsc",
88
"start": "node dist/index.js",
99
"test": "jest --coverage",
10+
"lint:ts": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
11+
"lint:ts:fix": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0 --fix",
1012
"test:watch": "jest --watch"
1113
},
1214
"keywords": [],
1315
"author": "Kushagra Singh Bisen",
1416
"license": "ISC",
1517
"devDependencies": {
18+
"@types/bunyan": "^1.8.11",
1619
"@types/jest": "^29.2.4",
20+
"eslint": "^8.57.0",
21+
"eslint-plugin-jest": "^27.9.0",
22+
"eslint-plugin-jsdoc": "^48.2.0",
1723
"jest": "^29.3.1",
1824
"supertest": "^6.3.3",
1925
"ts-jest": "^29.0.3",
2026
"typescript": "^4.9.4"
2127
},
2228
"dependencies": {
2329
"@types/redis": "^4.0.11",
30+
"@typescript-eslint/parser": "^7.1.0",
31+
"bunyan": "^1.8.15",
2432
"redis": "^4.6.13"
2533
}
2634
}

src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { CacheServiceHTTPServer } from "./server/CacheServiceHTTPServer";
2+
import * as bunyan from "bunyan";
3+
import * as fs from 'fs';
4+
const program = require('commander');
5+
const log_file = fs.createWriteStream('./logs/info.log', { flags: 'a' });
6+
7+
const logger = bunyan.createLogger({
8+
name: 'solid-stream-notifications-cache',
9+
streams: [
10+
{
11+
level: 'info',
12+
stream: log_file
13+
}
14+
],
15+
serializers: {
16+
log: (log_data: any) => {
17+
return {
18+
...log_data,
19+
}
20+
}
21+
}
22+
})
23+
24+
program
25+
.version('1.0.0')
26+
.description('Solid Stream Notifications Cache Service')
27+
.name('solid-stream-notifications-cache')
28+
29+
program
30+
.command('cache-notifications')
31+
.description('Starts the cache service for notifications from the solid server(s).')
32+
.option('-p, --port <port>', 'The port where the HTTP server will listen.', '8085')
33+
.option('-p --pod <pod>', 'The location of the Solid Pod', 'http://localhost:3000/aggregation_pod/')
34+
.action((options: any) => {
35+
new CacheServiceHTTPServer(options.port, options.pod, logger);
36+
});

src/server/CacheServiceHTTPServer.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as http from 'http';
2+
import { CacheService } from '../service/CacheService';
3+
4+
/**
5+
* A class for the HTTP server that interacts with the cache service to handle requests.
6+
* It stores the notifications from the solid server(s) and allows clients to retrieve them.
7+
* @class CacheServiceHTTPServer
8+
*/
9+
export class CacheServiceHTTPServer {
10+
private readonly cacheService: CacheService;
11+
private readonly server: http.Server;
12+
private pod_url: string;
13+
/**
14+
* Creates an instance of CacheServiceHTTPServer.
15+
* @param {number} port - The port where the HTTP server will listen.
16+
* @param {string} pod_url - The location of the Solid Pod from which the notifications are retrieved.
17+
* @param {*} logger - The logger object.
18+
* @memberof CacheServiceHTTPServer
19+
*/
20+
constructor(port: number, pod_url: string, logger: any) {
21+
this.cacheService = new CacheService();
22+
this.pod_url = pod_url;
23+
this.server = http.createServer(this.request_handler.bind(this));
24+
this.setupServer(port);
25+
}
26+
/**
27+
* Sets up the HTTP server where it listens on the specified port as well as connects to the cache service.
28+
* @private
29+
* @param {number} port - The port where the HTTP server will listen.
30+
* @memberof CacheServiceHTTPServer
31+
*/
32+
private async setupServer(port: number) {
33+
await this.cacheService.connect();
34+
this.server.listen(port, () => {
35+
console.log(`Server listening on port ${port}`);
36+
});
37+
}
38+
/**
39+
* Handles the requests to the HTTP server.
40+
* @private
41+
* @param {http.IncomingMessage} request - The request object.
42+
* @param {http.ServerResponse} response - The response object.
43+
* @returns {Promise<void>} - A promise which responses nothing.
44+
* @memberof CacheServiceHTTPServer
45+
*/
46+
private async request_handler(request: http.IncomingMessage, response: http.ServerResponse) {
47+
if (request.method === 'POST') {
48+
await this.handleNotificationPostRequest(request, response);
49+
}
50+
else if (request.method === 'GET') {
51+
await this.handleClientGetRequest(request, response);
52+
}
53+
else if (request.method === 'DELETE') {
54+
await this.handleNotificationDeleteRequest(request, response);
55+
}
56+
else {
57+
response.writeHead(405, 'Method Not Allowed');
58+
response.end('Method Not Allowed');
59+
}
60+
}
61+
/**
62+
* Handles the POST requests to the HTTP server, which are notifications from the solid server(s).
63+
* @private
64+
* @param {http.IncomingMessage} request - The request object.
65+
* @param {http.ServerResponse} response - The response object.
66+
* @returns {Promise<void>}
67+
* @memberof CacheServiceHTTPServer
68+
*/
69+
private async handleNotificationPostRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
70+
71+
}
72+
/**
73+
* Handles the GET requests to the HTTP server, which are requests from the clients to retrieve the notifications.
74+
* @private
75+
* @param {http.IncomingMessage} request - The request object.
76+
* @param {http.ServerResponse} response - The response object.
77+
* @returns {Promise<void>}
78+
* @memberof CacheServiceHTTPServer
79+
*/
80+
private async handleClientGetRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
81+
82+
}
83+
/**
84+
* Handles the DELETE requests to the HTTP server.
85+
* @private
86+
* @param {http.IncomingMessage} request - The request object.
87+
* @param {http.ServerResponse} response - The response object.
88+
* @returns {Promise<void>} - A promise which responses nothing.
89+
* @memberof CacheServiceHTTPServer
90+
*/
91+
private async handleNotificationDeleteRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
92+
93+
}
94+
95+
96+
}

src/server/HTTPServer.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/service/CacheService.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import { createClient } from "redis";
2-
2+
/**
3+
* A service for interacting with the Redis cache.
4+
* @class CacheService
5+
*/
36
export class CacheService {
47
private client: any;
8+
/**
9+
* Creates an instance of CacheService.
10+
* @memberof CacheService
11+
*/
512
constructor() {
613
this.client = createClient({});
714
this.setupListeners();
815
}
9-
16+
/**
17+
* Connects to the Redis cache.
18+
* @returns {Promise<boolean>} - A promise that resolves to true if the connection is successful.
19+
* @memberof CacheService
20+
*/
1021
async connect() {
1122
await this.client.connect();
1223
return true;
1324

1425
}
1526

27+
/**
28+
* Sets up the listeners for the Redis client.
29+
* @private
30+
* @memberof CacheService
31+
*/
1632
private setupListeners() {
1733
this.client.on("connect", () => {
1834
return true;
@@ -24,19 +40,41 @@ export class CacheService {
2440
});
2541
}
2642

43+
/**
44+
* Sets a value in the Redis cache.
45+
* @param {string} key - The key to set.
46+
* @param {any} value - The value to set.
47+
* @returns {Promise<void>} - A promise that resolves when the value is set.
48+
* @memberof CacheService
49+
*/
2750
async set(key: any, value: any) {
2851
await this.client.set(key, value);
2952
}
30-
31-
async get(key: any) {
53+
/**
54+
* Gets a value from the Redis cache.
55+
* @param {string} key - The key to get.
56+
* @returns {Promise<any>} - The value of the key in the cache returned as a promise.
57+
* @memberof CacheService
58+
*/
59+
async get(key: string) {
3260
return await this.client.get(key);
3361
}
34-
62+
/**
63+
* Disconnects from the Redis cache.
64+
* @returns {Promise<void>}
65+
* @memberof CacheService
66+
*/
3567
async disconnect() {
3668
await this.client.quit();
3769
}
3870

39-
async delete(key: any) {
71+
/**
72+
* Deletes a key from the Redis cache.
73+
* @param {string} key - The key to delete.
74+
* @returns {Promise<void>}
75+
* @memberof CacheService
76+
*/
77+
async delete(key: string) {
4078
await this.client.del(key);
4179
}
4280
}

0 commit comments

Comments
 (0)