Latest commit 1b8a42d Jul 20, 2016 @weaver-viii weaver-viii committed on GitHub Update README.adoc
Permalink
..
Failed to load latest commit information.
.ceylon Cleanup Jun 24, 2016
source/examples Cleanup Jun 24, 2016
.gitignore Update with staging repo URL and simplify a few examples Feb 4, 2016
README.adoc Update README.adoc Jul 20, 2016

README.adoc

Vert.x Ceylon examples

or Ceylon Vert.x examples - as you like.

Vert.x Stack

This current stack of modules is supported:

For more info please refer to http://vertx.io/docs/

Usage from Ceylon

From the "ceylon" directory. Only once.

> ceylon compile examples.utils
Note: Created module examples.utils/1.0.0
> ceylon compile examples.core.http.simple.server
Note: Created module examples.core.http.simple.server/1.0.0

> ceylon run examples.core.http.simple.server/1.0.0
deployed

The module is deployed from the Ceylon run using Vert.x API

shared void run() {
    vertx.vertx().deployVerticle("ceylon:examples.core.http.simple.server/1.0.0", void (String|Throwable ar) {
    if (is String ar) {
      print("deployed");
    } else {
      ar.printStackTrace();
    }
  });
}

Usage from Vert.x

Download Vert.x from http://vertx.io/download, add $VERTX_HOME/bin to your path. Then edit the vertx-stack.json and set the included boolean to true for the vertx-lang-ceylon dependency. Ceylon support is not included by default as it breaks the on-demand Java compilation. Once edited, launch:

vertx resolve --dir=lib

It will download vertx-lang-ceylon and its dependencies.

Them run a Ceylon Verticle from Vert.x as follows:

> vertx run ceylon:source/examples/core/http/simple/server

Vert.x will compile first the Ceylon verticle.

When the verticle is already compiled (i.e there is a modules/examples/core/http/simple/server/1.0.0/examples.core.http.simple.server-1.0.0.car file), it can be ran too:

> vertx run ceylon:examples.core.http.simple.server/1.0.0

