SHARE
TWEET

Untitled

a guest Jun 18th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Router.ts
  2.  
  3. import { AuthController } from "../controllers/AuthController";
  4. import { UserController } from "../controllers/UserController";
  5. import { EstacaoController } from "../controllers/EstacaoController";
  6. import { EstacaoAreaController } from '../controllers/EstacaoAreaController';
  7. import { CalculateRouterController } from '../controllers/CalculateRouterController';
  8. import authVerify from '../middleware/authverify';
  9. import {Request, Response} from 'express'
  10. import facebookStrategy from 'passport-facebook';
  11. import * as passport from 'passport';
  12.  
  13. import {Passport} from '../App';
  14. export default class Router {
  15.  
  16.     private authController: AuthController;
  17.     private userController: UserController;
  18.     private estacaoController: EstacaoController;
  19.     private estacaoAreaController: EstacaoAreaController;
  20.     private calculateRouterController: CalculateRouterController;
  21.  
  22.     private BASE_URL: string = "/api/v1";
  23.  
  24.     private AUTH_URL: string = this.BASE_URL + "/auth";
  25.     private FACEBOOK_AUTH_URL: string = this.BASE_URL + "/auth/facebook";
  26.     private GOOGLE_AUTH_URL: string = this.BASE_URL + "/auth/google";
  27.     private TWITTER_AUTH_URL: string = this.BASE_URL + "/auth/twitter";
  28.  
  29.     private REGISTER_URL: string = this.BASE_URL + "/register";
  30.  
  31.     private ESTACAO_URL: string = this.BASE_URL + "/estacao";
  32.     private ESTACAO_URL_GET: string = this.BASE_URL + "/estacao/pontos";
  33.  
  34.     private ESTACAOAREA_URL: string = this.BASE_URL + "/estacao/area";
  35.  
  36.     private CALCULAR_ROTA_URL: string = this.BASE_URL + "/calcular/rota/:origem/:destino"
  37.     constructor(authController: AuthController, userController: UserController,
  38.         estacaoController: EstacaoController, estacaoAreaController: EstacaoAreaController,
  39.         calculateRouterController: CalculateRouterController) {
  40.         this.authController = authController;
  41.         this.userController = userController;
  42.         this.estacaoController = estacaoController;
  43.         this.estacaoAreaController = estacaoAreaController
  44.         this.calculateRouterController = calculateRouterController
  45.     }
  46.  
  47.     public startWith(app) {
  48.  
  49.         // AUTH
  50.         app.post('/auth/facebook', Passport.authenticate('facebookToken',  {session: false}))
  51.  
  52.  
  53.         app.route(this.AUTH_URL).post(this.authController.getUser);
  54.         // app.route(this.FACEBOOK_AUTH_URL).post(Auth().authenticateFacebook(), this.authController.getFacebookOrGoogleUser);
  55.         // app.route(this.GOOGLE_AUTH_URL).all(Auth().authenticateGoogle()).post(this.authController.getFacebookOrGoogleUser);
  56.  
  57.  
  58.         // REGISTER
  59.         app.route(this.REGISTER_URL).post(this.userController.createUser);
  60.  
  61.         //Estacao
  62.         app.route(this.ESTACAO_URL).all(authVerify).post(this.estacaoController.createNewStation);
  63.         app.route(this.ESTACAO_URL_GET).all(authVerify).get(this.estacaoController.getStation);
  64.  
  65.         //EstacaoArea
  66.         app.route(this.ESTACAOAREA_URL).post(this.estacaoAreaController.createNewAreaStation);
  67.  
  68.         //calcularRota
  69.         app.route(this.CALCULAR_ROTA_URL).get(this.calculateRouterController.calculateRouterNow);
  70.  
  71.     }
  72. }
  73.      
  74. import * as express from 'express'
  75. import * as bodyParser from 'body-parser';
  76. import { Server, createServer } from 'http';
  77. import { sequelize } from "./db/sequelize";
  78. import Router from './routes/Router';
  79. import * as io from 'socket.io';
  80. import * as passport from 'passport'
  81. import { AuthController } from './controllers/AuthController';
  82. import { UserRepository } from './repository/UserRepository';
  83. import { UserController } from './controllers/UserController';
  84. import { EstacaoController } from './controllers/EstacaoController';
  85. import { EstacaoRepository } from './repository/EstacaoRepository';
  86. import { EstacaoAreaController } from './controllers/EstacaoAreaController';
  87. import { EstacaoAreaRepository } from './repository/EstacaoAreaRepository';
  88. import { LinhasTrilhosRepository } from './repository/LinhasTrilhosRepository';
  89. import { CalculateRouterController } from './controllers/CalculateRouterController';
  90. import { CalculateRouterRepository } from './repository/CalculateRouterRepository';
  91. import { initializeStrategies } from './middleware/-initialize'
  92. import { Database } from './db/Database';
  93. // import { BilheteRepository } from './repository/BilheteRepository';
  94. // import { BilheteController } from './controllers/BilheteController';
  95.  
  96.  
  97. export const Passport = passport;
  98.  
  99. export class App {
  100.  
  101.     public static readonly PORT: number = 3000;
  102.     private app: express.Application;
  103.     private port: string | number;
  104.     private server: Server;
  105.     public auth;
  106.  
  107.     constructor() {
  108.         this.createApp();
  109.         this.config();
  110.         this.createServer();
  111.         this.initializeStrategies();
  112.         this.middleware();
  113.         this.listen();
  114.  
  115.         new Database();
  116.  
  117.     }
  118.  
  119.     private async listen() {
  120.         try {
  121.             await sequelize.sync({ force: false })
  122.  
  123.             this.server.listen(this.port, () => {
  124.                 console.log('Running server on port %s', this.port);
  125.             });
  126.         } catch (e) {
  127.             console.log("FATAL ERROR: COULD NOT CONNECT TO DATABASE.");
  128.             console.log("ERROR:" + e);
  129.         }
  130.     }
  131.  
  132.     private createApp(): void {
  133.         this.app = express();
  134.     }
  135.  
  136.     private config(): void {
  137.         this.port = process.env.PORT || App.PORT;
  138.     }
  139.  
  140.  
  141.  
  142.     private createServer(): void {
  143.         this.server = createServer(this.app);
  144.     }
  145.  
  146.     private middleware(): void {
  147.         this.app.use(bodyParser.json());
  148.         this.app.use(bodyParser.urlencoded({ extended: false }));
  149.         this.app.use(passport.initialize())
  150.         this.app.use(passport.session())
  151.  
  152.         const userRepository = new UserRepository();
  153.         const estacaoRepository = new EstacaoRepository();
  154.         const estacaoAreaRepository = new EstacaoAreaRepository();
  155.         const calculateRouterRepository = new CalculateRouterRepository();
  156.         const linhasTrilhosRepository = new LinhasTrilhosRepository();
  157.  
  158.         new Router(new AuthController(userRepository),
  159.             new UserController,
  160.             new EstacaoController(estacaoRepository),
  161.             new EstacaoAreaController(estacaoAreaRepository),
  162.             new CalculateRouterController(calculateRouterRepository,
  163.                 linhasTrilhosRepository))
  164.             .startWith(this.app);
  165.     }
  166.  
  167.     private initializeStrategies(): void {
  168.             initializeStrategies(passport)
  169.     }
  170.     public getApp(): express.Application {
  171.         return this.app;
  172.     }
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top