mirror of
https://github.com/okiba-org/backend.git
synced 2025-07-24 07:47:25 +00:00
feat: set paste route
This commit is contained in:
21
src/db/bin.ts
Normal file
21
src/db/bin.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { Pool, QueryResult } from "pg";
|
||||
import { Word } from "../utils/types";
|
||||
import { Query } from "./queries";
|
||||
|
||||
// query wrappers
|
||||
export const getAvailableWord = async (db: Pool): Promise<Word | undefined> => {
|
||||
const data: void | QueryResult<Word> = await db
|
||||
.query(Query.getAvailableWord)
|
||||
.catch((err) => err);
|
||||
|
||||
if (data != undefined) {
|
||||
let word = data.rows[0];
|
||||
return word;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export const setWordTaken = async (db: Pool, id: number) => {
|
||||
await db.query(Query.setWordTaken(id)).catch((err) => err);
|
||||
};
|
9
src/db/queries.ts
Normal file
9
src/db/queries.ts
Normal file
@ -0,0 +1,9 @@
|
||||
// query strings
|
||||
export const Query = {
|
||||
getAvailableWord:
|
||||
"SELECT * FROM words WHERE taken = 'f' ORDER BY random() LIMIT 1;",
|
||||
|
||||
setWordTaken(id: number) {
|
||||
return `UPDATE words SET taken = 't' WHERE id = ${id}`;
|
||||
},
|
||||
};
|
26
src/main.ts
26
src/main.ts
@ -1,15 +1,37 @@
|
||||
import express, { Response, Request, Express } from "express";
|
||||
import dotenv from "dotenv";
|
||||
import BinRouter from "./routes/bin";
|
||||
import { Client, Pool } from "pg";
|
||||
import { createDataDir, errorHandler, logError, logSuccess } from "./utils";
|
||||
|
||||
dotenv.config();
|
||||
const app: Express = express();
|
||||
createDataDir();
|
||||
|
||||
// init db
|
||||
const pool = new Pool();
|
||||
const client = new Client();
|
||||
|
||||
client
|
||||
.connect()
|
||||
.then(() => {
|
||||
logSuccess("Connected to database!");
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
logError(`Could not connect to database! \n\n${err.message}`);
|
||||
});
|
||||
|
||||
// start server
|
||||
const app: Express = express();
|
||||
const port = process.env.PORT;
|
||||
|
||||
app.use(express.text());
|
||||
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
res.send("Hello, World!");
|
||||
});
|
||||
app.use("/bin/", BinRouter(pool));
|
||||
|
||||
app.use(errorHandler);
|
||||
app.listen(port, () => {
|
||||
console.log(`Server started on port ${port}`);
|
||||
logSuccess(`Server started on port ${port}`);
|
||||
});
|
||||
|
36
src/routes/bin.ts
Normal file
36
src/routes/bin.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import { Pool, QueryResult } from "pg";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { Word } from "../utils/types";
|
||||
import { projectRoot } from "../utils";
|
||||
import { getAvailableWord, setWordTaken } from "../db/bin";
|
||||
|
||||
export default function BinRouter(db: Pool) {
|
||||
const router: Router = Router();
|
||||
|
||||
router.post("/paste", async (req: Request, res: Response) => {
|
||||
let body: string = req.body;
|
||||
|
||||
const word: Word | undefined = await getAvailableWord(db);
|
||||
|
||||
if (word != undefined) {
|
||||
await fs.writeFile(
|
||||
path.join(projectRoot, "data", word.val + ".txt"),
|
||||
body,
|
||||
(err) => err
|
||||
);
|
||||
|
||||
setWordTaken(db, word.id);
|
||||
console.log(word.id);
|
||||
return res.status(200).json({
|
||||
endpoint: word.val,
|
||||
message: "Code pasted successfully!",
|
||||
});
|
||||
}
|
||||
|
||||
res.status(500).json({ message: "Something went wrong" });
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
24
src/utils/index.ts
Normal file
24
src/utils/index.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import chalk from "chalk";
|
||||
import { ErrorRequestHandler } from "express";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
// logs
|
||||
export const logError = (msg: string) => console.error(chalk.bold.red(msg));
|
||||
export const logSuccess = (msg: string) => console.log(chalk.bold.green(msg));
|
||||
|
||||
export const projectRoot = path.join(__dirname, "..", "..");
|
||||
|
||||
export const errorHandler: ErrorRequestHandler = (err, _, res, __) => {
|
||||
logError(err);
|
||||
|
||||
return res.status(500).json({ message: "Internal server error!" });
|
||||
};
|
||||
|
||||
export const createDataDir = () => {
|
||||
let dir = path.join(projectRoot, "data");
|
||||
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
};
|
5
src/utils/types.ts
Normal file
5
src/utils/types.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export type Word = {
|
||||
id: number;
|
||||
val: string;
|
||||
taken: boolean;
|
||||
};
|
Reference in New Issue
Block a user