-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
85 lines (73 loc) · 2.75 KB
/
Copy pathindex.ts
File metadata and controls
85 lines (73 loc) · 2.75 KB
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
import dotenv from 'dotenv'
import express from 'express';
import mongoose from "mongoose";
import bot from "./bot";
import {isMonday, setDefaultOptions} from "date-fns";
import cron from "node-cron"
import {sendNotification} from "./services/notificationService";
import {getTime} from "./services/timeService";
import helmet from "helmet";
import rateLimiter from "./common/middleware/rateLimiter";
import {healthCheckRouter} from "./routes/healthCheck/healthCheckRouter";
import errorHandler from "./common/middleware/errorHandler";
import {telegramBotRouter} from "./routes/bot/telegramBotRouter";
import {authRouter} from "./routes/auth/authRouter";
import {goalRouter} from "./routes/goal/goalRouter";
import {auth} from "./common/middleware/auth";
import {chatRouter} from "./routes/chat/chatRouter";
import {countRouter} from "./routes/count/countRouter";
import {rankRouter} from "./routes/count/rankRouter";
import {statisticRouter} from "./routes/count/statisticRouter";
import {streakRouter} from "./routes/count/streakRouter";
dotenv.config()
setDefaultOptions({
weekStartsOn: 1,
})
const app = express()
const PORT = process.env.PORT || 4000
const MONGO_URL = process.env.MONGO_URL || '';
const TELEGRAM_TOKEN = process.env.TELEGRAM_TOKEN || '';
app.use(express.json())
app.use(express.urlencoded({ extended: true }));
// app.use(cors({ origin: env.CORS_ORIGIN, credentials: true }));
app.use(helmet());
app.use(rateLimiter);
// Routes
app.use("/api/health-check", healthCheckRouter);
app.use("/api/bot", telegramBotRouter);
app.use("/api/auth", authRouter);
app.use("/api/goal", auth, goalRouter)
app.use("/api/chat", chatRouter)
app.use("/api/count", auth, countRouter)
app.use("/api/rank", auth, rankRouter)
app.use("/api/statistic", auth, statisticRouter)
app.use("/api/streak", auth, streakRouter)
// Error handlers
app.use(errorHandler());
cron.schedule("0 0 * * *", async () => {
await sendNotification(3, "day", 1);
})
cron.schedule("0 0 * * 1", async () => {
await sendNotification(3, "week", isMonday(getTime()) ? 1 : 0, false);
})
cron.schedule("0 0 1 * *", async () => {
await sendNotification(3, "month", 1, false);
})
app.listen(PORT, async () => {
console.log(`Server is up and running on PORT ${PORT}`)
mongoose.connect(MONGO_URL, {}).then(() => {
if (process.env.NODE_ENV === "development") {
console.log(`Server is up and running on DEV to bot ${TELEGRAM_TOKEN.replace(/:(.+)/, "")}`)
bot.startPolling({
polling: {
params: {
allowed_updates: ["message", "callback_query"]
}
}
}).then()
}
bot.getMe().then((me) => {
console.log("Listening to bot @" + me.username)
})
})
})