/
app.ts
180 lines (163 loc) · 6.18 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import config from './config';
import * as fs from 'fs';
import * as url from 'url';
import * as path from 'path';
import * as knex from 'knex';
import * as Koa from 'koa';
import * as bodyParser from 'koa-bodyparser';
import * as cors from 'kcors';
import * as send from 'koa-send';
import * as Router from 'koa-router';
import { Model, ValidationError as ObjectionValidationError } from 'objection';
Model.knex(knex(config.DATABASE));
import * as jwt from 'jsonwebtoken';
import * as yaml from 'yamljs';
// tslint:disable-next-line
const swaggerUIAbsolutePath = require('swagger-ui-dist').absolutePath();
import { authenticateUser, tokenRefresh } from './authentication/jwt/routes';
import { requireAuthentication } from './authentication/jwt/middleware';
import ProjectViewSet from './viewsets/ProjectViewSet';
import UserViewSet from './viewsets/UserViewSet';
import ClientViewSet from './viewsets/ClientViewSet';
import WebHookViewSet from './viewsets/WebHookViewSet';
import ProjectInviteViewSet from './viewsets/ProjectInviteViewSet';
import MediaViewSet from './viewsets/MediaViewSet';
import MediaTagViewSet from './viewsets/MediaTagViewSet';
import EntryTypeViewSet from './viewsets/EntryTypeViewSet';
import EntryViewSet from './viewsets/EntryViewSet';
import EntryTagViewSet from './viewsets/EntryTagViewSet';
import Client from './models/Client';
import Entry from './models/Entry';
import EntryTag from './models/EntryTag';
import EntryType from './models/EntryType';
import Media from './models/Media';
import MediaTag from './models/MediaTag';
import Permission from './models/Permission';
import Project from './models/Project';
import ProjectInvite from './models/ProjectInvite';
import ProjectMembership from './models/ProjectMembership';
import Role from './models/Role';
import User from './models/User';
import WebHook from './models/WebHook';
import NotFoundError from './errors/NotFoundError';
import ValidationError from './errors/ValidationError';
import AuthenticationError from './errors/AuthenticationError';
import WebHookMiddleware from './webhooks/middleware';
// Attach the storage backend to the viewSetOptions
const viewSetOptions = {
storage: config.STORAGE_BACKEND
};
// Instantiate root router and attach routes
const router = new Router();
router.post('/authenticate', authenticateUser);
router.post('/token-refresh', requireAuthentication, tokenRefresh);
router.use('/user/', new UserViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/', async (ctx: Koa.Context, next: () => Promise<any>) => {
const project = await Project.getById(ctx.params.projectId);
if (!project) throw new NotFoundError();
ctx.state.project = project;
await next();
});
router.use('/project/', new ProjectViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/web-hook/', new WebHookViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/invite/', new ProjectInviteViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/media/', new MediaViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/media-tag/', new MediaTagViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/entry-type/', new EntryTypeViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/entry-tag/', new EntryTagViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/entry/', new EntryViewSet(viewSetOptions).routes());
router.use('/project/:projectId(\\d+)/client/', new ClientViewSet(viewSetOptions).routes());
// Load the OpenAPI spec from disk converting YAML to JSON and dynamically populating
// the servers array with our config.BACKEND_URL.
const spec = yaml.load('spec.yml');
if (!spec.servers) spec.servers = [];
spec.servers.push({ url: config.BACKEND_URL });
router.get('/spec', (ctx: Koa.Context) => {
ctx.set('Cache-Control', 'max-age=604800');
ctx.body = spec;
});
// robots.txt
router.get('/robots.txt', (ctx: Koa.Context) => {
ctx.set('Cache-Control', 'max-age=604800');
ctx.body = 'User-agent: *\nDisallow: /';
});
const app = new Koa();
app
.use(cors(config.CORS))
.use(async (ctx: Koa.Context, next: () => Promise<any>) => {
try {
await next();
// Catch Koa's stadard 404 response and throw our own error
if (ctx.response.status === 404) throw new NotFoundError();
} catch (err) {
if (err instanceof ObjectionValidationError) {
const e = new ValidationError();
e.errors = err.data;
ctx.body = e;
ctx.status = e.status;
} else if (err instanceof jwt.JsonWebTokenError || err instanceof jwt.TokenExpiredError) {
const e = new AuthenticationError(err.message);
ctx.body = e;
ctx.status = e.status;
} else {
ctx.status = err.status || err.statusCode || 500;
ctx.body = err;
}
// tslint:disable-next-line
if (config.DEBUG) console.log(err.stack);
}
})
.use(WebHookMiddleware);
if (config.SERVE_MEDIA) {
app.use(async (ctx: Koa.Context, next: () => Promise<any>) => {
if (ctx.path.match(/^\/media\/.*$/)) {
await send(ctx, path.join(config.MEDIA_ROOT, ctx.path.replace('/media/', '')));
} else {
await next();
}
});
}
if (config.SERVE_SWAGGER_UI) {
const swaggerIndex = fs
.readFileSync(path.join(swaggerUIAbsolutePath, 'index.html'), { encoding: 'utf8' })
.replace(/http:\/\/petstore\.swagger\.io\/v2\/swagger\.json/g, url.resolve(config.BACKEND_URL, 'spec'));
app.use(async (ctx: Koa.Context, next: () => Promise<any>) => {
if (ctx.path.match(/^\/swagger\/.*$/)) {
const _path = ctx.path.replace('/swagger/', '');
if (_path === '' || _path === '/index.html') {
ctx.body = swaggerIndex;
return;
}
await send(ctx, _path, { root: swaggerUIAbsolutePath });
} else {
await next();
}
});
}
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods())
.use(async (ctx: Koa.Context) => {
ctx.status = 404;
ctx.body = {
message: 'Not found',
status: 404
};
});
export default app;
export const models = {
Client,
Entry,
EntryTag,
EntryType,
Media,
MediaTag,
Permission,
Project,
ProjectInvite,
ProjectMembership,
Role,
User,
WebHook
};