initial sveltekit setup
This commit is contained in:
46
api/index.js
46
api/index.js
@ -1,46 +0,0 @@
|
||||
const express = require("express")
|
||||
const { MongoClient } = require("mongodb")
|
||||
require("dotenv").config()
|
||||
|
||||
/*
|
||||
* error: 0 -> no error
|
||||
* error: 1 -> parameter error
|
||||
* error: 2 -> auth error
|
||||
* error: 3 -> not found error
|
||||
*/
|
||||
|
||||
const db = new MongoClient(process.env.DATABASE)
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
app.use(express.urlencoded({ extended: false }))
|
||||
app.use(async (req, res, next) => {
|
||||
await db.connect()
|
||||
req.db = db
|
||||
next()
|
||||
})
|
||||
|
||||
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")
|
||||
const routes = [resources, projects, blog, auth]
|
||||
|
||||
routes.forEach((route) => {
|
||||
app.use(route.path, route)
|
||||
})
|
||||
|
||||
async function pexit() {
|
||||
await db.close()
|
||||
process.exit()
|
||||
}
|
||||
|
||||
process.on("SIGTERM", pexit)
|
||||
process.on("SIGINT", pexit)
|
||||
|
||||
export default {
|
||||
path: "/api",
|
||||
handler: app
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
const express = require("express")
|
||||
const { gimmeToken } = require("../util.js")
|
||||
const auth = express.Router()
|
||||
auth.path = "/auth"
|
||||
|
||||
const PASS = process.env.PASS
|
||||
let TOKEN = gimmeToken()
|
||||
|
||||
function authware(req, res, next) {
|
||||
const token = req.query.token ? req.query.token : req.body.token
|
||||
|
||||
if (typeof token !== "string") return res.json({ error: 1 })
|
||||
|
||||
if (token !== TOKEN) return res.json({ error: 2 })
|
||||
|
||||
next()
|
||||
}
|
||||
auth.use("/logout", authware)
|
||||
|
||||
auth.get("/login", async (req, res) => {
|
||||
const pass = req.query.pass
|
||||
|
||||
if (typeof pass !== "string") return res.json({ error: 1 })
|
||||
|
||||
if (pass !== PASS) return res.json({ error: 2 })
|
||||
|
||||
res.json({ error: 0, token: TOKEN })
|
||||
})
|
||||
|
||||
auth.get("/logout", async (req, res) => {
|
||||
TOKEN = gimmeToken()
|
||||
res.json({ error: 0 })
|
||||
})
|
||||
|
||||
module.exports = { auth, authware }
|
@ -1,87 +0,0 @@
|
||||
const express = require("express")
|
||||
const { makeID } = require("../util.js")
|
||||
const blog = express.Router()
|
||||
blog.path = "/blog"
|
||||
|
||||
blog.get("/sum", async (req, res) => {
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("posts")
|
||||
const results = await col.find({ priv: { $eq: false } }).toArray()
|
||||
|
||||
let posts = []
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
posts.push({
|
||||
title: results[i]["title"],
|
||||
desc:
|
||||
results[i]["content"]
|
||||
.substring(0, 140) // a short desc
|
||||
.replaceAll("#", "") // remove all the markdown stuff
|
||||
.replaceAll("*", "")
|
||||
.replaceAll("`", "")
|
||||
.replaceAll("-", "") + "...", // add "..." to make it look like desc
|
||||
info: `${results[i]["author"]} | ${results[i]["date"]}`
|
||||
})
|
||||
}
|
||||
|
||||
// reversing so we can get
|
||||
// the latest posts on the top
|
||||
res.json({ error: 0, posts: posts.reverse() })
|
||||
})
|
||||
|
||||
blog.get("/get", async (req, res) => {
|
||||
const id = req.query.id
|
||||
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("posts")
|
||||
const results = await col.find().toArray()
|
||||
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
// id is basically the title of the post
|
||||
// but ve remove the whitespace
|
||||
// and make it lowerspace
|
||||
// for example:
|
||||
// Online Privacy Guide -> onlineprivacyguide
|
||||
if (makeID(results[i]["title"]) === id) {
|
||||
return res.json({
|
||||
error: 0,
|
||||
post: {
|
||||
title: results[i]["title"],
|
||||
// info is the subtitle, for example:
|
||||
// ngn | 01/06/2023
|
||||
info: `${results[i]["author"]} | ${results[i]["date"]}`,
|
||||
content: results[i]["content"]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ error: 3 })
|
||||
})
|
||||
|
||||
blog.post("/add", async (req, res) => {
|
||||
const title = req.body.title
|
||||
const author = req.body.author
|
||||
const content = req.body.content
|
||||
const priv = req.body.priv
|
||||
|
||||
if (
|
||||
typeof title !== "string" ||
|
||||
typeof author !== "string" ||
|
||||
typeof content !== "string" ||
|
||||
typeof priv !== "boolean"
|
||||
)
|
||||
return res.json({ error: 1 })
|
||||
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("posts")
|
||||
await col.insertOne({
|
||||
title: title,
|
||||
author: author,
|
||||
date: new Date().toLocaleDateString(),
|
||||
content: content,
|
||||
priv: priv
|
||||
})
|
||||
res.json({ error: 0 })
|
||||
})
|
||||
|
||||
module.exports = blog
|
@ -1,35 +0,0 @@
|
||||
const express = require("express")
|
||||
const projects = express.Router()
|
||||
projects.path = "/projects"
|
||||
|
||||
projects.get("/get", async (req, res) => {
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("projects")
|
||||
const results = await col.find().toArray()
|
||||
res.json({ error: 0, projects: results })
|
||||
})
|
||||
|
||||
projects.get("/add", async (req, res) => {
|
||||
let name = req.query.name
|
||||
let desc = req.query.desc
|
||||
let url = req.query.url
|
||||
|
||||
if (
|
||||
typeof name !== "string" ||
|
||||
typeof desc !== "string" ||
|
||||
typeof url !== "string"
|
||||
)
|
||||
return res.json({ error: 1 })
|
||||
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("projects")
|
||||
await col.insertOne({
|
||||
name: name,
|
||||
desc: desc,
|
||||
url: url,
|
||||
click: 0
|
||||
})
|
||||
res.json({ error: 0 })
|
||||
})
|
||||
|
||||
module.exports = projects
|
@ -1,32 +0,0 @@
|
||||
const express = require("express")
|
||||
const resources = express.Router()
|
||||
resources.path = "/resources"
|
||||
|
||||
resources.get("/get", async (req, res) => {
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("resources")
|
||||
let results = []
|
||||
if (req.query.sum) results = await col.find().limit(10).toArray()
|
||||
else results = await col.find().toArray()
|
||||
res.json({ error: 0, resources: results.reverse() })
|
||||
})
|
||||
|
||||
resources.get("/add", async (req, res) => {
|
||||
let name = req.query.name
|
||||
let tags = req.query.tags
|
||||
let url = req.query.url
|
||||
|
||||
if (
|
||||
typeof name !== "string" ||
|
||||
typeof tags !== "string" ||
|
||||
typeof url !== "string"
|
||||
)
|
||||
return res.json({ error: 1 })
|
||||
|
||||
const db = req.db.db("ngn13")
|
||||
const col = db.collection("resources")
|
||||
await col.insertOne({ name: name, tags: tags.split(","), url: url })
|
||||
res.json({ error: 0 })
|
||||
})
|
||||
|
||||
module.exports = resources
|
22
api/util.js
22
api/util.js
@ -1,22 +0,0 @@
|
||||
function gimmeToken() {
|
||||
var result = ""
|
||||
var characters =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
var charactersLength = characters.length
|
||||
for (var i = 0; i < 32; i++) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function makeID(title) {
|
||||
// this is used in blog.js
|
||||
// id is basically the title of the post
|
||||
// but ve remove the whitespace
|
||||
// and make it lowerspace
|
||||
// for example:
|
||||
// Online Privacy Guide -> onlineprivacyguide
|
||||
return title.toLowerCase().replaceAll(" ", "")
|
||||
}
|
||||
|
||||
module.exports = { gimmeToken, makeID }
|
Reference in New Issue
Block a user