| | |
| | | const path = require("path") |
| | | const fastify = require('fastify')({ |
| | | logger: true |
| | | }) |
| | | const _ = require("lodash") |
| | | 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) => { |
| | | console.log(req.url) |
| | | const token = req.query.token |
| | | if (token !== settings.authtoken) { |
| | | 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) |
| | | } |
| | | }) |
| | | |
| | | /////// 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 |
| | | .get('/users', async function (req, res) { |
| | | const {offset, limit} = req.query |
| | | const users = await db.getUsers(offset, limit) |
| | | /////// USER //////////////////////////////////////////////////////////////// |
| | | .get('/api/users', async function (req, res) { |
| | | const {offset, limit, search} = req.query |
| | | const users = await db.getUsers(offset, limit, search) |
| | | 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) { |
| | | return res.send(user[0]) |
| | | if (user) { |
| | | return res.send(user) |
| | | } |
| | | else { |
| | | 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) |
| | |
| | | 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("/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.getKursItems2(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"}) |
| | | } |
| | | }) |
| | | |
| | | |
| | | /////// STATIC //////////////////////////////////////////////////////////////// |
| | | |
| | | |
| | | 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) |
| | |
| | | |
| | | ///////////////////////////////////////////////////////////////////////// |
| | | |
| | | async function promiseDelay(ms) { |
| | | return new Promise(resolve => setTimeout(resolve, ms)) |
| | | async function promiseDelay (ms) { |
| | | return new Promise(resolve => setTimeout(resolve, ms)) |
| | | } |