-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
84 lines (65 loc) · 2.3 KB
/
server.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
import * as express from "express";
import * as bodyParser from "body-parser";
import * as logger from 'morgan';
import { buildSchema } from "type-graphql";
// graphql resolvers
import { UserResolver } from "./src/graphql-resolver/userResolver";
import { SignResolver } from "./src/graphql-resolver/signResolver";
import { SocialResolver } from "./src/graphql-resolver/socialResolver";
import * as graphqlHTTP from "express-graphql";
import { createConnection, ConnectionOptions } from "typeorm";
import PassportCustom from './src/utility/passport';
import * as passport from 'passport';
import * as config from './config';
const port = process.env.PORT || config.Config.EXPRESS_PORT;
// Creates and configures an ExpressJS web server.
class App {
// ref to Express instance
public express: express.Application;
//Run configuration methods on the Express instance.
constructor() {
this.initDatabase();
this.express = express();
this.middleware();
this.setRouter();
}
private middleware(): void {
this.express.use(logger('dev'));
this.express.use(bodyParser.json());
// need to use passport.autenticate
this.express.use(bodyParser.urlencoded({ extended: false }));
this.express.use(bodyParser.text({ type: 'application/graphql' }));
this.express.listen(port);
PassportCustom.init(this.express);
}
private initDatabase() {
createConnection(config.Config.DB_CONNECTION);
}
private async setRouter() {
const schemaSign = await buildSchema({
resolvers: [SignResolver, SocialResolver]
});
this.express.use('/sign',
graphqlHTTP((req, res, next) => ({
schema: schemaSign,
graphiql: true,
req: req,
res: res,
next: next
})));
const schemaApi = await buildSchema({
resolvers: [UserResolver]
});
this.express.use('/api',
passport.authenticate('jwt',
{
session: false
}),
graphqlHTTP((req) => ({
schema: schemaApi,
graphiql: true,
context: req.user
})));
}
}
export default new App().express;