2023-06-11 00:22:06 +03:00
|
|
|
const express = require("express")
|
|
|
|
const { MongoClient } = require("mongodb")
|
2023-05-27 21:16:21 +03:00
|
|
|
require("dotenv").config()
|
2023-04-16 20:12:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* error: 0 -> no error
|
|
|
|
* error: 1 -> parameter error
|
|
|
|
* error: 2 -> auth error
|
2023-05-27 21:16:21 +03:00
|
|
|
* error: 3 -> not found error
|
2023-06-24 18:48:18 +03:00
|
|
|
*/
|
2023-06-11 00:22:06 +03:00
|
|
|
|
2023-06-24 18:48:18 +03:00
|
|
|
const db = new MongoClient(process.env.DATABASE)
|
2023-06-11 00:22:06 +03:00
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
2023-06-24 18:48:18 +03:00
|
|
|
app.use(express.urlencoded({ extended: false }))
|
|
|
|
app.use(async (req, res, next) => {
|
|
|
|
await db.connect()
|
|
|
|
req.db = db
|
|
|
|
next()
|
2023-04-16 20:12:24 +03:00
|
|
|
})
|
|
|
|
|
2023-06-11 20:19:35 +03:00
|
|
|
const { auth, authware } = require("./routes/auth.js")
|
|
|
|
// anything starts with "add"
|
|
|
|
// requires admin privs
|
|
|
|
app.use("/*/a*", authware)
|
|
|
|
const resources = require("./routes/resources.js")
|
|
|
|
const projects = require("./routes/projects.js")
|
|
|
|
const blog = require("./routes/blog.js")
|
2023-06-24 18:48:18 +03:00
|
|
|
const routes = [resources, projects, blog, auth]
|
2023-06-11 20:19:35 +03:00
|
|
|
|
2023-06-24 18:48:18 +03:00
|
|
|
routes.forEach((route) => {
|
2023-06-11 20:19:35 +03:00
|
|
|
app.use(route.path, route)
|
2023-04-16 20:12:24 +03:00
|
|
|
})
|
|
|
|
|
2023-06-24 18:48:18 +03:00
|
|
|
async function pexit() {
|
|
|
|
await db.close()
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on("SIGTERM", pexit)
|
|
|
|
process.on("SIGINT", pexit)
|
2023-04-16 20:12:24 +03:00
|
|
|
|
|
|
|
export default {
|
|
|
|
path: "/api",
|
2023-06-24 18:48:18 +03:00
|
|
|
handler: app
|
2023-06-11 20:19:35 +03:00
|
|
|
}
|