Advertisement
Guest User

script.ts

a guest
Aug 8th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Koa from 'koa';
  2. import jwt from 'koa-jwt';
  3. import bodyParser from 'koa-bodyparser';
  4. import helmet from 'koa-helmet';
  5. import cors from '@koa/cors';
  6. import winston from 'winston';
  7. import { createConnection } from 'typeorm';
  8. import 'reflect-metadata';
  9. import * as PostgressConnectionStringParser from 'pg-connection-string';
  10.  
  11. import { logger } from './logging';
  12. import { config } from './config';
  13. import { unprotectedRouter } from './unprotectedRoutes';
  14. import { protectedRouter } from './protectedRoutes';
  15.  
  16. // Get DB connection options from env variable
  17. const connectionOptions = PostgressConnectionStringParser.parse(config.databaseUrl);
  18.  
  19. // create connection with database
  20. // note that its not active database connection
  21. // TypeORM creates you connection pull to uses connections from pull on your requests
  22. createConnection({
  23.     type: 'postgres',
  24.     host: connectionOptions.host,
  25.     port: connectionOptions.port,
  26.     username: connectionOptions.user,
  27.     password: connectionOptions.password,
  28.     database: connectionOptions.database,
  29.     synchronize: true,
  30.     logging: false,
  31.     entities: [
  32.        'dist/entity/**/*.js'
  33.     ],
  34.     extra: {
  35.         ssl: config.dbsslconn, // if not development, will use SSL
  36.     }
  37. }).then(async connection => {
  38.  
  39.     const app = new Koa();
  40.  
  41.     // Provides important security headers to make your app more secure
  42.     app.use(helmet());
  43.  
  44.     // Enable cors with default options
  45.     app.use(cors());
  46.  
  47.     // Logger middleware -> use winston as logger (logging.ts with config)
  48.     app.use(logger(winston));
  49.  
  50.     // Enable bodyParser with default options
  51.     app.use(bodyParser());
  52.  
  53.     // these routes are NOT protected by the JWT middleware, also include middleware to respond with "Method Not Allowed - 405".
  54.     app.use(unprotectedRouter.routes()).use(unprotectedRouter.allowedMethods());
  55.  
  56.     // JWT middleware -> below this line routes are only reached if JWT token is valid, secret as env variable
  57.     // do not protect swagger-json and swagger-html endpoints
  58.     app.use(jwt({ secret: config.jwtSecret }).unless({ path: [/^\/swagger-/] }));
  59.  
  60.     // these routes are protected by the JWT middleware, also include middleware to respond with "Method Not Allowed - 405".
  61.     app.use(protectedRouter.routes()).use(protectedRouter.allowedMethods());
  62.  
  63.     app.listen(config.port);
  64.  
  65.     console.log(`Server running on port ${config.port}`);
  66.  
  67. }).catch(error => console.log('TypeORM connection error: ', error));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement