-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
61 lines (53 loc) · 1.52 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
//importing dependencies
import express from 'express';
import dotenv from 'dotenv';
import { ApolloServer } from 'apollo-server-express';
import mongoose from 'mongoose';
import cloudinary from 'cloudinary';
//importing graphql utils
import { typeDefs } from './schema/typeDefs';
import { resolvers } from './schema/resolvers';
import { saveAndUpdateArticles } from './utils';
//init app
const app = express();
dotenv.config();
//env init
const PORT = process.env.PORT;
const password = process.env.MONGO_DB_PASSWORD;
const dbName = process.env.MONGO_DB_DATABASE_NAME;
//initialize cloudinary
cloudinary.v2.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
//init server
const server = new ApolloServer({
typeDefs,
resolvers,
});
// connection to mongodb
const dbURI = `mongodb+srv://achraf:${password}@newslydb.xihcp.mongodb.net/${dbName}?retryWrites=true&w=majority`;
mongoose
.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: false,
})
.then(() => {
app.listen(PORT || '4000', async () => {
const delay = 1800000;
//updating server every 15min
const callback = async () => {
await saveAndUpdateArticles();
setTimeout(callback, delay);
};
setTimeout(callback, delay);
console.log(
`Server running on port ${PORT}`,
`visit http://localhost:${PORT}/graphql`
);
});
});
//middlewares
server.applyMiddleware({ app });