forked from aetherzcode/REST-API-NEW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (112 loc) · 3.42 KB
/
index.js
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
//―――――――――――――――――――――――――――――――――――――――――― ┏ Modules ┓ ―――――――――――――――――――――――――――――――――――――――――― \\
require("./settings");
const express = require("express");
const app = express();
const favicon = require("serve-favicon");
const path = require("path");
const cookieParser = require("cookie-parser");
const createError = require("http-errors");
const mongoose = require("mongoose");
const expressSession = require("express-session");
const MemoryStore = require("memorystore")(expressSession);
const passport = require("passport");
const flash = require("connect-flash");
const csrf = require("csurf");
const cron = require("node-cron");
const bodyParser = require("body-parser");
const User = require("./model/user");
const dataweb = require("./model/DataWeb");
//_______________________ ┏ Funtion ┓ _______________________\\
async function resetapi() {
await User.updateMany({}, { $set: { limitApikey: LimitApikey } });
console.log("RESET LIMIT DONE");
}
async function ResetRequestToday() {
await dataweb.updateOne(
{},
{
RequestToday: 0,
}
);
console.log("RESET Request Today DONE");
}
//_______________________ ┏ Code ┓ _______________________\\
(cors = require("cors")), (secure = require("ssl-express-www"));
app.use(favicon(path.join(__dirname, "public", "images", "akito.jpg")));
var main = require("./routes/main"),
api = require("./routes/api");
app.set("view engine", "ejs");
app.set("views", __dirname + "/view");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//_______________________ ┏ Connect Database ┓ _______________________\\
mongoose.set("strictQuery", false);
mongoose
.connect(keymongodb, { useNewUrlParser: true, useUnifiedTopology: true })
.then(async () => {
console.log("Connected !");
let limit = await dataweb.findOne();
if (limit === null) {
let obj = { RequestToday: 0 };
await dataweb.create(obj);
console.log("DATA WEBSITE Sussces Create");
}
});
//_______________________ ┏ CronJob For Reset Limit ┓ _______________________\\
// Reset Request Today Setiap sehari
cron.schedule(
"0 0 0 * * *",
() => {
ResetRequestToday();
},
{
scheduled: true,
timezone: "Asia/Jakarta",
}
);
//Reset All User Apikey Limit setiap sebulan
cron.schedule(
"0 0 1 * *",
() => {
resetapi();
},
{
scheduled: true,
timezone: "Asia/Jakarta",
}
);
//_______________________ ┏ Code ┓ _______________________\\
app.use(cookieParser("random"));
app.use(
expressSession({
secret: "random",
resave: true,
saveUninitialized: true,
maxAge: 24 * 60 * 60 * 1000,
store: new MemoryStore(),
})
);
app.use(csrf());
app.use(passport.initialize());
app.use(express.static("public"));
app.use(passport.session());
app.set("trust proxy", true);
app.set("json spaces", 2);
app.use(cors());
app.use(secure);
app.use(flash());
app.use(function (req, res, next) {
res.locals.success_messages = req.flash("success_messages");
res.locals.error_messages = req.flash("error_messages");
res.locals.error = req.flash("error");
next();
});
app.use("/", main);
app.use("/", api);
app.use(function (req, res, next) {
next(createError(404));
});
app.use(function (err, req, res, next) {
res.render("404");
});
module.exports = app;