-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ::..'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ;)', | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters