-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This guide will take you through Leverage's concepts and provide you with enough information to use the library efficiently. If you find anything confusing and/or missing, please don't hesitate to file an issue.
Leverage was created to allow any developer to create an application by using a common interface. Application configuration should be:
-
Simple
-
As declarative as possible
-
Easy to change later
With these ideas in mind, let's take a look at the structure of Leverage:
NOTE: The above image is a simplification and ignores both Services and Middleware because they can be more complex and will be introduced later.
Plugins and Components are kinds of Units in Leverage. A Leverage Unit is a small, single purpose object which is intended to be composed together with other Units in order to form a full application. In order to compose Units together, Leverage provides a Manager:
The Leverage Manager is provided in the @leverage/core
package and will handle gluing the rest of your application together. Leverage's manager functions as the base of your application that we will then build on top of. All other pieces of your app will be handed off to the manager so that it can work its magic!
Now let's talk briefly about the Plugin and Component Units:
A Leverage Plugin defines some kind of interaction or process. For example, a very common plugin would be the HTTP plugin available from the @leverage/plugin-http
package. This plugin defines how one would receive and respond to HTTP requests.
A Leverage Component defines a specific interaction within a plugin's process. Were we to create a component for a HTTP plugin, this specific interaction would be handling one HTTP request at a given route.
With the general concepts understood, let's put together a very simple Hello World application:
Note: We will be needing both the core library (@leverage/core
) as well as a HTTP plugin (@leverage/plugin-http
) for this example
npm install --save @leverage/core @leverage/plugin-http
Now that we have the required packages installed, let's start writing our application by importing them:
// We only need Leverage's manager in this example
import { Manager } from '@leverage/core';
import HTTP from '@leverage/plugin-http';
import { Manager } from '@leverage/core';
import HTTP from '@leverage/plugin-http';
/*====== NEW CODE ======*/
const manager = new Manager();
const http = new HTTP();
import { Manager } from '@leverage/core';
import HTTP from '@leverage/plugin-http';
const manager = new Manager();
const http = new HTTP();
/*====== NEW CODE ======*/
const component = {
// Let the manager know that this object is a component
is: 'component',
// Configuration for this component
config: {
// Let the manager know what plugin this component is intended to be used in
type: 'http',
// Configuration that the HTTP plugin will use to set up our request handling
http: {
// In this example, we'll respond to all requests using this one component
path: '*',
// We'll also let the HTTP component know we are listening for `GET` requests
method: 'get',
},
},
// Now, we can create a callback that will handle our HTTP requests
http (request, response) {
// Send the user a "Hello World" message!
response.send('Hello World');
}
};
import { Manager } from '@leverage/core';
import HTTP from '@leverage/plugin-http';
const manager = new Manager();
const http = new HTTP();
const component = {
is: 'component',
config: {
type: 'http',
http: {
path: '*',
method: 'get',
},
},
http (request, response) {
response.send('Hello World');
}
};
/*====== NEW CODE ======*/
// `Manager.add` can be given any number of objects to add to our application
manager.add(http, component);
import { Manager } from '@leverage/core';
import HTTP from '@leverage/plugin-http';
const manager = new Manager();
const http = new HTTP();
const component = {
is: 'component',
config: {
type: 'http',
http: {
path: '*',
method: 'get',
},
},
http (request, response) {
response.send('Hello World');
}
};
manager.add(http, component);
/*====== NEW CODE ======*/
// Tell the HTTP server to start listening on a port
http.listen(3000);
Open up localhost:3000 in your browser to see the result!
If you are ready to learn about Leverage's structures in depth, start here.