12.0.1 • Published 2 years ago

rapptor v12.0.1

Weekly downloads
705
License
-
Repository
github
Last release
2 years ago

Rapptor is a server framework that vastly simplifies use of hapi servers by leveraging easy-to-read YAML config files instead of Javascript. If you use the hapi library, the Rapptor framework is a must-have!

Features

  • Configure and manage server plugins with YAML
  • Automatically load and configure server routes from the routes directory
  • Automatically load and configure server methods from the methods directory
  • Configure logging from YAML
  • Configure server health monitoring and alerts
  • Track server metrics using Prometheus metrics.

Installation

npm install rapptor

or

yarn add rapptor

Usage:

Add the following to your package.json:

{
  "scripts": {
    "start": "rapptor"
  }
}

Then run: npm start to launch the server!

Directory Layout

The following is an example of the directory layout for a rapptor project:

/conf
│   ├── default.yaml/
│   ├── default-envs.yaml/
│   ├── dev.yaml/
/methods
│   ├── foo_method.js/
│   ├── auth_methods
│       ├── login.js/
│       ├── logout.js/
/plugins
│   ├── my_security_plugin.js/
/routes
├── api
│   ├── foo_route.js/
│   ├── bat/
│       ├── bif_route.js/
├── login.js
├── logout.js
/node_modules
│   ...
package.json

Configuration files are in /conf, routes are defined in the /routes directory, server methods are defined in /methods and server plugins are defined either in /plugins or installed automatically by npm in node_modules.

Configuring

All server configuration takes place in YAML files located in the /conf directory. At minimum you should have a conf/default.yaml file but you can have as many configuration files in /conf as you want.

File names beginning with default-.yaml form will always be loaded in the order they appear in the directory. Files lacking the _default- prefix will only be loaded if they match ENV.NODEENV. So you can have a _dev.yaml that only loads when NODEENV is dev and a _production.yaml that only loads when NODE_ENV is production.

Inside your configuration files you can access environment variables and create/access more complex variables inside YAML in a flexible manner with the double-bracket notation like this:

github:
  token: '{{ENV.GITHUB_TOKEN}}'
  endpoint: '{{ENV.GITHUB_REPO}}'
githubToken: '{{github.token}}'

This could cause problems if you forgot to define the correct environment variables in your shell, so for safety it is best to use the built-in env handlers like so:

aString: '{{getEnv("EXAMPLE_ENV_VARIABLE", "the backup value!")}}'
aBoolean: '{{truthyEnv("IS_TRUTHY")}}'

since this will set aValue to the string "the backup value!" if you didn't define EXAMPLE_ENV_VARIABLE and will coerce aBoolean to be either 'true' or 'false' depending on whether ENV.IS_TRUTHY was defined and is 'truthy'. For example if IS_TRUTHY is the literal string 'false' or the numeric -1 then it will be a boolean 'false' value.

More information on configuration file format is available at hapi-confi.

Adding a plugin

Plugins can be added and configured by listing them in a plugins: section like so:

plugins:
  my_security_plugin:
    authRoute: '/auth'
    keys:
      serverKey: '{{ENV.SERVER_API_KEY"}}'
      adminKey: '1234abc'

This will cause hapi to look for "mysecurity_plugin" in the _plugins directory and then in node_modules for a plugin named my_security_plugin. Once found it will be registered with your hapi server along with any of the underlying options. Note that plugins: could appear in any of your conf files, so you can have a default-plugins.yaml file just for listing your plugins, or have different plugins in a production.yaml file that will only load when you set NODE_ENV to production.

By default rapptor includes the following core plugins:

Adding a route

Create a file: routes/someroute.js and add the following:

exports.homepage = {
  path: '/',
  method: 'GET'
  handler(req, h) {
    return 'homepage!';
  }
}

Now if you launch rapptor and go to http://localhost:8080/ you should see homepage!.

For more information on configuring route loading see hapi-route-loader.

Adding a method

Add a file: methods/foo.js and then add the following:

module.exports = {
  method() {
    //some stuff
    this.log(['tag1', 'tag2'], "Hello World!");
  }
}

