Skip to content

Commit

Permalink
feat(service/commenting): add route
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Dec 4, 2022
1 parent 705d7b9 commit 542cc52
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 11 deletions.
10 changes: 10 additions & 0 deletions packages/service/commenting/demo.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@

### home
GET {{apiUrl}}/

### Add comment
PUT {{apiUrl}}/add?path=product/test
Authorization: Bearer {{token}}
Content-Type: application/json

{
"userName": "njfamirm",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
}
1 change: 1 addition & 0 deletions packages/service/commenting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/nano-server": "^0.24.1",
"@alwatr/storage-client": "^0.24.1",
"@alwatr/storage-engine": "^0.24.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/service/commenting/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './route/home.js'
import './route/home.js';
import './route/add.js';
import {logger} from './lib/config.js';

logger.logOther('..:: Alwatr Storage Nanoservice API ::..');
15 changes: 11 additions & 4 deletions packages/service/commenting/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import {createLogger} from '@alwatr/logger';
export const logger = createLogger('storage-server');

export const config = {
host: process.env.HOST ?? '0.0.0.0',
port: process.env.PORT != null ? +process.env.PORT : 8000,
storagePath: process.env.STORAGE_PATH ?? 'db',
token: process.env.TOKEN ?? 'YOUR_SECRET_TOKEN',
nanoServer: {
host: process.env.HOST ?? '0.0.0.0',
port: process.env.PORT != null ? +process.env.PORT : 8000,
token: process.env.TOKEN ?? 'YOUR_SECRET_TOKEN',
},
storage: {
host: process.env.STORAGE_HOST ?? '127.0.0.1',
port: process.env.STORAGE_PORT != null ? +process.env.STORAGE_PORT : 9000,
name: process.env.STORAGE_NAME ?? 'comment',
token: process.env.STORAGE_TOKEN ?? 'YOUR_SECRET_TOKEN',
},
};

logger.logProperty('config', {...config, token: '***'});
6 changes: 5 additions & 1 deletion packages/service/commenting/src/lib/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import {AlwatrNanoServer} from '@alwatr/nano-server';

import {config} from './config.js';

export const nanoServer = new AlwatrNanoServer({host: config.host, port: config.port, allowAllOrigin: true});
export const nanoServer = new AlwatrNanoServer({
host: config.nanoServer.host,
port: config.nanoServer.port,
allowAllOrigin: true,
});
5 changes: 0 additions & 5 deletions packages/service/commenting/src/lib/storage-provider.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/service/commenting/src/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {AlwatrStorageClient} from '@alwatr/storage-client';

import {config} from './config.js';
import {Comment} from './type.js';

export const storage = new AlwatrStorageClient<Comment>(config.storage);
6 changes: 6 additions & 0 deletions packages/service/commenting/src/lib/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {AlwatrDocumentObject} from '@alwatr/storage-engine';

export type Comment = AlwatrDocumentObject & {
userName: string;
text: string;
}
35 changes: 35 additions & 0 deletions packages/service/commenting/src/route/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {logger} from 'src/lib/config.js';

import {nanoServer} from '../lib/nano-server.js';
import {storage} from '../lib/storage.js';
import {Comment} from '../lib/type.js';


import type {AlwatrConnection} from '@alwatr/nano-server';

nanoServer.route('PUT', '/add', addComment);

async function addComment(connection: AlwatrConnection): Promise<void> {
const params = await connection.requireQueryParams<{path: string}>({path: 'string'});
if (params == null) return;

const bodyJson = await connection.requireJsonBody<Comment>();
if (bodyJson == null) return;

storage.config.name = params.path;

try {
connection.reply({
ok: true,
data: await storage.set({...bodyJson, id: 'auto_increment'}),
});
}
catch (err) {
logger.error('newJob', (err as Error).message ?? 'storage_error', (err as Error).stack ?? err);
connection.reply({
ok: false,
statusCode: 500,
errorCode: 'storage_error',
});
}
}

0 comments on commit 542cc52

Please sign in to comment.