new routes done + fixed blog

This commit is contained in:
ngn
2023-06-11 20:19:35 +03:00
parent aa7c0a1f08
commit 8abe9cd20c
17 changed files with 353 additions and 396 deletions

View File

@ -1,248 +1,43 @@
const express = require("express")
const {gimmeToken} = require("./util.js")
const { MongoClient } = require("mongodb")
require("dotenv").config()
// routes
/*
* error: 0 -> no error
* error: 1 -> parameter error
* error: 2 -> auth error
* error: 3 -> not found error
*/
const resources = require("./routes/resources.js")
const projects = require("./routes/projects.js")
const blog = require("./routes/blog.js")
const auth = require("./routes/auth.js")
const db = new MongoClient(process.env.DATABASE);
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }));
app.use("/resources", resources)
app.use("/projects", projects)
app.use("/blog", blog)
app.use("/auth", auth)
const db = new MongoClient(process.env.DATABASE);
const PASS = process.env.PASS
let TOKEN = gimmeToken();
app.use((req, res, next) => {
req.db = db;
next();
app.use((req,res,next)=>{
req.db = db
next()
})
// PATH: /api/login
// METHOD: GET
// PARAMETERS: pass
app.get("/login", (req,res)=>{
let pass = req.query.pass;
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,
]
if (pass === undefined)
return res.json({error: 1})
if (pass !== PASS)
return res.json({error: 2})
res.json({error: 0, token:TOKEN})
routes.forEach(route=>{
app.use(route.path, route)
})
// PATH: /api/logout
// METHOD: GET
// PARAMETERS: token
app.get("/logout", (req,res)=>{
let token = req.query.token;
if (token === undefined)
return res.json({error: 1})
if (token !== TOKEN)
return res.json({error: 2})
TOKEN = gimmeToken()
res.json({error: 0})
})
// PATH: /api/add_project
// METHOD: GET
// PARAMETERS: token, name, desc, url
app.get("/add_project", async (req, res) => {
let token = req.query.token;
let name = req.query.name;
let desc = req.query.desc;
let url = req.query.url;
if (
token === undefined ||
name === undefined ||
desc === undefined ||
url === undefined
)
return res.json({error: 1})
if (token !== TOKEN)
return res.json({error: 2})
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("projects")
await col.insertOne({"name":name, "desc":desc, "url":url, "click":0})
await client.close()
res.json({error: 0})
});
// PATH: /api/add_resource
// METHOD: GET
// PARAMETERS: token, name, tags, url
app.get("/add_resource", async (req, res) => {
let token = req.query.token;
let name = req.query.name;
let tags = req.query.tags;
let url = req.query.url;
if (
token === undefined ||
name === undefined ||
tags === undefined ||
url === undefined
)
return res.json({error: 1})
if (token !== TOKEN)
return res.json({error: 2})
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("resources")
await col.insertOne({"name":name, "tags":tags.split(","), "url":url})
await client.close()
res.json({error: 0})
});
// PATH: /api/get_projects
// METHOD: GET
// PARAMETERS: NONE
app.get("/get_projects", async (req, res) => {
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("projects")
const array = await col.find().toArray()
await client.close()
res.json({error: 0, projects:array})
});
// PATH: /api/get_resources
// METHOD: GET
// PARAMETERS: NONE
app.get("/get_resources", async (req, res) => {
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("resources")
const array = await col.find().toArray()
await client.close()
res.json({error: 0, resources:array})
});
// PATH: /api/get_resources
// METHOD: GET
// PARAMETERS: NONE
app.get("/get_resources", async (req, res) => {
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("resources")
const array = await col.find().toArray()
await client.close()
res.json({error: 0, resources:array})
});
// PATH: /api/add_post
// METHOD: POST
// PARAMETERS: token, title, author, content
app.post("/add_post", async (req, res) => {
let token = req.body.token;
let title = req.body.title;
let author = req.body.author;
let content = req.body.content;
if (
token === undefined ||
title === undefined ||
author === undefined ||
content === undefined
)
return res.json({error: 1})
if (token !== TOKEN)
return res.json({error: 2})
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("posts")
await col.insertOne({
"title":title,
"author":author,
"date": new Date().toLocaleDateString(),
"content":content
})
await client.close()
res.json({error: 0})
});
// PATH: /api/get_posts
// METHOD: POST
// PARAMETERS: NONE
app.get("/get_posts", async (req, res) => {
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("posts")
const array = await col.find().toArray()
await client.close()
let newarray = []
for(let i = 0;i<array.length;i++){
newarray.push({
"title":array[i]["title"],
"desc":array[i]["content"]["en"].substring(0, 140) + "...",
"info":`${array[i]["author"]} | ${array[i]["date"]}`
})
}
res.json({error: 0, posts:newarray})
});
// PATH: /api/get_post
// METHOD: POST
// PARAMETERS: id
app.get("/get_post", async (req, res) => {
let id = req.query.id;
await client.connect()
const db = await client.db("ngn13")
const col = await db.collection("posts")
const array = await col.find().toArray()
await client.close()
for(let i = 0;i<array.length;i++){
if(array[i]["title"].toLowerCase().replaceAll(" ", "")===id){
return res.json({error: 0, post:{
"title": array[i]["title"],
"info": `${array[i]["author"]} | ${array[i]["date"]}`,
"content": array[i]["content"],
}})
}
}
res.json({error: 3})
});
// PATH: /api/ping
// METHOD: GET
// PARAMETERS: NONE
app.get("/ping", (req, res) => {
res.send({ error: 0 });
});
export default {
path: "/api",
handler: app,
};
}

39
api/routes/auth.js Normal file
View File

@ -0,0 +1,39 @@
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(!token)
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(!pass)
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 }

93
api/routes/blog.js Normal file
View File

@ -0,0 +1,93 @@
const express = require("express")
const { makeID } = require("../util.js")
const blog = express.Router()
blog.path = "/blog"
blog.get("/sum", async (req,res)=>{
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("posts")
const results = await col.find({priv: {$eq: false}}).toArray()
await req.db.close()
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
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("posts")
const results = await col.find().toArray()
await req.db.close()
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)=>{
console.log("heyy")
const title = req.body.title
const author = req.body.author
const content = req.body.content
const priv = req.body.priv
console.log(title, author, content, priv)
if ( !title || !author || !content || !priv )
return res.json({ error: 1 })
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("posts")
await col.insertOne({
"title":title,
"author":author,
"date": new Date().toLocaleDateString(),
"content":content,
"priv": priv
})
await req.db.close()
res.json({ error: 0 })
})
module.exports = blog

35
api/routes/projects.js Normal file
View File

@ -0,0 +1,35 @@
const express = require("express")
const projects = express.Router()
projects.path = "/projects"
projects.get("/get", async (req,res)=>{
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("projects")
const results = await col.find().toArray()
await req.db.close()
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 (!name || !desc || !url )
return res.json({ error: 1 })
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("projects")
await col.insertOne({
"name":name,
"desc":desc,
"url":url,
"click":0
})
await req.db.close()
res.json({ error: 0 })
})
module.exports = projects

View File

@ -1,12 +1,34 @@
const express = require("express")
const resources = express.Router()
resources.path = "/resources"
resources.get("/get", async (req,res)=>{
await req.db.connect()
const col = await req.db.collection("ngn13")
const results = col.find().limit(10).toArray()
const db = await req.db.db("ngn13")
const col = await db.collection("resources")
let results = []
if(req.query.sum)
results = await col.find().limit(10).toArray()
else
results = await col.find().toArray()
await req.db.close()
res.json({ error: 0, resources: results })
})
resources.get("/add", async (req,res)=>{
let name = req.query.name;
let tags = req.query.tags;
let url = req.query.url;
if(!name || !tags || !url)
return res.json({"error":1})
await req.db.connect()
const db = await req.db.db("ngn13")
const col = await db.collection("resources")
await col.insertOne({"name":name, "tags":tags.split(","), "url":url})
await req.db.close()
res.json({error: 0})
})
module.exports = resources

View File

@ -8,5 +8,15 @@ function gimmeToken() {
return result;
}
module.exports = {gimmeToken}
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 }