Skip to content
xllora edited this page Sep 13, 2010 · 12 revisions

This pages contains a list of examples describing different usages of Crochet. Please make sure you are familiar with Basic Crochet Concepts before reading this page. Information about how to get quickly started the examples in this section is provided on the QuickStart page. Also, examples are illustrated using get requests, but others HTTP methods work in the same manner (the only exception is the HEAD method that do not allow body definitions).

Path and regular expression examples

import crochet._
   new Crochet {
     get("/message") { 
         <html>
               <head><title>Hello World</title></head>
               <body><h1>Hello World!</h1></body>
         </html>
     }
   } on 8080

Creates a method that returns a simple HTML Hello World! message. The same method could also be defined as a regular expression as follows

import crochet._
new Crochet {
get(“^/message$”.r) {
<html>
<head><title>Hello World!</title></head>
<body><h1>Hello World!</h1></body>
</html>
}
} on 8080

The above example also responds to the same get request on the /message path as the previous one. However, since regular expressions are used, we can also extract elements from the path as the following example shows.

import crochet._
new Crochet {
get(“^/greet/([a-zA-Z]+)$”.r) {
<html>
<head><title>Hello {elements(0)}</title></head>
<body><h1>Hello {elements(0)}!</h1></body>
</html>
}
} on 8080

If you run paste the above example on the Scala console and make a get requests against http://localhost:8080/greet/John you will get a page back with the page content Hello John!. Each of the blocks matched in the regular expression of the path is added to the list represented by elements. In the previous example element is a list of size 1 containing the extracted string “John”.

Specifying the result MIME type

The first example listed in the QuickStart page showed and example of modifying the default response type.

import crochet._
new Crochet {
get(“/message”,“text/plain”) { “Hello World!” }
} serving “static_content” as “/static” on 8080

The above definition modifies the return type to text/plain instead of the default text/html. If specific not serializable return types with toString method, a valid option would be to direct write to the response object and return and empty string as the following example illustrates.

import crochet._
new Crochet {
get(“/message”,“text/plain”) { response.getWriter.print(“Hello World!”); "" }
} on 8080

Guards for conditional path activation

Guards are powerful tools to allow tailoring of responses for a given path.

import crochet._
   new Crochet {
     get("/message", {header("Host")!="localhost:8080"}) { 
         <html>
               <head><title>Hello World</title></head>
               <body><h1>Hello World from outside!</h1></body>
         </html>
     }
   } on 8080

The above example will return a Not Found response if http://localhost:8080/message accessed from the same host, and a “Hello World from outside!” message. An important constrain of using strings for defining the path is that multiple guards cannot be attached to the same path, since only the last definition will be kept. Path definitions based on strings are optimized for retrieval speed being stored under the hood in a hash map where the path is the key used for retrieval. However, overcoming these limitation can be achieved by defining the target path using regular expressions instead of strings as the example below illustrates.

import crochet._
new Crochet {
get(“^/message$”.r, {header(“Host”)==“localhost:8080”}) {
<html>
<head><title>Hello World!</title></head>
<body><h1>Hello Local World!</h1></body>
</html>
}
get(“&/message$”.r, {header(“Host”)!=“localhost:8080”}) {
<html>
<head><title>Hello World!</title></head>
<body><h1>Hello Outside World!</h1></body>
</html>
}
} on 8080

The above example presents the same path using the regular expression “^/message$”, but both of them are guarded by different conditions. The first one is only active if the request is made from the localhost. On the other hand, the second one is active if the request is not made by the localhost machine. If multiple guard for the same path are active, the first one listed is the one that will be invoked. Using guards, combined with regular expression, you are able to tailor responses for the same path requests, allowing to narrow down specific response based on the provided guard functions.

Custom authentication and authorization

A requested path can also be custom tailored to fit specialized authentication and authorization.

import crochet._
new Crochet {
get(“/message”, (path:String,user:Option[String])=>user!=None) {
<html>
<head><title>Hello {request.getRemoteUser}!</title></head>
<body><h1>Hello {request.getRemoteUser}!</h1></body>
</html>
}
} on 8080

Authentication and authorization can also be left to the application server. However on some particular API implementations it may come handy to provide specially tailored functions for custom authorization. The above example represents a simple example were “/message” access to /message path is only authorized if the function is satisfied.