Skip to content

Commit 775b5c4

Browse files
committed
adds a barebone implementation of the caching server.
1 parent 23884d0 commit 775b5c4

File tree

6 files changed

+302
-4
lines changed

6 files changed

+302
-4
lines changed

package-lock.json

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

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@
1818
"supertest": "^6.3.3",
1919
"ts-jest": "^29.0.3",
2020
"typescript": "^4.9.4"
21+
},
22+
"dependencies": {
23+
"@types/redis": "^4.0.11",
24+
"redis": "^4.6.13"
2125
}
2226
}

src/server/HTTPServer.test.ts

Whitespace-only changes.

src/server/HTTPServer.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as http from 'http';
2+
import { CacheService } from '../service/CacheService';
3+
4+
export class CacheServiceHTTPServer {
5+
private readonly cacheService: CacheService;
6+
private readonly server: http.Server;
7+
8+
constructor(port: number) {
9+
this.cacheService = new CacheService();
10+
this.server = http.createServer(this.request_handler.bind(this));
11+
this.setupServer(port);
12+
}
13+
14+
private async setupServer(port: number) {
15+
await this.cacheService.connect();
16+
this.server.listen(port, () => {
17+
console.log(`Server listening on port ${port}`);
18+
});
19+
}
20+
21+
private async request_handler(request: http.IncomingMessage, response: http.ServerResponse) {
22+
if (request.method === 'POST') {
23+
await this.handleNotificationPostRequest(request, response);
24+
}
25+
else if (request.method === 'GET') {
26+
await this.handleClientGetRequest(request, response);
27+
}
28+
else if (request.method === 'DELETE') {
29+
await this.handleNotificationDeleteRequest(request, response);
30+
}
31+
else {
32+
response.writeHead(405, 'Method Not Allowed');
33+
response.end('Method Not Allowed');
34+
}
35+
}
36+
37+
private async handleNotificationPostRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
38+
39+
}
40+
41+
private async handleClientGetRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
42+
43+
}
44+
45+
private async handleNotificationDeleteRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
46+
47+
}
48+
49+
50+
}

src/service/CacheService.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { CacheService } from './CacheService';
2+
3+
describe('CacheService', () => {
4+
let cacheService: CacheService;
5+
6+
beforeEach(() => {
7+
cacheService = new CacheService();
8+
});
9+
10+
afterEach(async () => {
11+
await cacheService.disconnect();
12+
});
13+
14+
it('should_connect_to_the_cache', async () => {
15+
const connected = await cacheService.connect();
16+
expect(connected).toBe(true);
17+
});
18+
19+
it('should_set_and_get_a_value', async () => {
20+
await cacheService.connect();
21+
await cacheService.set('key', 'value');
22+
const value = await cacheService.get('key');
23+
expect(value).toBe('value');
24+
});
25+
26+
it('should_delete_a_value', async () => {
27+
await cacheService.connect();
28+
await cacheService.set('key', 'value');
29+
await cacheService.delete('key');
30+
const value = await cacheService.get('key');
31+
expect(value).toBe(null);
32+
});
33+
34+
it('should_close_the_connection', async () => {
35+
36+
});
37+
});

0 commit comments

Comments
 (0)