Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22ee91b
commit 6304153
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* App | ||
* Главный класс приложения | ||
*/ | ||
import * as express from 'express'; | ||
import { Express, Request, Response, NextFunction } from 'express'; | ||
import * as bodyParser from 'body-parser'; | ||
import * as session from 'express-session'; | ||
|
||
import IApplicationConfig from './core/IApplicationConfig'; | ||
import AppRoutes from './routes/AppRoutes'; | ||
import AppDataProviders from './providers/AppDataProviders'; | ||
|
||
export default class App { | ||
/** | ||
* App instance | ||
*/ | ||
private static app: App; | ||
|
||
/** | ||
* Express instance | ||
*/ | ||
private expApp: Express; | ||
|
||
/** | ||
* Провайдеры данных | ||
*/ | ||
private dataProviders: AppDataProviders; | ||
|
||
public static getInstance(): App { | ||
return App.app; | ||
} | ||
|
||
constructor(private config: IApplicationConfig) { | ||
if (App.app instanceof App) { | ||
throw new Error('Нельзя создать более одного экземпляра приложения'); | ||
} | ||
|
||
this.config = config; | ||
this.expApp = express(); | ||
App.app = this; | ||
} | ||
|
||
/** | ||
* Инициализация и запуск приложения | ||
*/ | ||
run(): void { | ||
// Подключаем использование сессий | ||
this.expApp.use(session({ | ||
resave: false, | ||
saveUninitialized: false, | ||
secret: 'chuck norris', | ||
cookie: {maxAge: 3600000} | ||
})); | ||
|
||
// Подключаем возможность обрабатывать запросы через | ||
// формы типа: application/x-www-form-urlencoded | ||
this.expApp.use(bodyParser.urlencoded({extended: false})); | ||
|
||
this.expApp.use((req: Request, res: Response, next: NextFunction) => { | ||
res.contentType('application/json'); | ||
next(); | ||
}); | ||
|
||
// Подключаем провайдеры | ||
this.dataProviders = new AppDataProviders(); | ||
|
||
// Подключаем маршрутизацию | ||
let appRouter = new AppRoutes(); | ||
appRouter.mount(this.expApp); | ||
|
||
// Запускаем сервер | ||
this.expApp.listen(this.config.listenPort, (err) => { | ||
if (err !== undefined) { | ||
console.log(err); | ||
} else { | ||
console.log("Server run on port: " + this.config.listenPort); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Возвращает провайдеры данных | ||
*/ | ||
get providers(): AppDataProviders { | ||
return this.dataProviders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Bootstrap скрипт | ||
*/ | ||
import App from './App'; | ||
|
||
const APP_CONFIG = { | ||
listenPort: 8080, | ||
appName: 'Example Application' | ||
}; | ||
|
||
try { | ||
|
||
(new App(APP_CONFIG)).run(); | ||
|
||
} catch (e) { | ||
console.error(e.message); | ||
} |