REST Service for POPCORN - ILIAS
alex
2025-06-10 0c2d8b058b4edd21922b5b4eca6c149046aee090
app.js
@@ -3,12 +3,18 @@
   logger: true
})
const _ = require("lodash")
const db = require("./lib/db")
const settings = require("./settings")
const fs = require("node:fs")
const db = require("./lib/db")
const settings = require("./settings")
const search = require("./lib/search.js")
/////////////////////////////////////////////////////////////////////////
// Compress
let compress = require('@fastify/compress')
// fastify.register(compress, { global: true })
fastify.register(compress)
// AUTH
fastify.addHook("onRequest", async (req, res) => {
@@ -22,15 +28,32 @@
      return res.send({status: "error", error: "access denied"})
   }
   else {
      console.log("NO AUTH FOR ",req.url)
      console.log("NO AUTH FOR ", req.url)
   }
})
/////// SEARCH ////////////////////////////////////////////////////////////////
const searchLib = require("./lib/search")
searchLib.doIndex().catch(console.error)
fastify.get("/api/search/user", async function (req, res) {
   console.log(req.query)
   const search = req.query?.search
   if (!search) {
      return res.code(422).send({status: "error", msg: "no search"})
   }
   else {
      console.log(search)
      const data = await searchLib.search(search)
      return res.send(data)
   }
})
fastify
   /////// USER ////////////////////////////////////////////////////////////////
   .get('/api/users', async function (req, res) {
      const {offset, limit} = req.query
      const users = await db.getUsers(offset, limit)
      const {offset, limit, search} = req.query
      const users = await db.getUsers(offset, limit, search)
      return res.send(users)
   })
   .get("/api/users/count", async function (req, res) {
@@ -40,8 +63,8 @@
   .get("/api/user/login/:login", async function (req, res) {
      const {login} = req.params
      const user = await db.getUserByLogin(login)
      if (user.length) {
         return res.send(user[0])
      if (user) {
         return res.send(user)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
@@ -49,12 +72,26 @@
   })
   .get("/api/user/userid/:userid", async function (req, res) {
      const {userid} = req.params
      if(!userid || isNaN(Number(userid))) {
      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)
      }
      else {
         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"})
@@ -85,6 +122,7 @@
   })
   /////// Kurs ////////////////////////////////////////////////////////////////
   .get("/api/kurs", async function (req, res) {
      let data = await db.getKurse()
      if (data) {
@@ -106,7 +144,7 @@
   })
   .get("/api/kurs/items/:refId", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKursItems(refId)
      let data = await db.getKursItems2(refId)
      if (data) {
         return res.send(data)
      }
@@ -135,6 +173,10 @@
      }
   })
/////// STATIC ////////////////////////////////////////////////////////////////
fastify.register(require('@fastify/static'), {
   root: path.join(__dirname, 'vue/dist'),
   prefix: '/ui/', // optional: default '/'
@@ -149,6 +191,12 @@
//    reply.sendFile('dist/index.html', {maxAge: 0, immutable: false})
// })
// const favicon = fs.readFileSync("./favicon-32x32.webp")
// fastify.get("/ui/favicon.webp", async function (req, res) {
//    console.log("<<<<<<<<<<<<<<<<<<<<< favicon", favicon.length)
//    res.type("image/webp").send(favicon)
// })
const indexFile = fs.readFileSync(path.join(__dirname, "vue/dist", 'index.html'), 'utf8')
fastify.setNotFoundHandler(function (req, res) {
   console.log("!!!")