From 62f8d28c73eb042edf9c31127f4c87f1f9e6ac4c Mon Sep 17 00:00:00 2001
From: alex <alex@alexloehr.net>
Date: Wed, 04 Jun 2025 18:30:05 +0000
Subject: [PATCH] adding user teilnahmen
---
app.js | 74 ++++++++++++++++++++++++++++--------
1 files changed, 57 insertions(+), 17 deletions(-)
diff --git a/app.js b/app.js
index 03ff46e..cc1bd5b 100644
--- a/app.js
+++ b/app.js
@@ -6,40 +6,43 @@
const db = require("./lib/db")
const settings = require("./settings")
+const fs = require("node:fs")
/////////////////////////////////////////////////////////////////////////
-fastify.register(require('@fastify/static'), {
- root: path.join(__dirname, 'vue/dist'),
- prefix: '/ui/', // optional: default '/'
- // constraints: { host: 'example.com' } // optional: default {}
-})
+// Compress
+let compress = require('@fastify/compress')
+// fastify.register(compress, { global: true })
+fastify.register(compress)
// AUTH
fastify.addHook("onRequest", async (req, res) => {
console.log(req.url)
const token = req.query.token
+ console.log(req.url)
if (token !== settings.authtoken && !req.url.startsWith("/ui/")) {
console.error("# AUTH ERROR #", token)
await promiseDelay(500) // delay response to avoid denial of service attacks
res.code(403)
return res.send({status: "error", error: "access denied"})
}
- else {}
+ else {
+ console.log("NO AUTH FOR ",req.url)
+ }
})
fastify
/////// USER ////////////////////////////////////////////////////////////////
- .get('/users', async function (req, res) {
+ .get('/api/users', async function (req, res) {
const {offset, limit} = req.query
const users = await db.getUsers(offset, limit)
return res.send(users)
})
- .get("/users/count", async function (req, res) {
+ .get("/api/users/count", async function (req, res) {
const count = await db.getUserCount()
return res.send(count)
})
- .get("/user/login/:login", async function (req, res) {
+ .get("/api/user/login/:login", async function (req, res) {
const {login} = req.params
const user = await db.getUserByLogin(login)
if (user.length) {
@@ -49,8 +52,11 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/user/userid/:userid", async function (req, res) {
+ .get("/api/user/userid/:userid", async function (req, res) {
const {userid} = req.params
+ if(!userid || isNaN(Number(userid))) {
+ return res.code(500).send({status: "error", msg: "userid error"})
+ }
const user = await db.getUserByUserId(userid)
if (user) {
return res.send(user)
@@ -59,10 +65,24 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
+ .get("/api/user/teilnahmen/:userId", async function (req, res) {
+ let userId = req.params.userId
+ console.log(`--------${userId}-----------`, typeof userId)
+ if(!userId || isNaN(Number(userId))) {
+ return res.code(500).send({status: "error", msg: "userId error"})
+ }
+ const tn = await db.getUserTeilnahmen(userId)
+ if (tn) {
+ return res.send(tn)
+ }
+ else {
+ return res.code(404).send({status: "error", msg: "not found"})
+ }
+ })
/////// ref_id / obj_id ////////////////////////////////////////////////////////////////
- .get("/ref_id/:ref_id", async function (req, res) {
+ .get("/api/ref_id/:ref_id", async function (req, res) {
const {ref_id} = req.params
const data = await db.getObjIdFromRefId(ref_id)
if (data) {
@@ -72,7 +92,7 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/obj_id/:obj_id", async function (req, res) {
+ .get("/api/obj_id/:obj_id", async function (req, res) {
const {obj_id} = req.params
let data = await db.getRefIdFromObjId(obj_id)
if (data) {
@@ -84,7 +104,7 @@
})
/////// Kurs ////////////////////////////////////////////////////////////////
- .get("/kurs", async function (req, res) {
+ .get("/api/kurs", async function (req, res) {
let data = await db.getKurse()
if (data) {
return res.send(data)
@@ -93,7 +113,7 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/kurs/:refId", async function (req, res) {
+ .get("/api/kurs/:refId", async function (req, res) {
const {refId} = req.params
let data = await db.getKurs(refId)
if (data) {
@@ -103,7 +123,7 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/kurs/items/:refId", async function (req, res) {
+ .get("/api/kurs/items/:refId", async function (req, res) {
const {refId} = req.params
let data = await db.getKursItems(refId)
if (data) {
@@ -113,7 +133,7 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/kurs/teilnehmer/:refId", async function (req, res) {
+ .get("/api/kurs/teilnehmer/:refId", async function (req, res) {
const {refId} = req.params
let data = await db.getKursTeilnehmer(refId)
if (data) {
@@ -123,7 +143,7 @@
return res.code(404).send({status: "error", msg: "not found"})
}
})
- .get("/kurs/teilnehmer/:refId/count", async function (req, res) {
+ .get("/api/kurs/teilnehmer/:refId/count", async function (req, res) {
const {refId} = req.params
let data = await db.getKursTeilnehmerCount(refId)
if (data) {
@@ -134,6 +154,26 @@
}
})
+fastify.register(require('@fastify/static'), {
+ root: path.join(__dirname, 'vue/dist'),
+ prefix: '/ui/', // optional: default '/'
+
+ // constraints: { host: 'example.com' } // optional: default {}
+})
+
+
+// fastify.get('*', function (req, reply) {
+// console.log("!!!!!!!!! send index")
+// // index.html should never be cached
+// reply.sendFile('dist/index.html', {maxAge: 0, immutable: false})
+// })
+
+const indexFile = fs.readFileSync(path.join(__dirname, "vue/dist", 'index.html'), 'utf8')
+fastify.setNotFoundHandler(function (req, res) {
+ console.log("!!!")
+ // res.sendFile("vue/dist/index.html")
+ res.type("text/html").send(indexFile)
+})
/////////////////////////////////////////////////////////////////////////
--
Gitblit v1.8.0