74 lines
4.8 KiB
Markdown
74 lines
4.8 KiB
Markdown
# report-server-ts
|
|
เป็น Web API ออกแบบมาเพื่อสร้างเอกสาร สามารถใช้ frontend โดยตรง เพื่อจะได้ไม่ต้องเขียนโปรแกรมทำเอกสารเฉพาะแต่ละตัวออกมา ปรับปรุงของเดิมจาก
|
|
|
|
|
|
## Feature & Change
|
|
- แก้ไขจากเดิมตัวเดิมเป็น JavaScript เป็น TypeScript ตัด pandoc ออก
|
|
- ใช้ docx xlsx เป็น template เพื่อสร้างเอกสารคล้าย Mail Merge ลูกค้าออกแบบเองได้
|
|
- รายงานเป็นไฟล์แบบเดียวกับ template หรือแปลงเป็น pdf หรือฟอร์แม็ตอื่นๆได้
|
|
- แปลงไฟล์จาก MS Office เป็น PDF
|
|
- โค้ดมีการ obfuscator
|
|
- API document ด้วย swagger
|
|
- มีโปรแกรมช่วยทดสอบ template แบบง่ายๆให้
|
|
- Docker Image จะใช้แบบ Standalone หรือเป็น Microservice ร่วมกับโปรเจ็กอื่นๆได้ ใช้งานทันที
|
|
|
|
## ติดตั้ง
|
|
clone project แล้วไปหัวข้อใช้งานได้เลย หัวนี้ไว้เพื่ออ้างอิงเท่านั้น
|
|
ตั้งค่าของ 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
|
|
npm i docx-templates xlsx-template-next swagger-ui-express swagger-jsdoc yaqrcode libreoffice-file-converter
|
|
npm i -D @types/swagger-ui-express @types/swagger-jsdoc
|
|
# 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
|
|
|
|
```
|
|
## การใช้งาน
|
|
ดู scripts ใน package.json และ compose.yaml
|
|
```bash
|
|
npm run dev
|
|
npm run build
|
|
npm run serve
|
|
npm run obfuscator
|
|
npm run preview
|
|
npm run build:docker
|
|
docker compose up -d
|
|
```
|
|
|
|
## ทดสอบ template
|
|
ไปที่โฟลเดอร์ test-run มีโปรแกรมเพื่อทดสอบ template ที่ออกมามาก่อน ไปว่างในเซิร์ฟเวอร์ ใช้ค่า default ได้เลย แปลงไปไฟล์แบบต่างๆที่ Libreoffice รองรับได้ ควรทดสอบรูปแบบข้อมูล(json) ให้เข้ากับ template(docx,xlsx) ถ้าเกิดปัญหา ถ้าค่าไม่ครบ template แบบ docx จะ error log ที่เซิร์ฟเวอร์ ส่วน xlsx ไม่แจ้งปัญหา แค่ไม่แสดงค่านั้นๆ คู่มือการใช้งานที่สมบูรณ์ให้ไปที่เวปของ library ที่ใช้
|
|
[docx-templates](https://www.npmjs.com/package/docx-templates) และ
|
|
[xlsx-template-next](https://www.npmjs.com/package/xlsx-template-next)
|
|
|
|
``` bash
|
|
$ 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
|
|
```
|
|
|
|
# Bun Note
|
|
เริ่มแรกในการพอร์ตจาก JavaScript ลองใช้ Bun(TypeScript) แทน Node.js ตัว Bun ค่อนข้างน่าประทับใจใช้ TypeScript โดยตรงไม่ต้องตั้งค่า หรือติดตั้งเพิ่ม แต่มีปัญหากับ libreoffice-file-converter ต้องแก้ค่าใน package.json
|
|
|
|
docker-template คาดว่าเป็นปัญหาจาก eval ทำให้ ส่วน EXEC กับ custom function ทำงานไม่ได้ เลยกลับมาใช้ node
|
|
|
|
## Todo
|
|
- รองรับ Authentication Header เพื่อให้ยูสเซอร์ในระบบใช้งานได้เท่านั้น
|
|
- หาทางสร้างเอกสารจาก text เช่น Markdown เป็นเอกสาร MS Office
|