forked from globalfund/data-explorer-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.ts
121 lines (108 loc) · 3.73 KB
/
application.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {
RestExplorerBindings,
RestExplorerComponent,
} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
import 'dotenv/config';
import multer from 'multer';
import path from 'path';
import {DbDataSource} from './datasources';
import {
AuthenticationComponent,
registerAuthenticationStrategy,
} from '@loopback/authentication';
import {
JWTAuthenticationStrategy,
JWTServiceProvider,
KEY,
} from './authentication-strategies';
import {FILE_UPLOAD_SERVICE, STORAGE_DIRECTORY} from './keys';
import {MySequence} from './sequence';
import {mimeTypeToFileExtension} from './utils/mimeTypeToFileExtension';
export {ApplicationConfig};
export class ApiApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super(options);
this.component(AuthenticationComponent);
this.service(JWTServiceProvider);
// Register the Auth0 JWT authentication strategy
// @ts-ignore
registerAuthenticationStrategy(this, JWTAuthenticationStrategy);
this.configure(KEY).to({
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`,
audience: process.env.AUTH0_AUDIENCE,
issuer: `https://${process.env.AUTH0_DOMAIN}/`,
algorithms: ['RS256'],
});
// Set datasource based off environment
const dbHost = process.env.MONGO_HOST ?? 'localhost';
const dbPort = process.env.MONGO_PORT ?? 27017;
const dbUser = process.env.MONGO_USERNAME ?? '';
const dbPass = process.env.MONGO_PASSWORD ?? '';
const database = process.env.MONGO_DB ?? 'the-data-explorer-db';
const authSource = process.env.MONGO_AUTH_SOURCE ?? '';
this.bind('datasources.config.db').to({
name: 'db',
connector: 'mongodb',
url: '',
host: dbHost,
port: dbPort,
user: dbUser,
password: dbPass,
database: database,
authSource: authSource,
useNewUrlParser: true,
});
this.bind('datasources.db').toClass(DbDataSource);
// Set up the custom sequence
this.sequence(MySequence);
// Set up default home page
this.static('/', path.join(__dirname, '../public'));
// Customize @loopback/rest-explorer configuration here
this.configure(RestExplorerBindings.COMPONENT).to({
path: '/api-explorer',
indexTitle: 'The Data Explorer API',
});
this.component(RestExplorerComponent);
this.configureFileUpload(options.fileStorageDirectory);
this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
this.bootOptions = {
controllers: {
// Customize ControllerBooter Conventions here
dirs: ['controllers'],
extensions: ['.controller.js'],
nested: true,
},
};
}
/**
* Configure `multer` options for file upload
*/
protected configureFileUpload(destination?: string) {
// Upload files to `dist/.sandbox` by default
destination = destination ?? process.env.DX_BACKEND_DIR + 'staging/';
this.bind(STORAGE_DIRECTORY).to(destination);
const multerOptions: multer.Options = {
storage: multer.diskStorage({
destination,
// Use the original file name as is
filename: (req, file, cb) => {
const newName = `${file.fieldname}${mimeTypeToFileExtension(
file.mimetype,
)}`;
cb(null, newName);
},
}),
limits: {fileSize: 1024 * 1024 * 150},
};
// Configure the file upload service with multer options
this.configure(FILE_UPLOAD_SERVICE).to(multerOptions);
}
}