Skip to content

Commit

Permalink
Merge pull request #459 from AliMD/feat/commenting-service
Browse files Browse the repository at this point in the history
feat(service/commenting): new microservice
  • Loading branch information
alimd authored Dec 6, 2022
2 parents d793b26 + 20c4c4b commit 591444a
Show file tree
Hide file tree
Showing 31 changed files with 458 additions and 35 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/publish-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ jobs:
short: 0
full: 0.24.1

- image: comment
path: service/comment
description: Distributed simple and fast comment nanoservice base on @alwatr/storage-server
version:
short: 0
full: 0.24.1

# - image: tdlib
# path: container/tdlib
# description: Cross-platform library for building Telegram clients.
Expand Down
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"MohammadMahdi Zamanian <[email protected]>"
],
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"tslib": "~2.4.1"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
"@typescript-eslint/parser": "~5.45.0",
"@typescript/lib-dom": "npm:@types/web",
"@web/dev-server": "~0.1.35",
"@web/dev-server-esbuild": "^0.3.3",
"@web/dev-server-esbuild": "~0.3.3",
"eslint": "~8.29.0",
"eslint-config-google": "~0.14.0",
"eslint-import-resolver-typescript": "~3.5.2",
"eslint-plugin-import": "~2.26.0",
"eslint-plugin-lit": "^1.6.1",
"eslint-plugin-lit": "~1.6.1",
"eslint-plugin-wc": "~1.4.0",
"lerna": "~6.1.0",
"lit-analyzer": "^1.2.1",
"lit-analyzer": "~1.2.1",
"npm-run-all": "~4.1.5",
"prettier": "~2.8.0",
"rimraf": "~3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"lit": "~2.4.1",
"tslib": "~2.4.1"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"tslib": "~2.4.1"
}
}
6 changes: 3 additions & 3 deletions packages/core/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/fetch": "^0.24.1",
"@alwatr/logger": "^0.24.1",
"@alwatr/signal": "^0.24.1",
"@alwatr/fetch": "~0.24.1",
"@alwatr/logger": "~0.24.1",
"@alwatr/signal": "~0.24.1",
"tslib": "~2.4.1"
}
}
6 changes: 3 additions & 3 deletions packages/core/nano-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/fetch": "^0.24.1",
"@alwatr/logger": "^0.24.1",
"@alwatr/math": "^0.24.1",
"@alwatr/fetch": "~0.24.1",
"@alwatr/logger": "~0.24.1",
"@alwatr/math": "~0.24.1",
"tslib": "~2.4.1"
}
}
4 changes: 2 additions & 2 deletions packages/core/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/signal": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"@alwatr/signal": "~0.24.1",
"tslib": "~2.4.1"
}
}
2 changes: 1 addition & 1 deletion packages/core/signal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"tslib": "~2.4.1"
}
}
6 changes: 3 additions & 3 deletions packages/core/storage-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/fetch": "^0.24.1",
"@alwatr/logger": "^0.24.1",
"@alwatr/storage-engine": "^0.24.1",
"@alwatr/fetch": "~0.24.1",
"@alwatr/logger": "~0.24.1",
"@alwatr/storage-engine": "~0.24.1",
"tslib": "~2.4.1"
}
}
4 changes: 2 additions & 2 deletions packages/core/storage-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/fetch": "^0.24.1",
"@alwatr/logger": "^0.24.1",
"@alwatr/fetch": "~0.24.1",
"@alwatr/logger": "~0.24.1",
"exit-hook": "~3.1.2",
"tslib": "~2.4.1"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/math": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"@alwatr/math": "~0.24.1",
"tslib": "~2.4.1"
}
}
2 changes: 1 addition & 1 deletion packages/lab/zx-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"build": "chmod -v +x *.js"
},
"dependencies": {
"@alwatr/logger": "^0.24.1",
"@alwatr/logger": "~0.24.1",
"tslib": "~2.4.1",
"zx": "~7.1.1"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/service/comment/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!src/
!package.json
!tsconfig.json
!*.lock
38 changes: 38 additions & 0 deletions packages/service/comment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ARG NODE_VERSION=19
FROM docker.io/library/node:${NODE_VERSION}-alpine as builder

WORKDIR /app

COPY package.json *.lock ./
RUN if [ -f *.lock ]; then \
yarn install --frozen-lockfile --non-interactive; \
else \
yarn install --non-interactive; \
fi;

COPY . .
RUN yarn build && \
rm -rf node_modules && \
yarn install --frozen-lockfile --non-interactive --production && \
ls -lahF && \
ls -lAh dist

# ---

FROM docker.io/library/node:${NODE_VERSION}-alpine as app

WORKDIR /app

RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "index.js"]

ENV NODE_ENV production
ENV ALWATR_DEBUG *
ENV HOST 0.0.0.0
ENV PORT 80
EXPOSE 80

COPY --from=builder /app/node_modules/ ./node_modules/
COPY --from=builder /app/dist/ ./
RUN ls -lahF
7 changes: 7 additions & 0 deletions packages/service/comment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Comment Service

Distributed simple and fast comment nanoservice base on [@alwatr/storage-server](../storage-server/).

## How to use

