-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (47 loc) · 1.33 KB
/
main.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
const express = require("express");
const { Op } = require("sequelize");
const cron = require("node-cron");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const ShortURL = require("./source/models/ShortURLModel");
require("dotenv").config();
const app = express();
const publicUrl = process.env.PUBLIC_URL || "http://localhost:3000";
const host = process.env.HOST || "localhost";
const port = process.env.PORT || 3000;
app.use(express.json());
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerJsdoc({
definition: {
openapi: "3.0.0",
info: {
title: "Short URL Service",
version: "1.0.0",
description: "A simple URL shortener service"
},
servers: [
{
url: publicUrl
}
],
},
apis: ["./source/routes/*.js"],
})));
app.use("/", require("./source/routes/index"));
// Scheduled task to delete expired URLs daily at midnight
cron.schedule("0 0 * * *", async () => {
try {
const result = await ShortURL.destroy({
where: {
expires_at: {
[Op.lt]: new Date()
}
}
});
console.log(`Deleted ${result} expired URLs`);
} catch (error) {
console.error("Failed to delete expired URLs:", error);
}
});
app.listen(port, () => {
console.log(`Server is running on http://${host}:${port}`);
});