REST Service for POPCORN - ILIAS
alex
2025-08-13 c7bb3d470433e1722212d3819cf3676fe4c0c155
app.js
@@ -1,9 +1,15 @@
const path = require("path")
const fastify = require('fastify')({
   logger: true
   logger: false,
   // logger: true
})
const _ = require("lodash")
const fs = require("node:fs")
const dayjs = require("dayjs")
const log = require("./logger")
log.info("")
log.info(`--- STARTUP ${dayjs().format("DD.MM.YYYY HH:mm:ss")} ---`)
const db = require("./lib/db")
const libIlias = require("./lib/libIlias")
@@ -19,17 +25,19 @@
// AUTH
fastify.addHook("onRequest", async (req, res) => {
   console.log(req.url)
   // custom logging
   log.info(`${req.method} ${req.url}`);
   // 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)
      log.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 {
      console.log("AUTH FOR ", req.url)
      // log.debug("AUTH FOR ", req.url)
   }
})
@@ -40,19 +48,19 @@
searchLib.doIndex().catch(console.error)
fastify
   .get("/api/search/user", async function (req, res) {
      console.log(req.query)
      log.info(req.query)
      const search = req.query?.search
      if (!search) {
         return res.code(422).send({status: "error", msg: "no search"})
      }
      else {
         console.log(search)
         log.info(search)
         const data = await searchLib.search(search)
         return res.send(data)
      }
   })
   .post("/api/search/reindex", async function (req, res) {
      console.log("REINDEX ++++")
      log.info("REINDEX ++++")
      const start = Date.now()
      await searchLib.doIndex().catch(console.error)
      return res.send({
@@ -97,7 +105,7 @@
   })
   .get("/api/user/teilnahmen/:userId", async function (req, res) {
      let userId = req.params.userId
      console.log(`--------${userId}-----------`, typeof userId)
      log.debug(`--------${userId}-----------`, typeof userId)
      if (!userId || isNaN(Number(userId))) {
         return res.code(500).send({status: "error", msg: "userId error"})
      }
@@ -127,7 +135,7 @@
      }
      else {
         const res2 = await libIlias.deleteUser(usr_id)
         console.log(res2)
         log.info(res2)
         return res.send(res2)
      }
   })
@@ -196,6 +204,27 @@
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/kurs/:refId/teilnehmerByRole", async function (req, res) {
      const {refId} = req.params
      const {obj_id} = await db.getObjIdFromRefId(refId)
      let data = await db.getKursTeilnehmerByRole(obj_id)
      return res.send(data)
   })
   .get("/api/kurs/:refId/roles", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKursRoles(refId)
      return res.send(data)
   })
   .get("/api/kurs/:refId/teilnehmer/:userId", async function (req, res) {
      const {refId, userId} = req.params
      let data = await db.getSingleKursTeilnehmer(refId, userId)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   // .get("/api/kurs/:refId/teilnehmer/count", async function (req, res) {
   //    const {refId} = req.params
   //    let data = await db.getKursTeilnehmerCount(refId)
@@ -211,7 +240,7 @@
   .post("/api/kurs/:refId/status/:usrId", async function (req, res) {
      const {refId, usrId} = req.params
      const {passed, status} = req.body
      if (!refId || !usrId || _.isEmpty(passed) || _.isEmpty(status)) {
      if (!refId || !usrId || _.isNil(passed) || _.isNil(status)) {
         throw {
            statusCode: 400,
            status: "error",
@@ -264,7 +293,7 @@
const indexFile = fs.readFileSync(path.join(__dirname, "vue/dist", 'index.html'), 'utf8')
fastify.setNotFoundHandler(function (req, res) {
   console.log("!!!")
   log.error("!!! Not found")
   // res.sendFile("vue/dist/index.html")
   res.type("text/html").send(indexFile)
})
@@ -273,9 +302,11 @@
/////////////////////////////////////////////////////////////////////////
fastify.listen({port: settings.port}, function (err, address) {
   console.log("📡 -=> Listening on", address)
   console.log(address)
   log.info(`📡 -=> Listening on ${address}`)
   if (err) {
      fastify.log.error(err)
      // fastify.log.error(err)
      log.error(err)
      process.exit(1)
   }
   // Server is now listening on ${address}
@@ -286,3 +317,4 @@
async function promiseDelay (ms) {
   return new Promise(resolve => setTimeout(resolve, ms))
}