|
1 | | -# MicroFramework |
| 1 | +# Microframework |
2 | 2 |
|
3 | | -Micro framework integrates popular libraries like [express.js][1], [Mongodb ODM][2], [validator.ts][5], |
4 | | -[controllers.ts][4], [event-dispatcher.ts][7] |
5 | | -and others for use in your Typescript application. Framework ships by default [dependency injection framework][3] and |
6 | | -[configuration framework][6] to make all modules to work like a sh |
| 3 | +Microframework is a simple tool that allows you to execute your modules in a proper order, |
| 4 | +helping you to organize bootstrap code in your application. |
7 | 5 |
|
8 | | -## Notice |
| 6 | +## Usage |
9 | 7 |
|
10 | | -Library is under active development and API may change from version to version. |
11 | | -Please consider it before using this library. |
| 8 | +First, install the module: |
12 | 9 |
|
13 | | -## Installation |
| 10 | +``` |
| 11 | +npm i microframework |
| 12 | +``` |
14 | 13 |
|
15 | | -You use framework with one or more the available modules. Lets say you want to use [express.js][1], [Mongodb ODM][2], |
16 | | -[validator][5], [event-dispatcher][7] and [restful controllers][4]. |
| 14 | +Second, create a simple "module" named `expressModule`: |
17 | 15 |
|
18 | | -1. Install npm modules: |
| 16 | +```typescript |
| 17 | +import {MicroframeworkBootstrapSettings} from "microframework"; |
19 | 18 |
|
20 | | - `npm install microframework microframework-express microframework-typeodm microframework-controllers.ts |
21 | | -microframework-validator.ts microframework-event-dispatcher.ts configurator.ts controllers.ts typedi typeodm validator.ts --save` |
| 19 | +export function expressModule(settings: MicroframeworkBootstrapSettings) { |
22 | 20 |
|
23 | | -2. Use [typings](https://github.com/typings/typings) to install all required definition dependencies. |
| 21 | + // create express app |
| 22 | + const app = express(); |
24 | 23 |
|
25 | | - `typings install` |
| 24 | + // register all routes, Routes are just routes that should be stored outside of this module |
| 25 | + const routes: any = Routes; |
| 26 | + Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath])); |
26 | 27 |
|
27 | | -3. ES6 features are used, so you may want to install [es6-shim](https://github.com/paulmillr/es6-shim) too: |
| 28 | + // run application to listen on given port |
| 29 | + app.listen(3000); |
| 30 | + |
| 31 | + // your module also can return a promise |
| 32 | +} |
| 33 | +``` |
28 | 34 |
|
29 | | - `npm install es6-shim --save` |
| 35 | +Create `app.ts` and bootstrap a microframework and your express module: |
30 | 36 |
|
31 | | - you may want to `require("es6-shim");` in your app |
| 37 | +```typescript |
| 38 | +import {bootstrapMicroframework} from "microframework"; |
| 39 | +import {expressModule} from "./expressModule"; |
32 | 40 |
|
33 | | -## Usage |
| 41 | +bootstrapMicroframework([ |
| 42 | + expressModule |
| 43 | +]) |
| 44 | + .then(() => console.log("Application is up and running.")) |
| 45 | + .catch(error => console.log("Application is crashed: " + error)); |
| 46 | +``` |
34 | 47 |
|
35 | | -1. Create `src/app.ts`: |
36 | | - |
37 | | - ```typescript |
38 | | - import {MicroFrameworkBootstrapper} from "microframework/MicroFrameworkBootstrapper"; |
39 | | - import {ExpressModule} from "microframework-express/ExpressModule"; |
40 | | - import {ControllersTsModule} from "microframework-controllers.ts/ControllersTsModule"; |
41 | | - import {TypeOdmModule} from "microframework-typeodm/TypeOdmModule"; |
42 | | - import {ValidatorTsModule} from "microframework-validator.ts/ValidatorTsModule"; |
43 | | - import {EventDispatcherTsModule} from "microframework-event-dispatcher.ts/EventDispatcherTsModule"; |
44 | | - |
45 | | - new MicroFrameworkBootstrapper({ srcDirectory: __dirname }) |
46 | | - .registerModules([ |
47 | | - new ExpressModule(), |
48 | | - new TypeOdmModule(), |
49 | | - new ControllersTsModule(), |
50 | | - new ValidatorTsModule(), |
51 | | - new EventDispatcherTsModule() |
52 | | - ]) |
53 | | - .bootstrap() |
54 | | - .then(result => console.log("Module is running. Open localhost:3000")) |
55 | | - .catch(error => console.error("Error: ", error)); |
56 | | - ``` |
57 | | - |
58 | | -2. Create configuration file `config/config.json` (note: its not in the same dir where your source is, |
59 | | -folder is near your `package.json` file): |
60 | | - |
61 | | - ```json |
62 | | - { |
63 | | - "express": { |
64 | | - "port": "3000", |
65 | | - "bodyParser": { |
66 | | - "type": "json" |
67 | | - } |
68 | | - }, |
69 | | - "typeodm": { |
70 | | - "connection": { |
71 | | - "url": "mongodb://localhost:27017/microframework-sample" |
72 | | - } |
73 | | - } |
74 | | - } |
75 | | - ``` |
76 | | - |
77 | | -3. Now create your first controller, lets say QuestionController: `src/controller/QuestionController.ts`: |
78 | | - |
79 | | - ```typescript |
80 | | - import {JsonController, Get} from "controllers.ts/Annotations"; |
81 | | - import {Response, Request} from "express"; |
| 48 | +That's all. You can do same for other modules. |
| 49 | +Take a look on sample to understand better how concept of modules and their bootstrapping in microframework. |
| 50 | + |
| 51 | +## Settings |
82 | 52 |
|
83 | | - @JsonController() |
84 | | - export class QuestionController { |
| 53 | +You can specify additional options to microframework. |
| 54 | + |
| 55 | +* `logo` - Logo needs to be used before application launches. To use logo ansi-art module should be installed. |
| 56 | +* `showBootstrapTime` - If set to true then framework shows how much time was spend to bootstrap all modules. |
| 57 | +* `bootstrapTimeout` - Number of milliseconds to wait before framework will bootstrap all modules. |
| 58 | + |
| 59 | +Example of using settings: |
| 60 | + |
| 61 | +```typescript |
| 62 | +import {bootstrapMicroframework} from "microframework"; |
| 63 | + |
| 64 | +bootstrapMicroframework({ |
| 65 | + config: { |
| 66 | + logo: "MyApp", |
| 67 | + showBootstrapTime: true, |
| 68 | + bootstrapTimeout: 10 |
| 69 | + }, |
| 70 | + modules: [ |
| 71 | + expressModule, |
| 72 | + // ... |
| 73 | + ] |
| 74 | +}) |
| 75 | + .then(() => console.log("Application is up and running.")) |
| 76 | + .catch(error => console.log("Application is crashed: " + error)); |
| 77 | +``` |
| 78 | + |
| 79 | +## Sharing data across modules |
| 80 | + |
| 81 | +Sometimes few modules need to communicate between each other and use shared data. |
| 82 | +For such purpose you can store the data in `settings` object passed to each module |
| 83 | +and use stored data across all other modules. For example: |
| 84 | + |
| 85 | +```typescript |
| 86 | +import {MicroframeworkBootstrapSettings} from "microframework"; |
| 87 | + |
| 88 | +export function expressModule(settings: MicroframeworkBootstrapSettings) { |
| 89 | + |
| 90 | + // create express app |
| 91 | + const app = express(); |
| 92 | + |
| 93 | + // register all routes, Routes are just routes that should be stored outside of this module |
| 94 | + const routes: any = Routes; |
| 95 | + Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath])); |
| 96 | + |
| 97 | + // run application to listen on given port |
| 98 | + app.listen(3000); |
85 | 99 |
|
86 | | - @Get("/questions") |
87 | | - all(): any[] { |
88 | | - return [ |
89 | | - { title: "Which processor to choose?", text: "Which processor is better: Core i5 or Core i7?" }, |
90 | | - { title: "When new star wars gonna be released?", text: "When star wars gonna be released? I think in december" } |
91 | | - ]; |
| 100 | + settings.setData("express_app", app); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +And another modules can use data this way: |
| 105 | + |
| 106 | +```typescript |
| 107 | +import {MicroframeworkBootstrapSettings} from "microframework"; |
| 108 | + |
| 109 | +export function socketIoModule(settings: MicroframeworkBootstrapSettings) { |
| 110 | + const io = io(); |
| 111 | + io.useExpress(settings.getData("express_app")); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Application Shutdown |
| 116 | + |
| 117 | +In the case if you want to shutdown running application you need to do following: |
| 118 | + |
| 119 | +```typescript |
| 120 | +import {bootstrapMicroframework} from "microframework"; |
| 121 | + |
| 122 | +bootstrapMicroframework({ |
| 123 | + config: { |
| 124 | + logo: "MyApp", |
| 125 | + showBootstrapTime: true, |
| 126 | + bootstrapTimeout: 10 |
| 127 | + }, |
| 128 | + modules: [ |
| 129 | + expressModule, |
| 130 | + // ... |
| 131 | + ] |
| 132 | +}) |
| 133 | + .then(framework => { |
| 134 | + // do something before shutdown |
| 135 | + |
| 136 | + // and shutdown everything |
| 137 | + return framework.shutdown(); |
| 138 | + }) |
| 139 | + .then(() => { |
| 140 | + // now everything is turned off |
| 141 | + }) |
| 142 | + .catch(error => console.log("Application is crashed: " + error)); |
| 143 | +``` |
| 144 | + |
| 145 | +All modules which use resources should release them, for example: |
| 146 | + |
| 147 | +```typescript |
| 148 | +export async function typeormModule(settings: MicroframeworkBootstrapSettings) { |
| 149 | + const connection = await createConnection({ |
| 150 | + driver: { |
| 151 | + type: "mysql", |
| 152 | + host: "localhost", |
| 153 | + username: "test", |
| 154 | + password: "test", |
| 155 | + database: "test" |
92 | 156 | } |
93 | | - } |
94 | | - ``` |
95 | | - |
96 | | -4. Run your app and open `http://localhost:3000/questions` in browser. You should see list of your questions. |
97 | | - |
98 | | -## Available Modules |
99 | | - |
100 | | -* [microframework-express](https://github.com/pleerock/microframework-express) - integration with [express.js][1] |
101 | | -* [microframework-typeodm](https://github.com/pleerock/microframework-typeodm) - integration with [TypeODM][2] |
102 | | -* [microframework-controllers.ts](https://github.com/pleerock/microframework-controllers.ts) - integration with [controllers.ts][4] |
103 | | -* [microframework-validator.ts](https://github.com/pleerock/microframework-validator.ts) - integration with [validator.ts][5] |
104 | | -* [microframework-event-dispatcher.ts](https://github.com/pleerock/microframework-event-dispatcher.ts) - integration with [event-dispatcher.ts][7] |
105 | | -* [microframework-winston](https://github.com/pleerock/microframework-winston) - integration with [winston][8] |
106 | | -* [microframework-elasticsearch](https://github.com/pleerock/microframework-elasticsearch) - integration with [elasticsearch][9] |
107 | | -* [microframework-rabbit.ts](https://github.com/pleerock/microframework-rabbit.ts) - integration with [rabbit.js][10] |
108 | | - |
109 | | -## Todos |
110 | | - |
111 | | -* cover with tests |
112 | | -* more documentation and examples |
113 | | -* more modules |
114 | | -* add ability to include other configs by include path in the object? |
115 | | -* add yo generator |
116 | | -* good to have todo mvc sample |
117 | | - |
118 | | -[1]: http://expressjs.com/ |
119 | | -[2]: https://github.com/pleerock/typeodm |
120 | | -[3]: https://github.com/pleerock/typedi |
121 | | -[4]: https://github.com/pleerock/controllers.ts |
122 | | -[5]: https://github.com/pleerock/validator.ts |
123 | | -[6]: https://github.com/pleerock/configurator.ts |
124 | | -[7]: https://github.com/pleerock/event-dispatcher.ts |
125 | | -[8]: https://github.com/winstonjs/winston |
126 | | -[9]: https://github.com/elastic/elasticsearch-js |
127 | | -[10]: https://github.com/squaremo/rabbit.js/ |
| 157 | + }); |
| 158 | + |
| 159 | + settings.addShutdownHandler(() => connection.close()); |
| 160 | +} |
| 161 | +``` |
0 commit comments