-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.ts
89 lines (71 loc) · 2.06 KB
/
app.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
// express server
import express from "express";
// database
import mongoose from "mongoose";
// body parser for request payload
import bp = require("body-parser");
// handler imports
import {
UsersRouter
} from "./src/api/user/entrypoint";
// dotenv
const dotenv = require("dotenv");
dotenv.config();
// Func to call people daily
import { InfoAPICronFunc } from "./src/pkg/outbound/cron";
import { MongoRepo } from "./src/pkg/user/mongodb";
// initializing express server
const app : express.Application = express();
// request logging layer
const morgan = require("morgan");
app.use(morgan(process.env.LOGGING_FMT));
// handling json request payload
app.use(bp.json());
// health route
app.get("/ping", (req, res, next) => {
return res.send({message: "Live!"}).status(200);
})
// application endpoint handlers
app.use(`${process.env.API_VERSION}/user`, UsersRouter);
// mongoDB connection promise
mongoose.connect(<string>process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}).then(() => {
console.log("Connected to MongoDB")
}).catch((err: any) => {
console.error(err);
process.exit(1);
})
// not found handler
app.get("/*", (req: express.Request, res: express.Response, next: express.NextFunction) => {
return res.json({message: "Hello world"})
})
// error handler
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
return res.json({message: "Error occurred", error_log: err.message}).status(500);
})
// server hosting section
let port = process.env.PORT || 3000;
app.listen(port, () => {
console.info(
`Serving on port ${port}`
);
let userRepo = new MongoRepo();
setInterval(async () => {
console.log("Calling subscribers");
userRepo.ShowAllPhoneNumbers(0, 0)
.then(nums => {
let numArray = [];
for(let n of nums) {
numArray.push(n.phoneNumber)
}
InfoAPICronFunc(numArray)
.then(console.log)
.catch(console.error)
})
.catch(console.error)
}, 1000 * 60 * 60 * 24);
});