import express from "express" ; import IAccountDataStore from "../common/IAccountDataStore" ; import IRequestListenerFactory from "../common/IRequestListenerFactory" ; import logger from "../common/Logger" ; import IExtentStore from "../common/persistence/IExtentStore" ; import { RequestListener } from "../common/ServerBase" ; import AccountSASAuthenticator from "./authentication/AccountSASAuthenticator" ; import QueueSASAuthenticator from "./authentication/QueueSASAuthenticator" ; import QueueSharedKeyAuthenticator from "./authentication/QueueSharedKeyAuthenticator" ; import QueueTokenAuthenticator from "./authentication/QueueTokenAuthenticator" ; import ExpressMiddlewareFactory from "./generated/ExpressMiddlewareFactory" ; import IHandlers from "./generated/handlers/IHandlers" ; import MiddlewareFactory from "./generated/MiddlewareFactory" ; import MessageIdHandler from "./handlers/MessageIdHandler" ; import MessagesHandler from "./handlers/MessagesHandler" ; import QueueHandler from "./handlers/QueueHandler" ; import ServiceHandler from "./handlers/ServiceHandler" ; import AuthenticationMiddlewareFactory from "./middlewares/AuthenticationMiddlewareFactory" ; import PreflightMiddlewareFactory from "./middlewares/PreflightMiddlewareFactory" ; import { IQueueMetadataStore } from "./persistence/IQueueMetadataStore" ; import { DEFAULT_QUEUE_CONTEXT_PATH } from "./utils/constants" ; import morgan = require ( "morgan" ) ; import { OAuthLevel } from "../common/models" ; import IAuthenticator from "./authentication/IAuthenticator" ; import createQueueStorageContextMiddleware from "./middlewares/queueStorageContext.middleware" ; export class implements IRequestListenerFactory export default class QueueRequestListenerFactory implements IRequestListenerFactory { public constructor ( private readonly metadataStore , private readonly extentStore , private readonly accountDataStore , private readonly enableAccessLog , private readonly accessLogWriteStream ? , private readonly skipApiVersionCheck ? , private readonly oauth ? , private readonly disableProductStyleUrl ? ) { } public createRequestListener ( ) { const app = express ( ) . disable ( "x-powered-by" ) ; const middlewareFactory = new ExpressMiddlewareFactory ( logger , DEFAULT_QUEUE_CONTEXT_PATH ) ; const handlers = { serviceHandler : new ServiceHandler ( this . metadataStore , this . extentStore , logger ) , queueHandler : new QueueHandler ( this . metadataStore , this . extentStore , logger ) , messagesHandler : new MessagesHandler ( this . metadataStore , this . extentStore , logger ) , messageIdHandler : new MessageIdHandler ( this . metadataStore , this . extentStore , logger ) } ; if ( this . enableAccessLog ) { app . use ( morgan ( "common" , { stream : this . accessLogWriteStream } ) ) ; } app . use ( createQueueStorageContextMiddleware ( this . skipApiVersionCheck , this . disableProductStyleUrl ) ) ; app . use ( middlewareFactory . createDispatchMiddleware ( ) ) ; const authenticationMiddlewareFactory = new AuthenticationMiddlewareFactory ( logger ) ; const authenticators = [ new QueueSharedKeyAuthenticator ( this . accountDataStore , logger ) , new AccountSASAuthenticator ( this . accountDataStore , logger ) , new QueueSASAuthenticator ( this . accountDataStore , this . metadataStore , logger ) ] ; if ( this . oauth !== undefined ) { authenticators . push ( new QueueTokenAuthenticator ( this . accountDataStore , this . oauth , logger ) ) ; } app . use ( authenticationMiddlewareFactory . createAuthenticationMiddleware ( authenticators ) ) ; app . use ( middlewareFactory . createDeserializerMiddleware ( ) ) ; app . use ( middlewareFactory . createHandlerMiddleware ( handlers ) ) ; const preflightMiddlewareFactory = new PreflightMiddlewareFactory ( logger ) ; app . use ( preflightMiddlewareFactory . createCorsRequestMiddleware ( this . metadataStore , true ) ) ; app . use ( preflightMiddlewareFactory . createCorsRequestMiddleware ( this . metadataStore , false ) ) ; app . use ( middlewareFactory . createSerializerMiddleware ( ) ) ; app . use ( preflightMiddlewareFactory . createOptionsHandlerMiddleware ( this . metadataStore ) ) ; app . use ( middlewareFactory . createErrorMiddleware ( ) ) ; app . use ( middlewareFactory . createEndMiddleware ( ) ) ; return app ; } }