-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
109 lines (94 loc) · 3.48 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
'use strict'
const path = require('path')
const fp = require('fastify-plugin')
const UserService = require('./user/service')
const TweetService = require('./tweet/service')
const FollowService = require('./follow/service')
const TimelineService = require('./timeline/service')
const swaggerOption = {
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
}
}
const schema = {
type: 'object',
required: [ 'MONGODB_URL', 'REDIS_URL', 'JWT_SECRET' ],
properties: {
MONGODB_URL: { type: 'string' },
REDIS_URL: { type: 'string' },
JWT_SECRET: { type: 'string' }
},
additionalProperties: false
}
async function connectToDatabases (fastify) {
fastify
// `fastify-mongodb` makes this connection and store the database instance into `fastify.mongo.db`
// See https://github.com/fastify/fastify-mongodb
.register(require('fastify-mongodb'), { url: fastify.config.MONGODB_URL, useNewUrlParser: true })
// `fastify-redis` makes this connection and store the database instance into `fastify.redis`
// See https://github.com/fastify/fastify-redis
.register(require('fastify-redis'), { url: fastify.config.REDIS_URL })
}
async function authenticator (fastify) {
fastify
// JWT is used to identify the user
// See https://github.com/fastify/fastify-jwt
.register(require('fastify-jwt'), {
secret: fastify.config.JWT_SECRET,
algorithms: ['RS256']
})
}
function transformStringIntoObjectId (str) {
return new this.mongo.ObjectId(str)
}
async function decorateFastifyInstance (fastify) {
const db = fastify.mongo.db
const userCollection = await db.createCollection('users')
const userService = new UserService(userCollection)
await userService.ensureIndexes(db)
fastify.decorate('userService', userService)
const tweetCollection = await db.createCollection('tweets')
const tweetService = new TweetService(tweetCollection)
await tweetService.ensureIndexes(db)
fastify.decorate('tweetService', tweetService)
const followService = new FollowService(fastify.redis)
fastify.decorate('followService', followService)
const timelineService = new TimelineService(followService, tweetService)
fastify.decorate('timelineService', timelineService)
fastify.decorate('authPreHandler', async function auth (request, reply) {
try {
await request.jwtVerify()
} catch (err) {
reply.send(err)
}
})
fastify.decorate('transformStringIntoObjectId', transformStringIntoObjectId)
}
module.exports = async function (fastify, opts) {
fastify
.register(require('fastify-swagger'), swaggerOption)
// fastify-env checks and coerces the environment variables and save the result in `fastify.config`
// See https://github.com/fastify/fastify-env
.register(require('fastify-env'), { schema, data: [ opts ] })
.register(fp(connectToDatabases))
.register(fp(authenticator))
.register(fp(decorateFastifyInstance))
// APIs modules
.register(require('./user'), { prefix: '/api/user' })
.register(require('./tweet'), { prefix: '/api/tweet' })
.register(require('./follow'), { prefix: '/api/follow' })
.register(require('./timeline'), { prefix: '/api/timeline' })
// Serving static files
.register(require('fastify-static'), {
root: path.join(__dirname, 'frontend', 'build'),
prefix: '/'
})
}