-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
174 lines (147 loc) · 4.21 KB
/
Copy pathserver.js
File metadata and controls
174 lines (147 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import express from "express";
import QRCode from "qrcode";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
const PORT = process.env.PORT || 3000;
const BASE_URL = process.env.BASE_URL || "";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
const documents = {
"DOC-1001": {
doc_id: "DOC-1001",
company_name: "شركة عبد الستار التجريبية",
owner_name: "Abdulstar Zeki",
driver_name: "جلال مهدي",
vehicle_number: "42040 دهوك",
governorate: "دهوك",
material: "مواد تجريبية",
quantity: "100",
unit: "كرتون",
destination: "أربيل",
status: "VALID",
numberOfVersion: 1,
created_at: "2026-07-10",
note: "نموذج تجريبي من سيرفرك - غير حكومي"
},
"DOC-1002": {
doc_id: "DOC-1002",
company_name: "مختبر QR Public",
owner_name: "Security Test",
driver_name: "اياس سلمان",
vehicle_number: "56680 دهوك",
governorate: "دهوك",
material: "اختبار كاميرا",
quantity: "250",
unit: "طن",
destination: "بغداد",
status: "VALID",
numberOfVersion: 2,
created_at: "2026-07-10",
note: "هذا QR خاص بسيرفر تجريبي"
}
};
function cleanCode(value) {
return String(value || "").trim();
}
function findDocument(code) {
return documents[cleanCode(code)] || null;
}
function getBaseUrl(req) {
if (BASE_URL) return BASE_URL.replace(/\/$/, "");
const proto = req.headers["x-forwarded-proto"] || req.protocol || "http";
const host = req.headers["x-forwarded-host"] || req.headers.host;
return `${proto}://${host}`;
}
app.get("/qrpubliclink/:qrcode", (req, res) => {
res.sendFile(path.join(__dirname, "public", "qrpubliclink.html"));
});
app.get("/qrpubliclink", (req, res) => {
if (req.query.code) {
return res.sendFile(path.join(__dirname, "public", "qrpubliclink.html"));
}
return res.sendFile(path.join(__dirname, "public", "scanner.html"));
});
app.get("/api/verify/:code", (req, res) => {
const code = cleanCode(req.params.code);
const doc = findDocument(code);
if (!doc) {
return res.status(404).json({
success: false,
error: "DOCUMENT_NOT_FOUND",
message: "لم يتم العثور على الوثيقة"
});
}
return res.json({
success: true,
document: doc
});
});
app.post("/api/read-qr", (req, res) => {
const code = cleanCode(req.body.QRcode || req.body.code);
const doc = findDocument(code);
if (!doc) {
return res.status(404).json({
success: false,
error: "DOCUMENT_NOT_FOUND",
message: "لم يتم العثور على الوثيقة"
});
}
return res.json({
success: true,
data: {
info: {
fullName: doc.owner_name,
orgName: doc.company_name,
orgPathInfo: `${doc.company_name} / ${doc.governorate}`
},
numberOfVersion: doc.numberOfVersion,
showIn: true,
document: doc
}
});
});
app.get("/api/make-qr/:code", async (req, res) => {
const code = cleanCode(req.params.code);
const doc = findDocument(code);
if (!doc) {
return res.status(404).json({
success: false,
error: "DOCUMENT_NOT_FOUND"
});
}
const qrLink = `${getBaseUrl(req)}/qrpubliclink/${encodeURIComponent(code)}`;
const png = await QRCode.toBuffer(qrLink, {
width: 420,
margin: 2,
errorCorrectionLevel: "M"
});
res.setHeader("Content-Type", "image/png");
res.send(png);
});
app.get("/api/make-qr-url/:code", (req, res) => {
const code = cleanCode(req.params.code);
const doc = findDocument(code);
if (!doc) {
return res.status(404).json({
success: false,
error: "DOCUMENT_NOT_FOUND"
});
}
const qrLink = `${getBaseUrl(req)}/qrpubliclink/${encodeURIComponent(code)}`;
res.json({
success: true,
code,
qrLink
});
});
app.get("/", (req, res) => {
res.redirect("/qrpubliclink");
});
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on port ${PORT}`);
console.log("Open /qrpubliclink");
});