Source Code

gyokuro is a framework written in Ceylon, similar to Sinatra and Spark, for creating web applications with very little boilerplate. It is based on the Ceylon SDK and uses ceylon.http.server.

Platform: Java
Packages
net.gyokuro.core
net.gyokuro.core.http

Default documentation for package net.gyokuro.core.http.

net.gyokuro.core.internal
net.gyokuro.core.json
Dependencies
ceylon.collection1.3.2
ceylon.http.server1.3.2
ceylon.io1.3.2
ceylon.json1.3.2
ceylon.logging1.3.2
java.base7
net.gyokuro.transform.api0.3
net.gyokuro.view.api0.3
Aliases
AnyTemplateSource Codeshared AnyTemplate<T>
=> Anything(TemplateRenderer<T>, Request, Response)

A template that can be called by a TemplateRenderer.

HandlerSource Codeshared Handler<Params>
=> Function<Anything,Params>|Anything(Request, Response)

A function capable of handling a request.

TemplateSource Codeshared Template=> AnyTemplate<String>
WSHandlerSource Codeshared WSHandler=> Anything(WebSocketChannel, String)|WebSocketHandler

A handler that can react to WebSocket events.

Annotations
controllerSource Codeshared ControllerAnnotation controller()

Declares a class or an object as a controller, allowing routes to be scanned.

See also route()
routeSource Codeshared RouteAnnotation route(String path, {AbstractMethod+} methods = ...)

Declares a partial path associated to a class or a function. Routes declared on a class will be concatenated with its member routes. For example, the following code will result in a route /foo/bar:

route("foo")
controller class MyController() {
    route("bar")
    void hello() { }
}
Parameters:
  • methods = { get, post }
sessionSource Codeshared SessionAnnotation session()

Declares that a handler parameter should be retrieved from the current HTTP session instead of GET/POST data. If no value can be retrieved from the session, a 400 response will be sent back to the client.

ControllerAnnotationSource Codeshared final ControllerAnnotation

The annotation class for the controller() annotation.

RouteAnnotationSource Codeshared final RouteAnnotation

The annotation class for the route() annotation.

SessionAnnotationSource Codeshared final SessionAnnotation

The annotation class for the session() annotation.

Values
mimeParseSource Codeshared mimeParse mimeParse

Adapted from MIMEParse.

Functions
bindSource Codeshared [String, Package|{Object*}] bind(Package|{Object*} pkgToScan, String context = ...)

Tells gyokuro to bind controllers scanned in pkgToScan to the given context root. This function is meant to be used for Application.controllers.

value app = Application {
    controllers = bind("rest", `package com.myapp.controllers`);
};
Parameters:
  • context = "/"
clearRoutesSource Codeshared void clearRoutes()

Clears every registered route.

connectSource Codeshared void connect<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new CONNECT route for the given path and handler.

deleteSource Codeshared void delete<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new DELET route for the given path and handler.

getSource Codeshared void get<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new GET route for the given path and handler.

haltSource Codeshared Nothing halt(Integer errorCode, String? message = null)

Interrupts the current handler immediately, resulting in an HTTP response with code errorCode and a body equal to message.

This can be used for example to indicate that something was not found in the database:

shared void findAuthor(Integer authorId) {
    value author = authorDao.findById(authorId) 
        else halt(404, "Author not found");
}
Parameters:
  • message = null
Source Codeshared void head<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new HEAD route for the given path and handler.

newFlashSource Codeshared Flash newFlash(Session session)

Creates a new instance of a Flash. You shouldn't have to use this function directly unless you are creating a custom TemplateRenderer. If you want to access a Flash instance from a handler, use parameters injection instead:

route("/login")
shared void login(Flash flash) {
    if (loginOk()) {
        flash.add("info", "You have been logged in");
        redirect("/");
    }
}
optionsSource Codeshared void options<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new OPTIONS route for the given path and handler.

patchSource Codeshared void patch<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new PATCH route for the given path and handler.

postSource Codeshared void post<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new POST route for the given path and handler.

putSource Codeshared void put<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new PUT route for the given path and handler.

redirectSource Codeshared Nothing redirect(String url, Integer redirectCode = ...)

Interrupts the current handler immediately, and asks the client browser to redirect to the specified url.

shared void login(String username, String password) {
    if (exists user = ...) {
        session.put("user", user);
        redirect("/");
    }
    ...
}
Parameters:
  • redirectCode = 303
renderSource Codeshared void render<T>(T template, Map<String,Anything> context = ..., String contentType = ..., Charset charset = ...)(TemplateRenderer<T> renderer, Request request, Response response)

Renders a template that will be returned as the response body.

Parameters:
  • template

    The template name

  • context = emptyMap

    A map of things to pass to the template.

  • contentType = "text/html"

    The content type to be used in the response.

  • charset = utf8

    The charset to be used in the response.

serveSource Codeshared String[2] serve(String path, String context = "")

Tells gyokuro to serve static assets located in the filesystem folder path under the given context root. For example, all routes starting with /public will serve files located in ./assets:

value app = Application {
    assets = serve("assets", "/public");
};
Parameters:
  • context = ""
traceSource Codeshared void trace<Params>(String path, Handler<Params> handler)
given Params satisfies Anything[]

Declares a new TRACE route for the given path and handler.

websocketSource Codeshared void websocket(String path, WSHandler handler)

Registers a new web socket handler for the given path. The handler can be a simple 'onText' function, or a more advanced WebSocketHandler.

Interfaces
FlashSource Codeshared Flash

A holder for special messages stored in the session, meant to be used exactly once. Flash messages are removed from the session as soon as they are accessed.

Classes
ApplicationSource Codeshared Application<T>

A web server application that can route requests to handler functions or annotated controllers, and serve static assets.

WebSocketHandlerSource Codeshared abstract WebSocketHandler

A handler for WebSockets that reacts to advanced events.

mimeParseSource Codeshared mimeParse

Adapted from MIMEParse.