aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-10-23 21:34:48 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-10-23 21:34:48 +0300
commit747097af03e422c72c2fe0065637e59a1685d219 (patch)
tree6b965e4c5d4ee812d104c3537866f2e3b5e27376 /src
nearly completed, forgot to make a repo
Diffstat (limited to 'src')
-rw-r--r--src/database/postgres.ts74
-rw-r--r--src/files/SwaggerUI.yaml206
-rw-r--r--src/models/Thread.ts17
-rw-r--r--src/routes/board.ts44
-rw-r--r--src/server.ts26
5 files changed, 367 insertions, 0 deletions
diff --git a/src/database/postgres.ts b/src/database/postgres.ts
new file mode 100644
index 0000000..2ea19bd
--- /dev/null
+++ b/src/database/postgres.ts
@@ -0,0 +1,74 @@
+import { Client } from 'pg';
+var connectionString = "postgres://scale:secret@db/scale?sslmode=disable";
+let client: Client;
+
+export function InitPostgres() {
+ client = new Client(connectionString);
+ client.connect();
+}
+
+export function ClosePostgres() {
+ client.end();
+}
+
+export function CustomQuery(query: string, callback: (r: { status: number, data: any }) => void) {
+ client.query(query, (err, res) => {
+ let r: { status: number, data: any } = { status: 500, data: undefined };
+ if (err) {
+ r.data = err.message;
+ } else {
+ r.status = 200; r.data = res.rows;
+ }
+ callback(r);
+ });
+}
+
+export function GetBoards(callback: (r: { status: number, data: any }) => void) {
+ let query = `
+select s.nspname as board
+from pg_catalog.pg_namespace s
+where nspname != 'pg_toast' and nspname != 'pg_catalog'
+and nspname != 'public' and nspname != 'information_schema';`; // to remove the default schemas
+
+ client.query(query, (err, res) => {
+ let r: { status: number, data: any } = { status: 500, data: undefined };
+ if (err) {
+ r.status = 404; r.data = err.message;
+ } else {
+ r.status = 200; r.data = [];
+ for(let board of res.rows)
+ r.data.push(board.board);
+ }
+ callback(r);
+ });
+}
+
+export function CreateBoard(name: string, callback: (r: { status: number, data: any }) => void) {
+ let query = `
+create schema ${name};
+create table ${name}.threads(
+ id serial primary key,
+ timestamp bigint,
+ title text,
+ content text,
+ image text,
+ imagetype text
+);
+create table ${name}.comments(
+ id serial primary key,
+ timestamp bigint,
+ content text,
+ image text,
+ imagetype text
+);`;
+
+ client.query(query, (err, res) => {
+ let r: { status: number, data: any } = { status: 500, data: undefined };
+ if (err) {
+ r.data = err.message;
+ } else {
+ r.status = 200;
+ }
+ callback(r);
+ });
+}
diff --git a/src/files/SwaggerUI.yaml b/src/files/SwaggerUI.yaml
new file mode 100644
index 0000000..019ca5e
--- /dev/null
+++ b/src/files/SwaggerUI.yaml
@@ -0,0 +1,206 @@
+openapi: 3.0.0
+info:
+ title: Converted from Thread.yaml with typeconv
+ version: '1'
+ x-id: Thread.yaml
+ x-comment: >-
+ Generated from src/models/Thread.ts by core-types-json-schema
+ (https://github.com/grantila/core-types-json-schema) on behalf of typeconv
+ (https://github.com/grantila/typeconv)
+paths:
+ /custom:
+ post:
+ summary: Run a custom sql query
+ responses:
+ '200':
+ description: Success
+ '500':
+ description: Failed
+ requestBody:
+ descripton: The command to run
+ content:
+ text/plain:
+ schema:
+ title: Command
+ type: string
+ /boards:
+ get:
+ summary: Get boards
+ responses:
+ '200':
+ description: Success
+ content:
+ application/json:
+ schema:
+ type: array
+ items:
+ type: string
+ '404':
+ description: Not Found
+ /boards/{board}:
+ post:
+ summary: Creat a new board
+ parameters:
+ - name: board
+ in: path
+ required: true
+ schema:
+ type: string
+ responses:
+ '200':
+ description: Success
+ '500':
+ description: Failed
+ /board/{board}:
+ get:
+ summary: Get threads in board
+ parameters:
+ - name: board
+ in: path
+ required: true
+ schema:
+ type: string
+ responses:
+ '200':
+ description: Success
+ content:
+ application/json:
+ schema:
+ type: array
+ items:
+ $ref: '#/components/schemas/Thread'
+ '404':
+ description: Not Found
+ post:
+ summary: Post a thread
+ parameters:
+ - name: board
+ in: path
+ required: true
+ schema:
+ type: string
+ requestBody:
+ description: Thread to post
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Thread'
+ responses:
+ '200':
+ description: Success
+ '500':
+ description: Failed
+
+ /board/{board}/{tid}:
+ get:
+ summary: Get a thread
+ parameters:
+ - name: board
+ in: path
+ required: true
+ schema:
+ type: string
+ - name: tid
+ in: path
+ required: true
+ schema:
+ type: string
+ responses:
+ '200':
+ description: Success
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Thread'
+ '404':
+ description: Not Found
+ post:
+ summary: Post a comment
+ parameters:
+ - name: board
+ in: path
+ required: true
+ schema:
+ type: string
+ - name: tid
+ in: path
+ required: true
+ schema:
+ type: string
+ requestBody:
+ descriptors: The comment to post
+ content:
+ application/json:
+ schema:
+ $ref: '#components/schemas/Comment'
+ responses:
+ '200':
+ description: Success
+ '500':
+ description: Failed
+
+components:
+ schemas:
+ Comment:
+ properties:
+ id:
+ title: Comment.id
+ type: string
+ timestamp:
+ title: Comment.timestamp
+ type: number
+ content:
+ title: Comment.content
+ type: string
+ image:
+ title: Comment.image
+ type: string
+ imagetype:
+ title: Comment.imagetype
+ type: string
+ required:
+ - id
+ - timestamp
+ - content
+ - image
+ - imagetype
+ additionalProperties: false
+ title: Comment
+ type: object
+ Thread:
+ properties:
+ id:
+ title: Thread.id
+ type: string
+ timestamp:
+ title: Thread.timestamp
+ type: number
+ title:
+ title: Thread.title
+ type: string
+ content:
+ title: Thread.content
+ type: string
+ comments:
+ items:
+ $ref: '#/components/schemas/Comment'
+ title: Thread.comments.[]
+ title: Thread.comments
+ type: array
+ image:
+ title: Thread.image
+ type: string
+ imagetype:
+ title: Thread.imagetype
+ type: string
+ required:
+ - id
+ - title
+ - contnet
+ - comments
+ - timestamp
+ - image
+ - imageType
+ additionalProperties: false
+ title: Thread
+ type: object
diff --git a/src/models/Thread.ts b/src/models/Thread.ts
new file mode 100644
index 0000000..5960e11
--- /dev/null
+++ b/src/models/Thread.ts
@@ -0,0 +1,17 @@
+export type Comment = {
+ id: string;
+ timestamp: number;
+ content: string;
+ image: string;
+ imagetype: string //image or video
+};
+
+export type Thread = {
+ id: string;
+ timestamp: number;
+ title: string;
+ content: string;
+ comments: Comment[];
+ image: string;
+ imagetype: string; //image or video
+};
diff --git a/src/routes/board.ts b/src/routes/board.ts
new file mode 100644
index 0000000..caa2d53
--- /dev/null
+++ b/src/routes/board.ts
@@ -0,0 +1,44 @@
+import express from 'express';
+import type { Thread, Comment } from '../models/Thread';
+import { CreateBoard, CustomQuery, GetBoards } from '../database/postgres'
+
+const router = express.Router();
+
+router.post('/custom', (req, res) => {
+ CustomQuery(req.body, (r) => {
+ res.status(r.status).send(r.data);
+ });
+});
+
+router.get('/boards', (req, res) => {
+ GetBoards((r) => {
+ res.status(r.status).send(r.data);
+ });
+});
+
+router.post('/boards/:board', (req, res) => {
+ CreateBoard(req.params.board, (r) => {
+ res.status(r.status).send(r.data);
+ });
+});
+
+router.get('/board/:board', (req, res) => {
+ res.status(404).send(req.params.board);
+});
+
+router.post('/board/:board', (req, res) => {
+ let t: Thread = req.body;
+ console.log(t);
+ res.status(403).send({board: req.params.board, thread: t});
+});
+
+router.get('/board/:board/:tid', (req, res) => {
+ res.status(404).send([req.params.board, req.params.tid]);
+});
+
+router.post('/board/:board/:tid', (req, res) => {
+ let c: Comment = req.body;
+ res.status(404).send({params: [req.params.board, req.params.tid], body: c});
+});
+
+export default router;
diff --git a/src/server.ts b/src/server.ts
new file mode 100644
index 0000000..0368f99
--- /dev/null
+++ b/src/server.ts
@@ -0,0 +1,26 @@
+import express from 'express';
+import boardRouter from './routes/board';
+
+import swaggerUI from 'swagger-ui-express';
+import YAML from 'yamljs';
+const doc = YAML.load('src/files/SwaggerUI.yaml');
+
+import { InitPostgres, ClosePostgres } from './database/postgres';
+InitPostgres();
+
+const app = express();
+
+app.use(express.json());
+app.use(express.text());
+
+app.use('/swagger', swaggerUI.serve, swaggerUI.setup(doc));
+
+app.use('/', boardRouter);
+
+app.get('*', (req, res) => {
+ res.send("haha i ate your request");
+});
+
+app.listen(6969);
+
+// ClosePostgres();