-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
143 lines (121 loc) · 3.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import env from "@/env";
import mongoose from "mongoose";
import express from "express";
import SHA512 from "crypto-js/sha512";
import { withModeDB } from "@/helper";
import cors from "cors";
import _authAPI, { _authAPILoose } from "@/middlewares/_authAPI";
import memberRegister from "@/controllers/users/member.register";
import mailVerify from "@/controllers/users/mail.verify";
import memberLogin from "@/controllers/users/member.login";
import resetPassword from "@/controllers/users/reset.password";
import renewToken from "@/controllers/auth/renew.token";
// paste imports
import createPaste from "@/controllers/pastes/create.default";
import readPaste from "@/controllers/pastes/read.default";
import updatePaste from "@/controllers/pastes/update.default";
import deletePaste from "@/controllers/pastes/delete.default";
import PasteModel from "@/models/paste.model";
const app = express();
app.use(express.json({ limit: "0.5mb" }));
app.use(
cors({
origin: "*",
})
);
app.use(async (req, res, next) => {
const body = req.body as any;
const testkey = req.headers["x-test-key"] || "";
const isTestHeaderOK = SHA512(testkey).toString() === env.X_TEST_KEY;
req.locals = {
hash: body?.options?.hash === true,
compress: body?.options?.compress === true,
encrypt: body?.options?.encrypt === true,
decrypt: body?.options?.decrypt === true,
owner: "default",
role: "member",
user: {},
// checks
checks: {
testkey: isTestHeaderOK,
},
headers: {
testkey: testkey,
},
};
return next();
});
app.post("/auth/register", memberRegister);
app.post("/auth/mailverify", mailVerify);
app.post("/auth/login", memberLogin);
app.post("/auth/reset", resetPassword);
app.post("/auth/renewtoken", renewToken);
// working of loose auth
// if present and not token not valid then it will allow the request
// if not present then it will allow the request
app.post("/paste", _authAPILoose, createPaste);
app.get("/paste", _authAPILoose, readPaste);
app.patch("/paste", _authAPILoose, updatePaste);
app.delete("/paste", _authAPILoose, deletePaste);
app.get("/paste/tagcheck", async (req, res) => {
const tag = req.query.tag as string;
if (!tag) {
return res.status(400).send({
msg: "tag is required",
status: "failed"
});
}
if (tag.length > 20) {
return res.status(400).send({
msg: "tag is too long",
status: "failed"
});
}
try {
const isAvailable = await PasteModel.exists({
_id: tag,
});
if (!isAvailable) {
return res.status(200).send({
msg: "tag is available",
available: true,
});
} else {
return res.status(200).send({
msg: "tag is not available",
available: false,
});
}
} catch (error) {
return res.status(500).send({
error: error.message,
msg: "internal server error",
});
}
});
app.use((req, res) => {
return res.status(404).send({
msg: "no route found",
});
});
let cachedDB;
async function connectToDatabase(uri) {
if (!cachedDB) {
cachedDB = await mongoose.connect(uri, {
serverSelectionTimeoutMS: 5000,
retryWrites: true,
});
}
}
app.listen(process.env.PORT || 2000, async () => {
console.log("db url -->",
withModeDB(env.DB_URL)
);
await connectToDatabase(
withModeDB(env.DB_URL)
);
console.log({
msg: "paste api is live",
status: "success",
})
})