aboutsummaryrefslogtreecommitdiff
path: root/src/app.ts
diff options
context:
space:
mode:
authorTom Harley2018-03-07 13:33:01 +0000
committerTom Harley2018-03-07 13:33:01 +0000
commite53741754d6f6039bc335a82e40af633e8a9762e (patch)
treeda56509373f8e5fa46a468704559755d84b177eb /src/app.ts
parentEdit configs to enable documentation generation (diff)
downloadmedicine-e53741754d6f6039bc335a82e40af633e8a9762e.tar.gz
medicine-e53741754d6f6039bc335a82e40af633e8a9762e.zip

Add documentation for some files

Diffstat (limited to 'src/app.ts')
-rw-r--r--src/app.ts46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/app.ts b/src/app.ts
index bedffa4..bcb29ca 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -13,10 +13,31 @@ import { default as File } from './db/model/File';
import { default as Project } from './db/model/Project';
import { default as User } from './db/model/User';
+/**
+ * Configuration of the Express server app, including installation of
+ * various middleware functions.
+ */
+
+/**
+ * The Class used to configure the Express app used in the server instance.
+ */
class App {
+ /**
+ * The Express app used throught the server.
+ */
public express: ex.Express;
+
+ /**
+ * Whether or not logging is enabled on the server.
+ */
private readonly logEnabled: boolean = true;
+ /**
+ * Constructs an instance of the app, by loading middleware.
+ *
+ * @param enableLog Whether or not logging should be enabled on the
+ * server.
+ */
public constructor(enableLog: boolean) {
this.logEnabled = enableLog;
this.express = ex();
@@ -35,11 +56,13 @@ class App {
this.errorware();
}
- // Configure Express middleware.
+ /**
+ * Configures regular Express middleware.
+ */
private middleware(): void {
this.express.use(expressJwt({secret: 'Mr Secret'})
.unless({path: [
- '/cs3099group-be-4/login',
+ '/cs3099group-be-4/oauth/token',
'/cs3099group-be-4/_supported_protocols_'
] })
);
@@ -51,7 +74,9 @@ class App {
this.express.use(bodyParser.urlencoded({ extended: false }));
}
- // Configure API endpoints.
+ /**
+ * Adds the Router routes to the Express app.
+ */
private mountRoutes(): void {
const defRouter: ex.Router = ex.Router();
defRouter.get('/', (req: ex.Request, res: ex.Response): void => {
@@ -75,11 +100,22 @@ class App {
this.express.use('/', defRouter);
}
+ /**
+ * Adds error-handling (4-arity) middleware to the Express app.
+ */
private errorware(): void {
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(
@@ -197,6 +233,10 @@ class App {
}
export default new App(true).express;
+
+/**
+ * A version of the app used only for testing.
+ */
export function TestApp() {
return new App(false).express;
}