mirror of
https://github.com/okiba-org/backend.git
synced 2025-04-20 09:26:56 +00:00
refactor: fix missing await and bad practices
This commit is contained in:
parent
e1c79a1840
commit
899ffa1799
5
.env
5
.env
@ -1,5 +0,0 @@
|
|||||||
PORT = 8080
|
|
||||||
PGUSER=alok-pg
|
|
||||||
PGHOST=localhost
|
|
||||||
PGDATABASE=okiba
|
|
||||||
PGPORT=5432
|
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
|||||||
|
.env
|
||||||
node_modules/
|
node_modules/
|
||||||
public/
|
public/
|
||||||
test/
|
test/
|
||||||
data/
|
data/
|
||||||
.env
|
|
@ -8,12 +8,7 @@ export const getAvailableWord = async (db: Pool): Promise<Word | undefined> => {
|
|||||||
.query(Query.getAvailableWord)
|
.query(Query.getAvailableWord)
|
||||||
.catch((err) => err);
|
.catch((err) => err);
|
||||||
|
|
||||||
if (data != undefined) {
|
return data?.rows[0];
|
||||||
let word = data.rows[0];
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setWordTaken = async (db: Pool, id: number) => {
|
export const setWordTaken = async (db: Pool, id: number) => {
|
||||||
|
37
src/main.ts
37
src/main.ts
@ -4,34 +4,39 @@ import BinRouter from "./routes/bin";
|
|||||||
import { Client, Pool } from "pg";
|
import { Client, Pool } from "pg";
|
||||||
import { createDataDir, errorHandler, logError, logSuccess } from "./utils";
|
import { createDataDir, errorHandler, logError, logSuccess } from "./utils";
|
||||||
|
|
||||||
dotenv.config();
|
const main = async () => {
|
||||||
createDataDir();
|
dotenv.config();
|
||||||
|
createDataDir();
|
||||||
|
|
||||||
// init db
|
// init db
|
||||||
const pool = new Pool();
|
const pool = await new Pool();
|
||||||
const client = new Client();
|
const client = await new Client();
|
||||||
|
|
||||||
client
|
client
|
||||||
.connect()
|
.connect()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
logSuccess("Connected to database!");
|
logSuccess("Connected to database!");
|
||||||
})
|
})
|
||||||
.catch((err: Error) => {
|
.catch((err: Error) => {
|
||||||
logError(`Could not connect to database! \n\n${err.message}`);
|
logError(`Could not connect to database! \n\n${err.message}`);
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
const app: Express = express();
|
const app: Express = express();
|
||||||
const port = process.env.PORT;
|
const port = process.env.PORT;
|
||||||
|
|
||||||
app.use(express.text());
|
app.use(express.text());
|
||||||
|
|
||||||
app.get("/", (req: Request, res: Response) => {
|
app.get("/", (req: Request, res: Response) => {
|
||||||
res.send("Hello, World!");
|
res.send("Hello, World!");
|
||||||
});
|
});
|
||||||
app.use("/bin/", BinRouter(pool));
|
app.use("/bin/", BinRouter(pool));
|
||||||
|
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
logSuccess(`Server started on port ${port}`);
|
logSuccess(`Server started on port ${port}`);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
|
@ -11,6 +11,9 @@ export default function BinRouter(db: Pool) {
|
|||||||
|
|
||||||
router.post("/paste", async (req: Request, res: Response) => {
|
router.post("/paste", async (req: Request, res: Response) => {
|
||||||
let body: string = req.body;
|
let body: string = req.body;
|
||||||
|
if (body == undefined || body == "") {
|
||||||
|
return res.status(400).json({ message: "Invalid body!" });
|
||||||
|
}
|
||||||
|
|
||||||
const word: Word | undefined = await getAvailableWord(db);
|
const word: Word | undefined = await getAvailableWord(db);
|
||||||
|
|
||||||
@ -21,8 +24,8 @@ export default function BinRouter(db: Pool) {
|
|||||||
(err) => err
|
(err) => err
|
||||||
);
|
);
|
||||||
|
|
||||||
setWordTaken(db, word.id);
|
await setWordTaken(db, word.id);
|
||||||
console.log(word.id);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
endpoint: word.val,
|
endpoint: word.val,
|
||||||
message: "Code pasted successfully!",
|
message: "Code pasted successfully!",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user