From 3a95396b773c3bfb5f8a407e256f98d926297002 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Tue, 10 Apr 2018 18:18:12 +0100 Subject: WIP: Add support for raw and meta views --- src/app.ts | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index c627027..8207aa6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,6 @@ import * as bodyParser from 'body-parser'; import * as ex from 'express'; import * as expressJwt from 'express-jwt'; -import * as logger from 'morgan'; import * as passport from 'passport'; import { default as authRouter, unauthorisedErr } from './auth'; @@ -24,7 +23,7 @@ import { default as UserGroup } from './db/model/UserGroup'; */ class App { /** - * The Express app used throught the server. + * The Express app used throughout the server. */ public express: ex.Express; @@ -42,16 +41,23 @@ class App { public constructor(enableLog: boolean) { this.logEnabled = enableLog; this.express = ex(); + this.express.use(( + req: ex.Request, res: ex.Response, next: ex.NextFunction + ): void => { console.log(req.url); next(); }); this.precondition(); this.middleware(); this.mountRoutes(); this.express.use( async(req: ex.Request, res: ex.Response, next: ex.NextFunction): Promise => { - res.json({ - status: 'success', - data: await res.locals.data - }); + if (res.locals.function) { + res.locals.function.bind(res)(res.locals.data); + } else { + res.json({ + status: 'success', + data: await res.locals.data + }); + } } ); this.errorware(); @@ -83,9 +89,6 @@ class App { } next(); }); - if (this.logEnabled) { - this.express.use(logger('combined')); - } this.express.use(bodyParser.json()); this.express.use(bodyParser.urlencoded({ extended: false })); } @@ -143,7 +146,7 @@ class App { username: name } }); - if (user === null) { + if (user == null) { return next(new RequestError(404, 'user_not_found')); } else { res.locals.user = user; @@ -177,9 +180,8 @@ class App { ): Promise => { const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; - if (project === null) { - next(new RequestError(404, 'project_not_found')); - return; + if (project == null) { + return next(new RequestError(404, 'project_not_found')); } let file: File = project.rootFolder; // TODO Rewrite using sub-queries instead of repeated queries. @@ -190,7 +192,7 @@ class App { parentFolder: file } }); - if (fileOrNull === null) { + if (fileOrNull == null) { return next(); } else { file = fileOrNull; @@ -206,7 +208,7 @@ class App { name: fileNames[0] } }); - if (parentFolder === null) { + if (parentFolder == null) { return next(); } res.locals.parentFolder = parentFolder; @@ -231,9 +233,8 @@ class App { uuid: fileId } }); - if (file === null) { - next(new RequestError(404, 'file_not_found')); - return; + if (file == null) { + return next(new RequestError(404, 'file_not_found')); } else { res.locals.file = file; } -- cgit v1.2.1 From 56b06b5ee36ed83fc44d0775b6218aa8fed85b5e Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 21:29:15 +0100 Subject: fixed auth tests --- src/app.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index c627027..91b8f95 100644 --- a/src/app.ts +++ b/src/app.ts @@ -101,7 +101,8 @@ class App { important: 'Endpoints start from /cs3099group-be-4/' }); }); - defRouter.get('/*', (req: ex.Request, res: ex.Response): void => { + const errRouter: ex.Router = ex.Router(); + errRouter.get('/*', (req: ex.Request, res: ex.Response): void => { res.status(404) .json({ status: 'error', @@ -112,6 +113,7 @@ class App { this.express.use('/cs3099group-be-4', FileRouter); this.express.use('/cs3099group-be-4', authRouter); this.express.use('/', defRouter); + this.express.use('/XX*', errRouter); } /** -- cgit v1.2.1 From f7220be4e1b80cdba0fbaf41f7382cbf18561e2b Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 23:30:51 +0100 Subject: sth works maybe --- src/app.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 91b8f95..1378177 100644 --- a/src/app.ts +++ b/src/app.ts @@ -161,6 +161,7 @@ class App { req: ex.Request, res: ex.Response, next: ex.NextFunction, project: string ): Promise => { + console.log("## as " + project) res.locals.project = await Project.findOne({ where: { name: project -- cgit v1.2.1 From 3a02f5b8c7f3ae40072f41466c214a984885c2ac Mon Sep 17 00:00:00 2001 From: Hafeez Date: Thu, 12 Apr 2018 14:57:47 +0100 Subject: fixed precondition --- src/app.ts | 122 +------------------------------------------------------------ 1 file changed, 1 insertion(+), 121 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index c51c574..d8dfe5d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,8 +43,7 @@ class App { this.express = ex(); this.express.use(( req: ex.Request, res: ex.Response, next: ex.NextFunction - ): void => { console.log(req.url); next(); }); - this.precondition(); + ): void => { console.log(req.url); next(); }); this.middleware(); this.mountRoutes(); this.express.use( @@ -126,125 +125,6 @@ class App { this.express.use(unauthorisedErr); this.express.use(errorHandler); } - - /** - * Introduces variable-preconditioning middleware to the Express app. - * - * These middleware functions pre-process Express' matching variables - * in routes (e.g. `:project_name`). - * If an entity cannot be found in the database, the corresponding - * value is set to `null` instead. - */ - private precondition(): void { - // Fetch a user from their name: - this.express.param( - 'username', - async( - req: ex.Request, res: ex.Response, next: ex.NextFunction, - name: string - ): Promise => { - const user: User | null = await User.findOne({ - where: { - username: name - } - }); - if (user == null) { - return next(new RequestError(404, 'user_not_found')); - } else { - res.locals.user = user; - } - next(); - } - ); - - // Fetch a project from its name: - this.express.param( - 'project_name', - async( - req: ex.Request, res: ex.Response, next: ex.NextFunction, - project: string - ): Promise => { - console.log("## as " + project) - res.locals.project = await Project.findOne({ - where: { - name: project - } - }); - next(); - } - ); - - // Fetch a file from its path: - this.express.param( - 'file', - async( - req: ex.Request, res: ex.Response, next: ex.NextFunction, - filename: string - ): Promise => { - const fileNames: string[] = req.params.path.split('/'); - const project: Project | null = res.locals.project; - if (project == null) { - return next(new RequestError(404, 'project_not_found')); - } - let file: File = project.rootFolder; - // TODO Rewrite using sub-queries instead of repeated queries. - while (fileNames.length > 2) { - const fileOrNull: File | null = await File.findOne({ - where: { - name: fileNames[0], - parentFolder: file - } - }); - if (fileOrNull == null) { - return next(); - } else { - file = fileOrNull; - } - fileNames.splice(0, 1); - } - const parentFolder: File | null = await File.findOne({ - include: [ - { model: File, as: 'containedFiles' } - ], - where: { - parentFolder: file, - name: fileNames[0] - } - }); - if (parentFolder == null) { - return next(); - } - res.locals.parentFolder = parentFolder; - const tFile: File | undefined = parentFolder.containedFiles.find( - (f: File): boolean => f.name === fileNames[1] - ); - res.locals.file = tFile; - res.locals.filename = fileNames[1]; - next(); - } - ); - - // Fetch a file from its UUID: - this.express.param( - 'id', - async( - req: ex.Request, res: ex.Response, next: ex.NextFunction, - fileId: string - ): Promise => { - const file: File | null = await File.findOne({ - where: { - uuid: fileId - } - }); - if (file == null) { - return next(new RequestError(404, 'file_not_found')); - } else { - res.locals.file = file; - } - next(); - } - ); - } } export default new App(true).express; -- cgit v1.2.1 From deab81f8ef469c4119838d72b56683b8dd4cbcfe Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:10:32 +0100 Subject: fixed some privs --- src/app.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index d8dfe5d..f71ecf3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -68,6 +68,7 @@ class App { private middleware(): void { this.express.use(expressJwt({secret: 'Mr Secret'}) .unless({path: [ + '/', '/cs3099group-be-4/oauth/token', '/cs3099group-be-4/_supported_protocols_' ] }) -- cgit v1.2.1 From 244fa29c38dcae5732a06cdb39de1a2a00557117 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 13 Apr 2018 16:12:04 +0100 Subject: Fix minor places, and add Postman collection --- src/app.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index d8dfe5d..fd094ef 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,7 @@ import * as expressJwt from 'express-jwt'; import * as passport from 'passport'; import { default as authRouter, unauthorisedErr } from './auth'; +import { logger } from './logger'; import { RequestError, errorHandler } from './errors/errorware'; import FileRouter from './FileRouter'; @@ -39,11 +40,15 @@ class App { * server. */ public constructor(enableLog: boolean) { + logger.debug('Constructing router'); this.logEnabled = enableLog; this.express = ex(); - this.express.use(( - req: ex.Request, res: ex.Response, next: ex.NextFunction - ): void => { console.log(req.url); next(); }); + this.express.use( + (req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { + logger.info(`Connection from ${req.ip} requesting ${req.url}`); + next(); + } + ); this.middleware(); this.mountRoutes(); this.express.use( @@ -57,6 +62,7 @@ class App { data: await res.locals.data }); } + logger.success(`Responded to ${req.ip}`); } ); this.errorware(); @@ -66,6 +72,7 @@ class App { * Configures regular Express middleware. */ private middleware(): void { + logger.debug('Loading basic middleware'); this.express.use(expressJwt({secret: 'Mr Secret'}) .unless({path: [ '/cs3099group-be-4/oauth/token', -- cgit v1.2.1 From 72704a9fb688acdb57b33846796b13abbeb18a3c Mon Sep 17 00:00:00 2001 From: Hafeez Date: Sat, 14 Apr 2018 18:26:36 +0100 Subject: returns 404 for unused routes only if logged in --- src/app.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index c299cf7..6854c46 100644 --- a/src/app.ts +++ b/src/app.ts @@ -77,7 +77,7 @@ class App { .unless({path: [ '/', '/cs3099group-be-4/oauth/token', - '/cs3099group-be-4/_supported_protocols_' + '/cs3099group-be-4/_supported_protocols_' ] }) ); this.express.use(passport.initialize()); @@ -110,20 +110,20 @@ class App { message: 'Welcome to the CS3099 BE4 server!', important: 'Endpoints start from /cs3099group-be-4/' }); - }); - const errRouter: ex.Router = ex.Router(); - errRouter.get('/*', (req: ex.Request, res: ex.Response): void => { + }); + const errRouter: ex.Router = ex.Router(); + errRouter.get('*', (req: ex.Request, res: ex.Response): void => { res.status(404) .json({ status: 'error', error : 'invalid_route', error_description: 'Endpoints start from /cs3099group-be-4/' }); - }); + }); this.express.use('/cs3099group-be-4', FileRouter); this.express.use('/cs3099group-be-4', authRouter); this.express.use('/', defRouter); - this.express.use('/XX*', errRouter); + this.express.use('*', errRouter); } /** -- cgit v1.2.1 From 71a674b70f0003e215f6d9ba5e5b94db36a83b36 Mon Sep 17 00:00:00 2001 From: Hafeez Date: Sat, 14 Apr 2018 19:47:18 +0100 Subject: think i fixed something --- src/app.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 6854c46..1d1c9cb 100644 --- a/src/app.ts +++ b/src/app.ts @@ -111,19 +111,26 @@ class App { important: 'Endpoints start from /cs3099group-be-4/' }); }); - const errRouter: ex.Router = ex.Router(); - errRouter.get('*', (req: ex.Request, res: ex.Response): void => { - res.status(404) + //const errRouter: ex.Router = ex.Router(); + + this.express.use('/cs3099group-be-4', FileRouter); + this.express.use('/cs3099group-be-4', authRouter); + this.express.use('/', defRouter); + this.express.use((req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { + console.log(res.locals) + if (Object.keys(res.locals).length === 0) { + console.log(404) + res.status(404) .json({ status: 'error', error : 'invalid_route', error_description: 'Endpoints start from /cs3099group-be-4/' }); - }); - this.express.use('/cs3099group-be-4', FileRouter); - this.express.use('/cs3099group-be-4', authRouter); - this.express.use('/', defRouter); - this.express.use('*', errRouter); + } + else { + next(); + } + }); } /** -- cgit v1.2.1 From 1e6cb08fcdec54f1263eb1f6fdae6e0f438dfc97 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 15 Apr 2018 17:24:10 +0100 Subject: fixed logging --- src/app.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 1d1c9cb..e9d1d68 100644 --- a/src/app.ts +++ b/src/app.ts @@ -110,26 +110,22 @@ class App { message: 'Welcome to the CS3099 BE4 server!', important: 'Endpoints start from /cs3099group-be-4/' }); - }); - //const errRouter: ex.Router = ex.Router(); - + }); this.express.use('/cs3099group-be-4', FileRouter); this.express.use('/cs3099group-be-4', authRouter); - this.express.use('/', defRouter); - this.express.use((req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { - console.log(res.locals) + this.express.use('/', defRouter); + this.express.use((req: ex.Request, res: ex.Response, + next: ex.NextFunction): void => { if (Object.keys(res.locals).length === 0) { - console.log(404) res.status(404) .json({ status: 'error', error : 'invalid_route', error_description: 'Endpoints start from /cs3099group-be-4/' }); + } else { + next(); } - else { - next(); - } }); } @@ -149,5 +145,5 @@ export default new App(true).express; */ // tslint:disable-next-line:variable-name export const TestApp: () => ex.Express = (): ex.Express => { - return new App(false).express; + return new App(true).express; }; -- cgit v1.2.1 From c8998c292d48e833f66cb43ea2c945f1a865a10e Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 15 Apr 2018 18:03:01 +0100 Subject: fixed fetching current_user --- src/app.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index e9d1d68..4dfeeda 100644 --- a/src/app.ts +++ b/src/app.ts @@ -77,7 +77,7 @@ class App { .unless({path: [ '/', '/cs3099group-be-4/oauth/token', - '/cs3099group-be-4/_supported_protocols_' + '/cs3099group-be-4/_supported_protocols_' ] }) ); this.express.use(passport.initialize()); -- cgit v1.2.1 From ec1162de99e1744b52fcce0c5341649a7e9a2955 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 19 Apr 2018 16:19:46 +0100 Subject: Add CORS support --- src/app.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 4dfeeda..09b9e6a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,6 +43,7 @@ class App { logger.debug('Constructing router'); this.logEnabled = enableLog; this.express = ex(); + this.cors(); this.express.use( (req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { logger.info(`Connection from ${req.ip} requesting ${req.url}`); @@ -68,6 +69,22 @@ class App { this.errorware(); } + /** + * Enables Cross-Origin for the server. + */ + public cors(): void { + this.express.use(( + req: ex.Request, res: ex.Response, next: ex.NextFunction + ): void => { + res.header('Access-Control-Allow-Origin', '*'); + res.header( + 'Access-Control-Allow-Headers', + 'Origin, X-Requested-With, Content-Type, Accept' + ); + next(); + }); + } + /** * Configures regular Express middleware. */ @@ -114,8 +131,8 @@ class App { this.express.use('/cs3099group-be-4', FileRouter); this.express.use('/cs3099group-be-4', authRouter); this.express.use('/', defRouter); - this.express.use((req: ex.Request, res: ex.Response, - next: ex.NextFunction): void => { + this.express.use( + (req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { if (Object.keys(res.locals).length === 0) { res.status(404) .json({ -- cgit v1.2.1