Quick Start
This page will help you install Lever OS and show you how to write your first Lever service!
Prerequisites
- Docker 1.11+ and Docker Compose 1.7+. On a Mac you can install Docker Toolbox to get what you need.
- Make
- Linux or Mac (Windows should work too but it was never tested)
If you need to use docker-machine to run docker (eg on a Mac), you also need to install VirtualBox and then run these commands to get started:
$ docker-machine create --driver virtualbox default
$ eval `docker-machine env default`
You will need to run the second command for every new terminal window.
Installation
$ git clone https://github.com/leveros/leveros
$ cd leveros
$ make
$ sudo make install-cli
$ make fastrun
The commands above pull the necessary Docker images, install the lever
CLI and run a Lever OS instance locally.
Your first Lever service
$ mkdir hello
$ cd hello
module.exports.sayHello = function (name, callback) {
callback(null, "Hello, " + name + "!");
};
{
"name": "helloService",
"description": "A hello service.",
"jsEntry": "server.js"
}
Deploy your service locally
$ lever deploy
This takes the whole current directory, archives it and deploys it onto Lever in dev.lever
, the default environment.
Invoke via CLI
$ lever invoke lever://dev.lever/helloService/sayHello '"world"'
"Hello, world!"
# Or even shorter (only for dev.lever)...
$ lever invoke /helloService/sayHello '"world"'
"Hello, world!"
Remember to use proper JSON for arguments. This includes the quotes for strings.
Invoke via HTTP POST request
# Without docker-machine
$ curl -H "Content-Type: application/json" -X POST -d '["world"]' \
http://127.0.0.1:8080/helloService/sayHello?forceenv=dev.lever
"Hello, world!"
# With docher-machine
$ curl -H "Content-Type: application/json" -X POST -d '["world"]' \
http://$(docker-machine ip default):8080/helloService/sayHello?forceenv=dev.lever
"Hello, world!"
Notice the forceenv
query param at the end of the command. This is a convenience feature that allows you to access Lever's HTTP API without having dev.lever
assigned to the listening IP (and also configuring Lever to serve on port 80
).
Invoke from browser (via HTTP API)
$.ajax({
'type': 'POST',
'url': 'http://127.0.0.1:8080/helloService/sayHello?forceenv=dev.lever',
'contentType': 'application/json',
'data': JSON.stringify(["world"]),
'dataType': 'json',
'success': function (data) {
console.log(data); // Hello, world!
},
});
Note that when using docker-machine, you need to replace 127.0.0.1
with the output of the command docker-machine ip default
.
Invoke from Node
$ npm install leveros
var leveros = require('leveros');
var client = new leveros.Client();
client.forceHost = process.env.LEVEROS_IP_PORT;
var service = client.service('dev.lever', 'helloService');
service.invoke('sayHello', "world", function (error, reply) {
console.log(reply); // Hello, world!
});
# Without docker-machine
$ LEVEROS_IP_PORT="127.0.0.1:8080" node client.js
# With docher-machine
$ LEVEROS_IP_PORT="$(docker-machine ip default):8080" node client.js
Setting LEVEROS_IP_PORT is necessary so that you can invoke the dev.lever
environment without adding an entry for it in /etc/hosts
and setting the listen port to 80
.
What's next?
Now that you have your first Lever service up and running, learn more about what you can do with Lever by taking a look at the Concepts page.
Later, you might also want to check out the API reference pages. See links on left-hand side.
Updated less than a minute ago