This will cause hapi to register foo() as a server method, so server.methods.foo() will be available in your code. Server methods will be bound to the server, so this.log is equivalent to server.log.

For more information on method loading in rapptor see hapi-method-loader.

Additional Features

Logging

Rapptor also includes facilities for logging in various formats and output media via the hapi-logr plugin. By default hapi-logr prints to console but can be configured to log to just about any output such as a Slack channel or text messages via SNS. For more information see the documentation at hapi-logr.

Healthcheck

Rapptor includes a built-in secure healthcheck plugin, which will register an endpoint at /health that is protected by ENV.HEALTH_TOKEN. Calling /health?token={HEALTH_TOKEN} will return basic metrics like server uptime, CPU level and current memory usage. This route can be polled at regular intervals from a remote script to ensure that your server is alive and available.

Metrics

Rapptor also includes metrics reporting via Prometheus. If ENV.ENABLE_PROM is set to true then the hapi-prom((https://github.com/firstandthird/hapi-prom) server plugin will automatically begin collecting default metrics using the prom-client package. These metrics are available at the /metrics route on your hapi server, for more info on how to configure hapi-prom see hapi-prom((https://github.com/firstandthird/hapi-prom).


A First + Third Project

12.0.1

2 years ago

12.0.0

3 years ago

11.0.0

4 years ago

10.0.0

4 years ago

9.1.0

4 years ago

9.0.3

5 years ago

9.0.2

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

8.4.1

5 years ago

8.2.0

5 years ago

8.1.1

5 years ago

8.1.0

5 years ago

8.0.0

5 years ago

7.6.1

5 years ago

7.6.0

5 years ago

7.5.0

5 years ago

7.4.0

5 years ago

7.3.1

5 years ago

7.3.0

5 years ago

7.2.0

5 years ago

7.1.0

5 years ago

7.0.0

6 years ago

6.1.0

6 years ago

6.0.0

6 years ago

5.16.0

6 years ago

5.15.0

6 years ago

5.14.8

6 years ago

5.14.7

6 years ago

5.14.6

6 years ago

5.14.5

6 years ago

5.14.4

6 years ago

5.14.3

6 years ago

5.14.2

6 years ago

5.14.1

6 years ago

5.14.0

6 years ago

5.13.2

6 years ago

5.13.1

6 years ago

5.13.0

6 years ago

5.12.0

6 years ago

5.11.1

6 years ago

5.11.0

6 years ago

5.10.1

6 years ago

5.10.0

6 years ago

5.9.0

6 years ago

5.8.0

6 years ago

5.7.1

6 years ago

5.7.0

6 years ago

5.6.0

6 years ago

5.5.1

6 years ago

5.5.0

6 years ago

5.4.1

6 years ago

5.4.0

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.2.0

6 years ago

4.1.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.6.0

6 years ago

3.5.1

6 years ago

3.5.0

6 years ago

3.4.0

6 years ago

3.3.0

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.0

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.17.0

7 years ago

1.16.0

7 years ago

1.15.2

7 years ago

1.15.1

7 years ago

1.15.0

7 years ago

1.14.0

7 years ago

1.13.0

7 years ago

1.12.0

7 years ago

1.11.1

7 years ago

1.11.0

7 years ago

1.10.0

7 years ago

1.9.0

7 years ago

1.8.2

7 years ago

1.8.1

7 years ago

1.8.0

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.20.0

8 years ago

0.19.1

9 years ago

0.19.0

9 years ago

0.18.2

9 years ago

0.18.1

9 years ago

0.18.0

9 years ago

0.17.0

9 years ago

0.16.1

9 years ago

0.16.0

9 years ago

0.15.0

9 years ago

0.14.0

9 years ago

0.13.0

9 years ago

0.12.0

9 years ago

0.11.1

9 years ago

0.11.0

9 years ago

0.9.0

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.0

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago

0.0.36

9 years ago

0.0.35

9 years ago

0.0.34

9 years ago

0.0.33

9 years ago

0.0.32

9 years ago

0.0.31

9 years ago

0.0.30

9 years ago

0.0.29

9 years ago

0.0.28

9 years ago

0.0.27

9 years ago

0.0.26

9 years ago

0.0.25

9 years ago

0.0.24

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago