From a19ce4aebaf1917057212d03abc7a02ff168c8ba Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 20 Oct 2023 22:28:00 +0200 Subject: [PATCH] refactor: prisma and file based router --- .prettierrc | 2 +- Dockerfile | 5 +- docs/swagger.json | 44 ----------------- index.ts | 69 +++++++-------------------- package.json | 7 +-- prisma/schema.prisma | 24 ++++++++++ public/index.html | 3 +- routes/misc/download.ts | 5 +- routes/sern/deleteTime.ts | 39 ++++++++------- routes/sern/getTime.ts | 25 +++++----- routes/sern/newTime.ts | 37 +++++++------- routes/transcriptor/getTranscript.ts | 21 ++++---- routes/transcriptor/saveTranscript.ts | 21 ++++---- schemas/sern-time.ts | 7 --- schemas/transcripts.ts | 9 ---- yarn.lock | 51 ++++++++++++-------- 16 files changed, 155 insertions(+), 214 deletions(-) delete mode 100644 docs/swagger.json create mode 100644 prisma/schema.prisma delete mode 100644 schemas/sern-time.ts delete mode 100644 schemas/transcripts.ts diff --git a/.prettierrc b/.prettierrc index 89e3875..803df73 100644 --- a/.prettierrc +++ b/.prettierrc @@ -2,7 +2,7 @@ "trailingComma": "es5", "useTabs": true, "semi": false, - "singleQuote": false, + "singleQuote": true, "quoteProps": "preserve", "tabWidth": 4 } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1e62282..11a85ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,10 +15,7 @@ FROM node:lts-alpine AS final WORKDIR /app COPY --from=build /app/dist ./dist -COPY --from=build /app/schemas ./schemas -COPY --from=build /app/util ./util -COPY --from=build /app/routes ./routes -COPY --from=build /app/docs ./docs +COPY --from=build /app/prisma ./prisma COPY --from=build /app/public ./public COPY --from=build /app/node_modules ./node_modules COPY --from=build /app/package.json ./package.json diff --git a/docs/swagger.json b/docs/swagger.json deleted file mode 100644 index 04b34f2..0000000 --- a/docs/swagger.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "swagger": "2.0", - "info": { - "title": "Sr Izan API", - "description": "Image recognition stuff for now lol", - "version": "1.0.0" - }, - "host": "api.srizan.dev", - "basePath": "/", - "schemes": [ - "https" - ], - "paths": { - "/misc/download": { - "get": { - "summary": "Download images from a URL", - "parameters": [ - { - "in": "query", - "name": "url", - "type": "string", - "required": true, - "description": "The URL (https and stuff)" - }, - { - "in": "query", - "name": "type", - "type": "string", - "required": true, - "description": "The file type (png, jpg, jpeg or gif)" - } - ], - "responses": { - "200": { - "description": "Success" - }, - "400": { - "description": "Bad Request" - } - } - } - } - } -} \ No newline at end of file diff --git a/index.ts b/index.ts index 4a876c0..aeffca8 100644 --- a/index.ts +++ b/index.ts @@ -1,17 +1,16 @@ -import mongoose from "mongoose" import express from "express" import 'dotenv/config' -import bodyParser from "body-parser" import rateLimit from "express-rate-limit" import { consolelogTime } from "./util/consolelogTime.js" -import swagger from './docs/swagger.json' assert { type: 'json' } -import swaggerUI from 'swagger-ui-express' -import cors from 'cors' +import { router } from "express-file-routing" +import path from 'node:path' +import { fileURLToPath } from 'node:url'; +import { PrismaClient } from "@prisma/client" -/* Mongoose */ -await mongoose.connect(`${process.env.MONGODB}`).then(() => { - consolelogTime(`Connected to MongoDB!`) -}) +const dirname = path.dirname(fileURLToPath(import.meta.url)); + +/* MongoDB */ +export const prisma = new PrismaClient() /* Express configuration */ const app = express() @@ -21,49 +20,15 @@ const limiter = rateLimit({ message: { success: false, reason: "you just got ratelimited", error: "You just got ratelimited." }, standardHeaders: true, }) -app.use(bodyParser.json()) -app.use('/docs', swaggerUI.serve, swaggerUI.setup(swagger)); +app.use(express.json()) app.use(express.static('public')) +app.use(limiter) app.disable("x-powered-by") -/* All route imports */ -import newTime from "./routes/sern/newTime.js" -import getTime from "./routes/sern/getTime.js" -import deleteTime from "./routes/sern/deleteTime.js" -import download from "./routes/misc/download.js" -import saveTranscript from "./routes/transcriptor/saveTranscript.js" -import getTranscript from "./routes/transcriptor/getTranscript.js" - -app.use("/sern/newTime", limiter) -app.post("/sern/newTime", async (req, res) => { - newTime(req, res) -}) - -app.use("/sern/getTime", limiter) -app.get("/sern/getTime", async (req, res) => { - getTime(req, res) -}) - -app.use("/sern/newTime", limiter) -app.delete("/sern/deleteTime", async (req, res) => { - deleteTime(req, res) -}) - -app.use("/misc/download", limiter) -app.get("/misc/download", async (req, res) => { - download(req, res) -}) - -app.use("/transcriptor/save", limiter) -app.post("/transcriptor/save", (req, res) => { - saveTranscript(req, res) -}) - -app.use("/transcriptor/get", limiter) -app.get("/transcriptor/get", cors(), (req, res) => { - getTranscript(req, res) -}) - -app.listen(7272, () => { - consolelogTime(`Listening`) -}) +router({ directory: path.join(dirname, '/routes') }).then((r) => { + app.use("/", r); + + app.listen(7272, () => { + consolelogTime(`Listening`) + }) +}); diff --git a/package.json b/package.json index 2fe8736..26fd804 100644 --- a/package.json +++ b/package.json @@ -20,18 +20,19 @@ }, "homepage": "https://github.com/SrIzan10/api#readme", "dependencies": { + "@prisma/client": "^5.4.2", "axios": "^1.2.5", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.1", + "express-file-routing": "^3.0.3", "express-rate-limit": "^6.6.0", - "mongoose": "^6.6.5", - "swagger-ui-express": "^4.6.0" + "mongoose": "^6.6.5" }, "devDependencies": { "@types/cors": "^2.8.13", "@types/express": "^4.17.14", - "@types/swagger-ui-express": "^4.1.3", + "prisma": "^5.4.2", "typescript": "^4.8.4" } } diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..cd495ff --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,24 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "mongodb" + url = env("MONGODB") +} + +model sern_timezones { + id String @id @default(auto()) @map("_id") @db.ObjectId + v Int @default(0) @map("__v") @ignore + timezone String + userid String @unique +} + +model transcripts { + id String @id @default(auto()) @map("_id") @db.ObjectId + v Int @default(0) @map("__v") @ignore + guild String + msgid String @unique + text String + username String +} diff --git a/public/index.html b/public/index.html index 3ccf60a..6a19117 100644 --- a/public/index.html +++ b/public/index.html @@ -7,7 +7,6 @@
wow an api