Some examples requires the webroot folder, like the web/staticsite example or the template examples, the vertx.cwd must be set to the parent of the _webroot`, for example:

Core Simple Http

A very simple HTTP server which always responds with the same response:

You can run the server then open a browser and point it at http://localhost:8080

And a simple HTTP client which makes a request to the server.

  • Vert.x

> vertx run ceylon:source/examples/core/http/simple/server &
> vertx run ceylon:source/examples/core/http/simple/client
  • Ceylon

> ceylon compile examples.core.http.server
> ceylon compile examples.core.http.client
> ceylon run examples.core.http.simple.server &
> ceylon run examples.core.http.simple.client

Core Http simple form upload

This example demonstrates how you can handle file uploads from an HTML form submission.

You can run the server then open a browser and point it at http://localhost:8080

Note
In practice you would probably also use Vert.x-Web for this rather than writing a server at this low level. Vert.x-Web provides built in support for HTML forms and file uploads, and avoids some of the security issues due to maliciously crafted URI paths.
  • Vert.x

> vertx run ceylon:source/examples/core/http/simpleformupload
  • Ceylon

> ceylon compile examples.core.http.simpleformupload
> ceylon run examples.core.http.simple.simpleformupload

Core Websockets

This example shows a Vert.x HTTP server which handles websockets connections. This example simply echoes back to the client whatever it receives on the websocket.

There’s also a client which connects to the server, sends some data and logs out what it receives.

You can run the server then open a browser and point it at http://localhost:8080

Note
in practice you would probably use Vert.x-Web to build a web application that uses WebSockets
  • Vert.x

> vertx run ceylon:source/examples/core/http/websockets/server &
> vertx run ceylon:source/examples/core/http/websockets/client
  • Ceylon

> ceylon compile examples.core.http.websockets.server
> ceylon compile examples.core.http.websockets.client
> ceylon run examples.core.http.websockets.server &
> ceylon run examples.core.http.websockets.client

Core exec blocking

This example demonstrates how you can include blocking code in with your non blocking code in a way that doesn’t block an event loop:

Run the example then open a browser and point it at http://localhost:8080

  • Vert.x

> vertx run ceylon:source/examples/core/execblocking
  • Ceylon

> ceylon compile examples.core.execblocking
> ceylon run examples.core.http.execblocking

Core event bus pub/sub

This example demonstrates publish / subscribe messaging between a receivers and a sender. With pub/sub messaging you can have multiple subscribers who all receive messages from publishers.

A receiver listens on an address on the event bus for incoming messages. When it receives a message it logs it.

The sender sends a message to that address every second, when it receives a reply it logs it.

At the command line you should run Sender and Receiver in different consoles using the -cluster flag:

> vertx run ceylon:source/examples/core/eventbus/pubsub/receiver -cluster &
> vertx run ceylon:source/examples/core/eventbus/pubsub/sender -cluster

The -cluster flag allows different Vert.x instances on the network to cluster the event bus together into a single event bus.

Web Hello World

The traditional hello world example. This one creates a server which just responds with "Hello World! to each request.

  • Vert.x

> vertx run ceylon:source/examples/web/helloworld
  • Ceylon

> ceylon compile examples.web.helloworld
> ceylon run examples.web.helloworld

Web Rest

  • Vert.x

> vertx run ceylon:source/examples/web/rest
  • Ceylon

> ceylon compile examples.web.rest
> ceylon run examples.web.rest

Web Simple REST Micro-service

Vert.x-Web is a great fit for HTTP/REST microservices.

Here’s a simple micro-service example which implements an API for a product catalogue.

The API allows you to list all products, retrieve details for a particular product and to add a new product.

Product information is provided in JSON.

List all products

GET /products

Get a product

GET /products/<product_id>

Add a product

PUT /products/<product_id>

Run the server then open your browser and hit list products to start playing with the API.

  • Vert.x

> vertx run ceylon:source/examples/web/rest
  • Ceylon

> ceylon compile examples.web.rest
> ceylon run examples.web.rest

Web Static web server example

This example shows a very simple web server which serves static files from disk.

  • Vert.x

> vertx run ceylon:source/examples/web/staticsite -Dvertx.cwd=source/examples/web/staticsite
  • Ceylon

> ceylon compile examples.web.staticsite
> ceylon run examples.web.staticsite

Web Form

This example shows a basic HTML form web-site and a backend end point that just returns an customizable hello world message.

Run, then open your browser and hit link:http://localhost:8080 and click around the links

  • Vert.x

> vertx run ceylon:source/examples/web/form
  • Ceylon

> ceylon compile examples.web.form
> ceylon run examples.web.form

Web JDBC example

This example shows a basic REST server backed by a JDBC client. It is exactly the same as the REST client however its data is persisted in a relational database using the asynchronous JDBC client.

Run, then open your browser and hit link:http://localhost:8080/products to get the list of products, or link:http://localhost:8080/products/0 for accessing a product with id 0. In order to create new products use the POST method to link:http://localhost:8080/products

  • Vert.x : Copy the HSQLDB driver in $VERTX_HOME/lib (the C3P0 connection pool needs it), then run it

> vertx run ceylon:source/examples/web/jdbc
  • Ceylon: Need the option --flat-classpath

> ceylon compile examples.web.jdbc
> ceylon run --flat-classpath examples.web.jdbc

Web static site with templating

This example shows a simple web-site containing some static pages and also a page dynamically generated using templates.

The dynamic page outputs some information (path and headers) of the request. It uses the MVEL template engine but you could use any of the other template engines if you prefer.

Run, then open your browser and hit link:http://localhost:8080 and click on the links

Web templating with Jade

> vertx run ceylon:source/examples/web/templating/jade -Dvertx.cwd=source/examples/web/templating/jade
  • Ceylon

> ceylon compile examples.web.templating.jade
> ceylon run --flat-classpath  examples.web.templating.jade

Web templating with Handlebars

> vertx run ceylon:source/examples/web/templating/handlebars -Dvertx.cwd=source/examples/web/templating/handlebars
  • Ceylon

> ceylon compile examples.web.templating.handlebars
> ceylon run --flat-classpath  examples.web.templating.handlebars

Web templating with MVEL

> vertx run ceylon:source/examples/web/templating/mvel -Dvertx.cwd=source/examples/web/templating/mvel
  • Ceylon

> ceylon compile examples.web.templating.mvel
> ceylon run --flat-classpath  examples.web.templating.mvel

Web templating with Thymeleaf

> vertx run ceylon:source/examples/web/templating/thymeleaf -Dvertx.cwd=source/examples/web/templating/thymeleaf
  • Ceylon

> ceylon compile examples.web.templating.thymeleaf
> ceylon run --flat-classpath  examples.web.templating.thymeleaf

Embeded Http

Simple http server example without verticles, Ceylon only.

  • Ceylon

> ceylon compile examples.embed.http
> ceylon run examples.embed.http