Skip to content

Commit

Permalink
fea(form): new micro service
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Jan 14, 2023
1 parent c3bb91d commit 73ee548
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 0 deletions.
7 changes: 7 additions & 0 deletions services/form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Form Service

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

## How to use

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

### home
GET {{apiUrl}}/

### Add new form
PUT {{apiUrl}}/
Authorization: Bearer {{token}}
Content-Type: application/json

{
"filed1": "test1",
"filed2": "test2",
"filed13": "test3"
}
65 changes: 65 additions & 0 deletions services/form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "@alwatr/form",
"version": "0.27.0",
"description": "Alwatr Form API Microservice, Distributed simple and fast form nanoservice base on @alwatr/storage-server",
"type": "module",
"keywords": [
"api",
"form",
"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": "services/form"
},
"homepage": "https://github.com/AliMD/alwatr/tree/main/services/form#readme",
"bugs": {
"url": "https://github.com/AliMD/alwatr/issues"
},
"scripts": {
"b": "yarn build",
"c": "yarn clean",
"cb": "run-s clean build",
"s": "yarn start",
"w": "yarn watch",
"start": "NODE_OPTIONS=--enable-source-maps run-s clean build serve",
"build": "yarn build:es --analyze=verbose",
"build:ts": "tsc --build",
"build:es": "esbuild src/index.ts --platform=node --target=node19 --bundle --format=esm --minify --sourcemap --outdir=dist --out-extension:.js=.mjs",
"clean": "rimraf dist build .tsbuildinfo **/*.{d.ts,map} src/**/*.{js,cjs,mjs}",
"serve": "node --enable-source-maps dist/index.mjs",
"serve:debug": "node --inspect --enable-source-maps dist/index.mjs",
"watch": "run-s clean build && run-p watch:es watch:node",
"watch:node": "nodemon -w dist/ --enable-source-maps dist/index.mjs",
"watch:debug-node": "nodemon -w dist/ --inspect --enable-source-maps dist/index.mjs",
"watch:ts": "yarn build:ts --watch --preserveWatchOutput",
"watch:es": "yarn build:es --watch"
},
"devDependencies": {
"@alwatr/logger": "^0.27.0",
"@alwatr/nano-server": "^0.27.0",
"@alwatr/type": "^0.27.0",
"@alwatr/storage-client": "^0.27.0",
"@types/node": "^18.11.18",
"esbuild": "^0.16.14",
"nodemon": "^2.0.20",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"typescript": "^4.9.4"
}
}
20 changes: 20 additions & 0 deletions services/form/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {createLogger} from '@alwatr/logger';

export const logger = createLogger('form');

export const config = {
nanoServer: {
host: process.env.HOST ?? '0.0.0.0',
port: process.env.PORT != null ? +process.env.PORT : 8000,
allowAllOrigin: true,
accessToken: process.env.ACCESS_TOKEN ?? 'YOUR_SECRET_TOKEN',
},
storage: {
name: process.env.STORAGE_NAME ?? 'form',
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);
7 changes: 7 additions & 0 deletions services/form/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {logger} from './config.js';

import './route/home.js';
import './route/put.js';
import './route/storage.js';

logger.logOther('..:: Alwatr Comment Nanoservice API ::..');
5 changes: 5 additions & 0 deletions services/form/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);
7 changes: 7 additions & 0 deletions services/form/src/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {AlwatrStorageClient} from '@alwatr/storage-client';

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

import type {AlwatrDocumentObject} from '@alwatr/type';

export const storageClient = new AlwatrStorageClient<AlwatrDocumentObject>(config.storage);
9 changes: 9 additions & 0 deletions services/form/src/route/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {nanoServer} from '../lib/nano-server.js';

nanoServer.route('GET', '/', () => ({
ok: true,
data: {
app: 'Alwatr Form API Microservice',
message: 'Hello ;)',
},
}));
39 changes: 39 additions & 0 deletions services/form/src/route/put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {config, logger} from '../config.js';
import {nanoServer} from '../lib/nano-server.js';
import {storageClient} from '../lib/storage.js';

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

nanoServer.route('PUT', '/', setForm);

async function setForm(connection: AlwatrConnection): Promise<AlwatrServiceResponse> {
logger.logMethod('setForm');

connection.requireToken(config.nanoServer.accessToken);

const bodyJson = await connection.requireJsonBody<AlwatrDocumentObject>();

bodyJson.id ??= 'auto_increment';

try {
return {
ok: true,
data: await storageClient.set(bodyJson),
};
}
catch (_err) {
const err = _err as Error;
logger.error('setForm', err.message || 'storage_error', err);
return {
ok: false,
statusCode: 500,
errorCode: 'storage_error',
meta: {
name: err.name,
message: err.message,
cause: err.cause,
},
};
}
}
31 changes: 31 additions & 0 deletions services/form/src/route/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {config, logger} from '../config.js';
import {nanoServer} from '../lib/nano-server.js';
import {storageClient} from '../lib/storage.js';

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

nanoServer.route('GET', '/storage', getFormStorage);

async function getFormStorage(connection: AlwatrConnection): Promise<AlwatrServiceResponse> {
logger.logMethod('getFormStorage');

connection.requireToken(config.nanoServer.accessToken);

try {
return await storageClient.getStorage();
}
catch (_err) {
const err = _err as Error;
logger.error('getFormStorage', err.message || 'storage_error', err);
return {
ok: false,
statusCode: 500,
errorCode: 'storage_error',
meta: {
name: err.name,
message: err.message,
cause: err.cause,
},
};
}
}
20 changes: 20 additions & 0 deletions services/form/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"target": "ESNext",
"composite": true,
"tsBuildInfoFile": ".tsbuildinfo",
"rootDir": "src",
"outDir": "build"
// "noEmit": true
},

"include": ["src/**/*.ts"],
"exclude": [],
"references": [
{"path": "../../core/nano-server"},
{"path": "../../core/logger"},
{"path": "../../core/type"},
{"path": "../../core/storage-client"}
]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

// services
{"path": "./services/comment"},
{"path": "./services/form"},
{"path": "./services/starter"},
{"path": "./services/storage-server"},
{"path": "./services/telegram-notifier"},
Expand Down

0 comments on commit 73ee548

Please sign in to comment.