REST Service for POPCORN - ILIAS
alex
2025-06-04 6cb1d311f04d1f68f2d194524974b0f7bc0d31bd
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
175
const path = require("path")
const fastify = require('fastify')({
   logger: true
})
const _ = require("lodash")
const db = require("./lib/db")
 
const settings = require("./settings")
const fs = require("node:fs")
 
/////////////////////////////////////////////////////////////////////////
 
// 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 {
      console.log("NO AUTH FOR ",req.url)
   }
})
 
fastify
   /////// USER ////////////////////////////////////////////////////////////////
   .get('/api/users', async function (req, res) {
      const {offset, limit} = req.query
      const users = await db.getUsers(offset, limit)
      return res.send(users)
   })
   .get("/api/users/count", async function (req, res) {
      const count = await db.getUserCount()
      return res.send(count)
   })
   .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])
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .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)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
 
   /////// ref_id / obj_id  ////////////////////////////////////////////////////////////////
 
   .get("/api/ref_id/:ref_id", async function (req, res) {
      const {ref_id} = req.params
      const data = await db.getObjIdFromRefId(ref_id)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/obj_id/:obj_id", async function (req, res) {
      const {obj_id} = req.params
      let data = await db.getRefIdFromObjId(obj_id)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
 
   /////// Kurs ////////////////////////////////////////////////////////////////
   .get("/api/kurs", async function (req, res) {
      let data = await db.getKurse()
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/kurs/:refId", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKurs(refId)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/kurs/items/:refId", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKursItems(refId)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/kurs/teilnehmer/:refId", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKursTeilnehmer(refId)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
   .get("/api/kurs/teilnehmer/:refId/count", async function (req, res) {
      const {refId} = req.params
      let data = await db.getKursTeilnehmerCount(refId)
      if (data) {
         return res.send(data)
      }
      else {
         return res.code(404).send({status: "error", msg: "not found"})
      }
   })
 
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)
})
 
 
/////////////////////////////////////////////////////////////////////////
 
fastify.listen({port: settings.port}, function (err, address) {
   console.log("📡 -=> Listening on", address)
   if (err) {
      fastify.log.error(err)
      process.exit(1)
   }
   // Server is now listening on ${address}
})
 
/////////////////////////////////////////////////////////////////////////
 
async function promiseDelay (ms) {
   return new Promise(resolve => setTimeout(resolve, ms))
}