Check [demo.http](demo.http).
36 changes: 36 additions & 0 deletions packages/service/comment/demo.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@apiUrl = http://127.0.0.1:8000
@apiVersion = v0
@token = YOUR_SECRET_TOKEN

### home
GET {{apiUrl}}/

### Add new comment
PATCH {{apiUrl}}/?storage=product/test
Authorization: Bearer {{token}}
Content-Type: application/json

{
"id": "auto_increment",
"user": "njfamirm",
"type": "text",
"text": "سلام ;)"
}

### Add new reply
PATCH {{apiUrl}}/?storage=product/test
Authorization: Bearer {{token}}
Content-Type: application/json

{
"id": "auto_increment",
"user": "alimd",
"type": "text",
"text": "علیک سلام :)",
"replyId": "1"
}

### Get all comment
GET {{apiUrl}}/all?storage=product/test
Authorization: Bearer {{token}}
Content-Type: application/json
79 changes: 79 additions & 0 deletions packages/service/comment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"name": "@alwatr/comment",
"version": "0.24.1",
"description": "Alwatr Comment API Microservice, Distributed simple and fast comment nanoservice base on @alwatr/storage-server",
"type": "module",
"keywords": [
"api",
"comment",
"chat",
"microservice",
"nanoservice",
"typescript",
"alwatr"
],
"author": "S. Ali Mihandoost <[email protected]> (https://ali.mihandoost.com)",
"contributors": [
"S. Amir Mohammad Najafi <[email protected]> (https://njfamirm.ir)"
],
"license": "MIT",
"private": true,
"engines": {
"node": ">=19.0.0",
"npm": ">=8.0.0",
"yarn": ">=1.22.0"
},
"repository": {
"type": "git",
"url": "https://github.com/AliMD/alwatr",
"directory": "packages/service/comment"
},
"homepage": "https://github.com/AliMD/alwatr/tree/main/packages/service/comment#readme",
"bugs": {
"url": "https://github.com/AliMD/alwatr/issues"
},
"scripts": {
"l": "yarn lint",
"b": "yarn build",
"cb": "run-s clean build",
"s": "run-s clean build serve",
"w": "yarn watch",
"f": "yarn format",
"fl": "yarn format:eslint",
"fp": "yarn format:prettier",
"start": "yarn serve",
"lint": "run-s lint:*",
"lint:ts": "eslint . --config .eslintrc.json --ext .ts",
"build": "run-s build:*",
"build:ts": "tsc --build",
"format": "run-s format:prettier format:eslint",
"format:eslint": "yarn lint:ts --fix",
"format:prettier": "prettier \"**/*.{html,json,md,ts}\" --write",
"clean": "rimraf dist/",
"serve": "node dist/index.js",
"serve:debug": "node --inspect dist/index.js",
"watch": "run-s clean build && run-p watch:ts watch:node",
"watch:node": "nodemon -w dist/ dist/index.js",
"watch:debug-node": "nodemon -w dist/ --inspect dist/index.js",
"watch:ts": "yarn build:ts --watch --preserveWatchOutput"
},
"dependencies": {
"@alwatr/logger": "~0.24.1",
"@alwatr/nano-server": "~0.24.1",
"@alwatr/storage-client": "~0.24.1"
},
"devDependencies": {
"@types/node": "~18.11.10",
"@typescript-eslint/eslint-plugin": "~5.45.0",
"@typescript-eslint/parser": "~5.45.0",
"eslint": "~8.28.0",
"eslint-config-google": "~0.14.0",
"eslint-import-resolver-typescript": "~3.5.2",
"eslint-plugin-import": "~2.26.0",
"nodemon": "~2.0.20",
"npm-run-all": "~4.1.5",
"prettier": "~2.8.0",
"rimraf": "~3.0.2",
"typescript": "~4.9.3"
}
}
6 changes: 6 additions & 0 deletions packages/service/comment/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './route/home.js';
import './route/patch.js';
import './route/all.js';
import {logger} from './lib/config.js';

logger.logOther('..:: Alwatr Comment Nanoservice API ::..');
19 changes: 19 additions & 0 deletions packages/service/comment/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {createLogger} from '@alwatr/logger';

export const logger = createLogger('comment');

export const config = {
nanoServer: {
host: process.env.HOST ?? '0.0.0.0',
port: process.env.PORT != null ? +process.env.PORT : 8000,
allowAllOrigin: true,
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,
token: process.env.STORAGE_TOKEN ?? 'YOUR_SECRET_TOKEN',
},
};

logger.logProperty('config', {...config, token: '***'});
5 changes: 5 additions & 0 deletions packages/service/comment/src/lib/nano-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {AlwatrNanoServer} from '@alwatr/nano-server';

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

export const nanoServer = new AlwatrNanoServer(config.nanoServer);
6 changes: 6 additions & 0 deletions packages/service/comment/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 {Message} from './type.js';

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

type CommonMessage = AlwatrDocumentObject & {
user: unknown;
replyId?: string;
};

export type TextMessage = CommonMessage & {
type: 'text';
text: string;
};

export type PhotoMessage = CommonMessage & {
type: 'photo';
photo: Record<string, unknown>;
};

export type Message = TextMessage | PhotoMessage;
Loading

0 comments on commit 591444a

Please sign in to comment.