diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..5c0d346 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +.nuxt diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..9954b43 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "none", + "tabWidth": 2, + "semi": false, + "singleQuote": false +} diff --git a/README.md b/README.md index e9d0a9b..d285246 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # My Website - [ngn13.fun](https://ngn13.fun) + This repo contains the source code of my personal website. It's written NuxtJS and supports full SSR. As database, it uses mongodb. ## Setup + ``` git clone https://github.com/ngn13/ngn13.fun.git cd ngn13.fun && npm i diff --git a/api/index.js b/api/index.js index 6d88611..045c444 100644 --- a/api/index.js +++ b/api/index.js @@ -7,15 +7,16 @@ require("dotenv").config() * error: 1 -> parameter error * error: 2 -> auth error * error: 3 -> not found error -*/ + */ -const db = new MongoClient(process.env.DATABASE); +const db = new MongoClient(process.env.DATABASE) const app = express() app.use(express.json()) -app.use(express.urlencoded({ extended: false })); -app.use((req,res,next)=>{ - req.db = db - next() +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") @@ -25,19 +26,21 @@ 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, -] +const routes = [resources, projects, blog, auth] -routes.forEach(route=>{ +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, + handler: app } diff --git a/api/routes/auth.js b/api/routes/auth.js index 1304807..112245d 100644 --- a/api/routes/auth.js +++ b/api/routes/auth.js @@ -4,34 +4,30 @@ const auth = express.Router() auth.path = "/auth" const PASS = process.env.PASS -let TOKEN = gimmeToken(); +let TOKEN = gimmeToken() -function authware(req,res,next){ +function authware(req, res, next) { const token = req.query.token ? req.query.token : req.body.token - if(!token) - return res.json({ error: 1 }) + if (typeof token !== "string") return res.json({ error: 1 }) - if(token!==TOKEN) - return res.json({ error: 2 }) + if (token !== TOKEN) return res.json({ error: 2 }) next() } auth.use("/logout", authware) -auth.get("/login", async (req,res)=>{ +auth.get("/login", async (req, res) => { const pass = req.query.pass - if(!pass) - return res.json({ error: 1 }) + if (typeof pass !== "string") return res.json({ error: 1 }) - if(pass!==PASS) - return res.json({ error: 2 }) + if (pass !== PASS) return res.json({ error: 2 }) res.json({ error: 0, token: TOKEN }) }) -auth.get("/logout", async (req,res)=>{ +auth.get("/logout", async (req, res) => { TOKEN = gimmeToken() res.json({ error: 0 }) }) diff --git a/api/routes/blog.js b/api/routes/blog.js index 10363bd..9ca056a 100644 --- a/api/routes/blog.js +++ b/api/routes/blog.js @@ -3,25 +3,23 @@ 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() +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{ res.json({ error: 0, posts: posts.reverse() }) }) -blog.get("/get", async (req,res)=>{ +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 db = req.db.db("ngn13") + const col = db.collection("posts") const results = await col.find().toArray() - await req.db.close() - for(let i = 0;i 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"], - } + 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") +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 - console.log(title, author, content, priv) - if ( !title || !author || !content || !priv ) + if ( + typeof title !== "string" || + typeof author !== "string" || + typeof content !== "string" || + typeof priv !== "string" + ) return res.json({ error: 1 }) - await req.db.connect() - const db = await req.db.db("ngn13") - const col = await db.collection("posts") + 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 + title: title, + author: author, + date: new Date().toLocaleDateString(), + content: content, + priv: priv }) - await req.db.close() res.json({ error: 0 }) }) diff --git a/api/routes/projects.js b/api/routes/projects.js index 9ea5140..8232f67 100644 --- a/api/routes/projects.js +++ b/api/routes/projects.js @@ -2,33 +2,33 @@ 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") +projects.get("/get", async (req, res) => { + const db = req.db.db("ngn13") + const col = 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; +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 ) + if ( + typeof name !== "string" || + typeof desc !== "string" || + typeof url !== "string" + ) return res.json({ error: 1 }) - await req.db.connect() - const db = await req.db.db("ngn13") - const col = await db.collection("projects") + const db = req.db.db("ngn13") + const col = db.collection("projects") await col.insertOne({ - "name":name, - "desc":desc, - "url":url, - "click":0 + name: name, + desc: desc, + url: url, + click: 0 }) - await req.db.close() res.json({ error: 0 }) }) diff --git a/api/routes/resources.js b/api/routes/resources.js index 06173df..a571b11 100644 --- a/api/routes/resources.js +++ b/api/routes/resources.js @@ -2,33 +2,31 @@ const express = require("express") const resources = express.Router() resources.path = "/resources" -resources.get("/get", async (req,res)=>{ - await req.db.connect() - const db = await req.db.db("ngn13") - const col = await db.collection("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() - await req.db.close() - res.json({ error: 0, resources: 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; +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}) + if ( + typeof name !== "string" || + typeof tags !== "string" || + typeof url !== "string" + ) + 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}) + 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 diff --git a/api/util.js b/api/util.js index 878748b..f59707b 100644 --- a/api/util.js +++ b/api/util.js @@ -1,14 +1,15 @@ 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; + 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){ +function makeID(title) { // this is used in blog.js // id is basically the title of the post // but ve remove the whitespace @@ -19,4 +20,3 @@ function makeID(title){ } module.exports = { gimmeToken, makeID } - diff --git a/components/Button.vue b/components/Button.vue index f3caafe..5b8f585 100644 --- a/components/Button.vue +++ b/components/Button.vue @@ -1,29 +1,29 @@ \ No newline at end of file + diff --git a/components/Card.vue b/components/Card.vue index 4eba457..a5f5cfc 100644 --- a/components/Card.vue +++ b/components/Card.vue @@ -1,56 +1,54 @@ \ No newline at end of file + diff --git a/components/Header.vue b/components/Header.vue index 3c40094..9856422 100644 --- a/components/Header.vue +++ b/components/Header.vue @@ -1,31 +1,30 @@ \ No newline at end of file + diff --git a/components/Input.vue b/components/Input.vue index 12dfe3c..1b88429 100644 --- a/components/Input.vue +++ b/components/Input.vue @@ -1,27 +1,27 @@ diff --git a/components/Logout.vue b/components/Logout.vue index d3d2682..0515e8c 100644 --- a/components/Logout.vue +++ b/components/Logout.vue @@ -1,41 +1,41 @@ diff --git a/components/Navbar.vue b/components/Navbar.vue index 0afb6ed..5b28531 100644 --- a/components/Navbar.vue +++ b/components/Navbar.vue @@ -1,53 +1,55 @@ \ No newline at end of file + diff --git a/components/NavbarLink.vue b/components/NavbarLink.vue index a4bc77c..eaddee5 100644 --- a/components/NavbarLink.vue +++ b/components/NavbarLink.vue @@ -1,60 +1,60 @@ \ No newline at end of file + diff --git a/components/NewPost.vue b/components/NewPost.vue index 8907ac6..99b0d75 100644 --- a/components/NewPost.vue +++ b/components/NewPost.vue @@ -1,57 +1,72 @@ diff --git a/components/NewProject.vue b/components/NewProject.vue index b9e3458..bf9256e 100644 --- a/components/NewProject.vue +++ b/components/NewProject.vue @@ -1,51 +1,67 @@ diff --git a/components/NewResource.vue b/components/NewResource.vue index 59bdc47..b0b94fd 100644 --- a/components/NewResource.vue +++ b/components/NewResource.vue @@ -1,51 +1,67 @@ diff --git a/components/Post.vue b/components/Post.vue deleted file mode 100644 index e69de29..0000000 diff --git a/components/PostPreview.vue b/components/PostPreview.vue index 82840a8..2b15b95 100644 --- a/components/PostPreview.vue +++ b/components/PostPreview.vue @@ -1,42 +1,42 @@ \ No newline at end of file + diff --git a/components/Project.vue b/components/Project.vue index 1012e51..e5311f9 100644 --- a/components/Project.vue +++ b/components/Project.vue @@ -1,62 +1,62 @@ diff --git a/components/ProjectList.vue b/components/ProjectList.vue index b71107b..6c47ead 100644 --- a/components/ProjectList.vue +++ b/components/ProjectList.vue @@ -1,28 +1,26 @@ diff --git a/components/Resource.vue b/components/Resource.vue index b251845..df74ce8 100644 --- a/components/Resource.vue +++ b/components/Resource.vue @@ -1,66 +1,66 @@ \ No newline at end of file + diff --git a/components/Tag.vue b/components/Tag.vue index 280fce9..28cff69 100644 --- a/components/Tag.vue +++ b/components/Tag.vue @@ -1,24 +1,20 @@ \ No newline at end of file +p { + background: var(--dark-three); + color: white; + text-shadow: 1px 1px 2px white; + padding: 5px 10px 5px 10px; + font-size: 25px; + border-radius: 7px; + margin-top: 10px; + transition: 0.4s; +} + diff --git a/ecosystem.config.js b/ecosystem.config.js index ff4561c..ddffb4b 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -1,11 +1,11 @@ module.exports = { apps: [ { - name: 'My Website', - exec_mode: 'cluster', - instances: 'max', // Or a number of instances - script: './node_modules/nuxt/bin/nuxt.js', - args: 'start' + name: "My Website", + exec_mode: "cluster", + instances: "max", // Or a number of instances + script: "./node_modules/nuxt/bin/nuxt.js", + args: "start" } ] } diff --git a/layouts/error.vue b/layouts/error.vue new file mode 100644 index 0000000..ad45e27 --- /dev/null +++ b/layouts/error.vue @@ -0,0 +1,7 @@ + diff --git a/links/index.js b/links/index.js index 5428467..5b52c83 100644 --- a/links/index.js +++ b/links/index.js @@ -1,37 +1,46 @@ -const express = require("express"); -const { MongoClient } = require("mongodb"); +const express = require("express") +const { MongoClient } = require("mongodb") const { makeID } = require("../api/util.js") require("dotenv").config() -const app = express(); -app.use(express.json()); -app.use(express.urlencoded({ extended: false })); +const app = express() +app.use(express.json()) +app.use(express.urlencoded({ extended: false })) -const client = new MongoClient(process.env.DATABASE); +const client = new MongoClient(process.env.DATABASE) -app.get("/:id", async (req,res)=>{ +app.get("/:id", async (req, res) => { const id = req.params.id + if (typeof id !== "string") return res.redirect("/projects") + await client.connect() - const db = await client.db("ngn13") - const col = await db.collection("projects") + const db = client.db("ngn13") + const col = db.collection("projects") const projects = await col.find().toArray() - console.log(projects) - - for(let i=0; i -
- -
- -

{{ post.info }}

-
-
-
-
+
+ +
+ +

{{ post.info }}

+
+
+
+
diff --git a/pages/blog/index.vue b/pages/blog/index.vue index ed56b66..c62ccd7 100644 --- a/pages/blog/index.vue +++ b/pages/blog/index.vue @@ -1,89 +1,92 @@ diff --git a/pages/index.vue b/pages/index.vue index dea575f..7f7cce2 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,104 +1,109 @@ diff --git a/pages/login.vue b/pages/login.vue index 1bd9cbf..1d5ec93 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -1,51 +1,56 @@ diff --git a/pages/projects.vue b/pages/projects.vue index 6cbf450..4648f95 100644 --- a/pages/projects.vue +++ b/pages/projects.vue @@ -1,96 +1,105 @@ diff --git a/pages/resources.vue b/pages/resources.vue index a669bf3..aed579e 100644 --- a/pages/resources.vue +++ b/pages/resources.vue @@ -1,103 +1,108 @@