Ceylon HTTP server
One of the exciting new features coming to the Ceylon SDK in M5
is the HTTP server contributed by Matej Lazar. The HTTP server
forms part of the module ceylon.net
and is based on
Undertow, a fairly
new project that will serve up HTTP in future versions of JBoss.
The API is still evolving, but I can give you a taste of it with a little hello world program.
import ceylon.net.httpd { createServer, Endpoint, Response, Request }
void runServer() {
//create a HTTP server
value server = createServer {
//an endpoint, on the path /hello
Endpoint {
path = "/hello";
//handle requests to this path
service(Request request, Response response) =>
response.writeString("hello world");
}
};
//start the server on port 8080
server.start(8080);
}
Or, if you're the kind of person who gets their kicks out of squeezing things into one line:
import ceylon.net.httpd { createServer, Endpoint, Response, Request }
void runServer() =>
createServer { Endpoint("/hello",
(Request request, Response response) =>
response.writeString("hello world")) }
.start(8080);
(No, FTR, I would never write it like that.)
Oh, I almost forgot, you'll also need to import ceylon.net
in
your module descriptor:
module hellohttp '1' {
import ceylon.net '0.5';
}
Of course, what you don't need is any kind of XML metadata or build script to package stuff up into deployment archives or copy archives to a deployment directory.
And it starts up in an instant, right from the commandline or IDE.