npm package: @hugobyte/microbootstrap
Description: Microbootstrap is a minimalistic framework you can use with TypeScript and JavaScript to bootstrap micro loaders.
Author: Muhammed Irfan K
Latest version: 0.1.2

# Microbootstrap

> This project is a fork of [microframework](https://github.com/typestack/microframework)


Microbootstrap is a simple framework that allows you to execute and configure your modules in a proper order, helping you to organize bootstrap code in your application.

## Usage

First, install microbootstrap:

```
npm i @hugobyte/microbootstrap
```

Second, create a simple "loader" named `expressLoader`.
Loader is a place where you can configure all your modules during bootstrap.
All loaders are executed one by one in a sequential order.

```typescript
import { MicrobootstrapSettings } from '@hugobyte/microbootstrap';

export function expressLoader(settings: MicrobootstrapSettings) {
  // create express app
  const app = express();

  // register all routes, Routes are just routes that should be stored outside of this module
  const routes: any = Routes;
  Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath]));

  // run application to listen on given port
  app.listen(3000);

  // your loader also can return a promise and microbootstrap will wait for it in a proper order
}
```

Create `app.ts` and bootstrap a microbootstrap and your express loader:

```typescript
import { Bootstrap } from '@hugobyte/microbootstrap';
import { expressLoader } from './expressLoader';

Bootstrap([expressLoader])
  .then(() => console.log('Application is up and running.'))
  .catch(error => console.log('Application is crashed: ' + error));
```

That's all. You can do same for other loaders.
Take a look on sample to understand better how concept of loaders and their bootstrapping in microbootstrap.

## Settings

You can specify additional options to microbootstrap.

- `logo` - Logo needs to be used before application launches. To use logo ansi-art node module should be installed.
- `showBootstrapTime` - If set to true then framework shows how much time was spend to bootstrap microbootstrap.
- `bootstrapTimeout` - Number of milliseconds to wait before framework will bootstrap.

Example of using settings:

```typescript
import { Bootstrap } from '@hugobyte/microbootstrap';
import { expressLoader } from './expressLoader';

Bootstrap({
  config: {
    logo: 'MyApp',
    showBootstrapTime: true,
    bootstrapTimeout: 10,
  },
  loaders: [
    expressLoader,
    // ...
  ],
})
  .then(() => console.log('Application is up and running.'))
  .catch(error => console.log('Application is crashed: ' + error));
```

## Sharing data across loaders

Sometimes few loaders need to communicate between each other and use shared data.
For such purpose you can store the data in `settings` object passed to each loader
and use stored data across all other loaders. For example:

```typescript
import { MicrobootstrapSettings } from '@hugobyte/microbootstrap';

export function expressLoader(settings: MicrobootstrapSettings) {
  // create express app
  const app = express();

  // register all routes, Routes are just routes that should be stored outside of this loader
  const routes: any = Routes;
  Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath]));

  // run application to listen on given port
  app.listen(3000);

  settings.setData('express_app', app);
}
```

And another loaders can use data this way:

```typescript
import { MicrobootstrapSettings } from '@hugobyte/microbootstrap';

export function socketIoLoader(settings: MicrobootstrapSettings) {
  const io = io();
  io.useExpress(settings.getData('express_app'));
}
```

## Application Shutdown

In the case if you want to shutdown running application you need to do following:

```typescript
import { Bootstrap } from '@hugobyte/microbootstrap';
import { expressLoader } from './expressLoader';

Bootstrap({
  config: {
    logo: 'MyApp',
    showBootstrapTime: true,
    bootstrapTimeout: 10,
  },
  loaders: [
    expressLoader,
    // ...
  ],
})
  .then(framework => {
    // do something before shutdown

    // and shutdown everything
    return framework.shutdown();
  })
  .then(() => {
    // now everything is turned off
  })
  .catch(error => console.log('Application is crashed: ' + error));
```

All loaders which use resources should release them, for example:

```typescript
export async function typeormModule(settings: MicrobootstrapSettings) {
  const connection = await createConnection({
    driver: {
      type: 'mysql',
      host: 'localhost',
      username: 'test',
      password: 'test',
      database: 'test',
    },
  });

  settings.onShutdown(() => connection.close()); // closing connection on microbootstrap shutdown
}
```