Skip to content

Commit bc1acb9

Browse files
author
Umed Khudoiberdiev
committed
completely refactored microframework
1 parent 8093ef1 commit bc1acb9

37 files changed

+616
-975
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
build/
22
node_modules/
3-
typings/
43
npm-debug.log

README.md

Lines changed: 144 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,161 @@
1-
# MicroFramework
1+
# Microframework
22

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.
75

8-
## Notice
6+
## Usage
97

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:
129

13-
## Installation
10+
```
11+
npm i microframework
12+
```
1413

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`:
1715

18-
1. Install npm modules:
16+
```typescript
17+
import {MicroframeworkBootstrapSettings} from "microframework";
1918

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) {
2220

23-
2. Use [typings](https://github.com/typings/typings) to install all required definition dependencies.
21+
// create express app
22+
const app = express();
2423

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]));
2627

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+
```
2834

29-
`npm install es6-shim --save`
35+
Create `app.ts` and bootstrap a microframework and your express module:
3036

31-
you may want to `require("es6-shim");` in your app
37+
```typescript
38+
import {bootstrapMicroframework} from "microframework";
39+
import {expressModule} from "./expressModule";
3240

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+
```
3447

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
8252

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);
8599

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"
92156
}
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+
```

gulpfile.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

gulpfile.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Gulpclass, Task, SequenceTask} from "gulpclass/Decorators";
1+
import {Gulpclass, Task, SequenceTask} from "gulpclass";
22

33
const gulp = require("gulp");
44
const del = require("del");
@@ -31,7 +31,7 @@ export class Gulpfile {
3131
*/
3232
@Task()
3333
compile() {
34-
return gulp.src("*.js", { read: false })
34+
return gulp.src("./package.json", { read: false })
3535
.pipe(shell(["tsc"]));
3636
}
3737

@@ -44,7 +44,7 @@ export class Gulpfile {
4444
*/
4545
@Task()
4646
npmPublish() {
47-
return gulp.src("*.js", { read: false })
47+
return gulp.src("./package.json", { read: false })
4848
.pipe(shell([
4949
"cd ./build/package && npm publish"
5050
]));
@@ -80,15 +80,6 @@ export class Gulpfile {
8080
.pipe(gulp.dest("./build/package"));
8181
}
8282

83-
/**
84-
* This task will copy typings.json file to the build package.
85-
*/
86-
@Task()
87-
copyTypingsFile() {
88-
return gulp.src("./typings.json")
89-
.pipe(gulp.dest("./build/package"));
90-
}
91-
9283
/**
9384
* Creates a package that can be published to npm.
9485
*/
@@ -97,7 +88,7 @@ export class Gulpfile {
9788
return [
9889
"clean",
9990
"compile",
100-
["packageFiles", "packagePreparePackageFile", "packageReadmeFile", "copyTypingsFile"]
91+
["packageFiles", "packagePreparePackageFile", "packageReadmeFile"]
10192
];
10293
}
10394

package.json

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "microframework",
33
"private": true,
4-
"version": "0.5.5",
5-
"description": "Micro framework is a bundle of express.js, mongodb ODM, validator, dependancy injection framework and restful controllers for your apps using Typescript",
6-
"license": "Apache-2.0",
4+
"version": "0.6.0",
5+
"description": "Microframework is a minimalistic framework you can use with TypeScript and JavaScript.",
6+
"license": "MIT",
77
"readmeFilename": "README.md",
88
"author": {
99
"name": "Umed Khudoiberdiev",
@@ -21,28 +21,31 @@
2121
"typescript",
2222
"typescript-framework"
2323
],
24-
"dependencies": {
25-
"configuration-manager": "~0.4.1",
26-
"find-root": "0.1.1",
27-
"path": "^0.12.7",
28-
"typedi": "~0.4.0"
29-
},
3024
"devDependencies": {
25+
"@types/express": "^4.0.35",
26+
"@types/node": "^7.0.5",
27+
"@types/winston": "^2.2.0",
28+
"ascii-art": "^1.4.2",
3129
"chai": "^3.4.1",
32-
"chai-as-promised": "^5.2.0",
33-
"del": "^2.0.2",
30+
"chai-as-promised": "^6.0.0",
31+
"del": "^2.2.2",
32+
"express": "^4.15.1",
3433
"gulp": "^3.9.1",
35-
"gulp-mocha": "^2.2.0",
34+
"gulp-mocha": "^4.0.1",
3635
"gulp-replace": "^0.5.4",
37-
"gulp-shell": "^0.5.0",
38-
"gulp-tslint": "^4.3.3",
39-
"gulpclass": "0.1.0",
40-
"mocha": "^2.3.4",
41-
"sinon": "^1.17.2",
36+
"gulp-shell": "^0.6.1",
37+
"gulp-tslint": "^7.1.0",
38+
"gulpclass": "0.1.2",
39+
"i": "^0.3.5",
40+
"mocha": "^3.2.0",
41+
"mysql": "^2.13.0",
42+
"npm": "^4.3.0",
43+
"sinon": "^1.17.7",
4244
"sinon-chai": "^2.8.0",
43-
"tslint": "^3.5.0",
44-
"tslint-stylish": "^2.1.0-beta",
45-
"typescript": "^1.8.7",
46-
"typings": "^0.7.7"
45+
"tslint": "^4.5.1",
46+
"tslint-stylish": "^2.1.0",
47+
"typeorm": "0.0.9",
48+
"typescript": "^2.2.1",
49+
"winston": "^2.3.1"
4750
}
4851
}

0 commit comments

Comments
 (0)