hrms-report-template/README.md

80 lines
6.3 KiB
Markdown
Raw Normal View History

2023-10-03 22:11:16 +07:00
# report-server-ts
2023-10-08 18:29:28 +07:00
เป็น Web API ออกแบบมาเพื่อสร้างเอกสารจาก templste สามารถใช้ frontend โดยตรง เพื่อจะได้ไม่ต้องทำ backend เพื่อสร้างเอกสารเฉพาะตัวออกมา
2023-10-07 17:56:17 +07:00
## Feature & Change
- แก้ไขจากเดิมตัวเดิมเป็น JavaScript เป็น TypeScript ตัด pandoc ออก
- ใช้ docx xlsx เป็น template เพื่อสร้างเอกสารคล้าย Mail Merge ลูกค้าออกแบบเองได้
2023-10-08 18:29:28 +07:00
- รายงานที่ได้เป็นไฟล์แบบเดียวกับ template หรือแปลงเป็น pdf หรือฟอร์แม็ตของ LibreOffice ได้
- แปลงไฟล์จาก MS Office เป็น PDF รองรับการตัดคำไทย
- โค้ดมีการ obfuscator เพื่อลดขนาดและกันลูกค้าเอาโค้ดไปใช้
- มี Swagger ไว้ทดสอบ API มี libs/swagger-specs.json เพื่อนำเข้า Postman หรือเครื่องมื่อื่นๆได้
- มีโปรแกรมช่วยทดสอบ template แบบง่ายๆ ให้ทดสอบก่อนเอาเข้าเซิร์ฟเวอร์
2023-10-07 17:56:17 +07:00
- Docker Image จะใช้แบบ Standalone หรือเป็น Microservice ร่วมกับโปรเจ็กอื่นๆได้ ใช้งานทันที
2023-10-08 18:29:28 +07:00
- Docker image เหลือ 1.76GB จากเดิม 3.5GB
2023-10-07 17:56:17 +07:00
## ติดตั้ง
2023-10-08 18:29:28 +07:00
รันโปรเจ็กบน Linux ,clone project ติดตั้ง Fonts และ LibreOffice ตามวิธีใน Dockerfile แล้วไปหัวข้อการใช้งานได้เลย หัวนี้ไว้เพื่ออ้างอิงเท่านั้น ใช้ node 20.7.0 บน Linux AMD x86-64
2023-10-07 17:56:17 +07:00
ตั้งค่าของ TypeScript [ตามเวปนี้](https://www.geeksforgeeks.org/how-to-use-express-in-typescript/) ให้ใช้ ES module ได้ด้วย
```bash
npm i express
npm i -D typescript @types/express @types/node ts-node
2023-10-08 18:29:28 +07:00
npm i docx-templates xlsx-template-next swagger-ui-express swagger-jsdoc yaqrcode cors libreoffice-file-converter
npm i -D @types/swagger-ui-express @types/swagger-jsdoc @types/cors
# obfuscate code tools
npm i -D javascript-obfuscator
# add type support for yaqrcode
cd node_modules/yaqrcode
wget https://raw.githubusercontent.com/zenozeng/node-yaqrcode/master/index.d.ts
2023-10-08 18:29:28 +07:00
```
2023-10-07 17:56:17 +07:00
## การใช้งาน
2023-10-08 18:29:28 +07:00
ดู scripts ใน package.json และ compose.yaml ให้ดูให้เข้าใจ การสร้าง docker อิมเมจ จะต้อง swaggergen,obfuscator ก่อน หลังจากนั้นขึ้น Docker registry
บน production ให้ดู compose.yaml จะต้องนำ template ไปเก็บไว้ที่โฟลเดอร์ templates ด้วย
```bash
npm run dev
npm run build
npm run serve
npm run obfuscator
npm run preview
npm run build:docker
docker compose up -d
2023-10-08 18:29:28 +07:00
npm run push:docker
```
2023-10-07 17:56:17 +07:00
## ทดสอบ template
2023-10-08 18:29:28 +07:00
ไปที่โฟลเดอร์ test-run มีโปรแกรมเพื่อทดสอบ template ให้ทดสอบที่นี้ก่อนเอา template ไปวางในเซิร์ฟเวอร์ มีค่า default สำหรับการทดสอบที่ใช้ได้เลย สามารถแปลงไปไฟล์แบบต่างๆที่ Libreoffice รองรับ(จำเป็นต้องติดตั้ง ) ควรทดสอบรูปแบบข้อมูล(json) ให้เข้ากับ template(docx,xlsx) ถ้าเกิดปัญหา ถ้าค่าไม่ครบ template แบบ docx จะ error log ที่เซิร์ฟเวอร์ ส่วน xlsx ไม่แจ้งปัญหา แค่ไม่แสดงค่านั้นๆ คู่มือการใช้งานที่สมบูรณ์ให้ไปที่เวปของ library ที่ใช้
2023-10-07 17:56:17 +07:00
[docx-templates](https://www.npmjs.com/package/docx-templates) และ
[xlsx-template-next](https://www.npmjs.com/package/xlsx-template-next)
``` bash
cd test-run
$ npx ts-node docx-template.ts
Output extension(docx,pdf,odt): pdf
JSON data path(./docx.json):
Base path of templates-docx(..):
$ npx ts-node xlsx-template.ts
Output extension(xlsx,pdf): ods
JSON data path(./xlsx.json):
Base path of templates-docx(..):
```
# Build docker
```bash
docker build -t docker.frappet.com/demo/report-server .
docker push docker.frappet.com/demo/report-server
docker run --name rserver -p 80:3000 docker.frappet.com/demo/report-server
```
2023-10-07 17:56:17 +07:00
# Bun Note
เริ่มแรกในการพอร์ตจาก JavaScript ลองใช้ Bun(TypeScript) แทน Node.js ตัว Bun ค่อนข้างน่าประทับใจใช้ TypeScript โดยตรงไม่ต้องตั้งค่า หรือติดตั้งเพิ่ม แต่มีปัญหากับ libreoffice-file-converter ต้องแก้ค่าใน package.json
2023-10-08 18:29:28 +07:00
docker-template ฟังก์ชั่นพื้นฐานใช้งานพอได้ ส่วน EXEC กับ custom function ทำงานไม่ได้ คาดว่าเป็นปัญหาจาก eval เลยกลับมาใช้ node เหมือนเดิม
2023-10-07 17:56:17 +07:00
## Todo
- รองรับ Authentication Header เพื่อให้ยูสเซอร์ในระบบใช้งานได้เท่านั้น
- หาทางสร้างเอกสารจาก text เช่น Markdown เป็นเอกสาร MS Office
2023-10-08 18:29:28 +07:00
- น่าจะทำ license key เผื่อขายให้ลูกค้าติดตั้งใช้งานต่อ (โค้ดที่ผ่าน obfuscator แล้วย้อนกลับมาได้ง่ายหรือเปล่า ?)