Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.

gServ Request URL Action Matching

Lee Collins edited this page Apr 17, 2015 · 1 revision

#Basic URL Matching By Example

Matching Static Pattern

###Given

new GServ().http {
    get "/books/inventory", { -> 
        write "text/plain", "Here is the inventory."
    } 
}.start(8080);

###Match Examples

http://yourhost:8080/books/inventory

Matching Pattern with Variables

###Given

new GServ().http {
    get "/books/:id", { bookId -> 
        write "text/plain", "Getting book $bookId."
    } 
}.start(8080);

###Match Examples

http://yourhost:8080/books/24
http://yourhost:8080/books/45.45.34.55
http://yourhost:8080/books/anything

Matching Pattern with Type Constrained Variables

###Given - 1

new GServ().http {
    get "/books/:id:Number", {Double bookId ->
        write "text/plain", "BookId is a Number."
    }
}.start(8080);

###Notes The value 'bookId' will be of type Double.

###Match Examples

http://yourhost:8080/books/24
http://yourhost:8080/books/45.45
http://yourhost:8080/books/76667645.343233546

###Given - 2

new GServ().http {
    get "/books/:id:Integer", {Integer bookId ->
        write "text/plain", "BookId is an Integer."
    }
}.start(8080);

###Notes The value 'bookId' will be of type Integer.

###Match Examples

http://yourhost:8080/books/24
http://yourhost:8080/books/45
http://yourhost:8080/books/766676

Matching Pattern with Variables Conforming to Regular Expression

###Given

new GServ().http {
    get "/bySSN/:ssn:`\\d\\d\\d\\-\\d\\d\\-\\d\\d\\d\\d`" , { ssn ->
        write "text/plain", "SSN is $ssn."
    }
}.start(8080);

###Notes The regular expression in surounded in backtick characters (`). The backslashes need to be doubled so the compiler will know you mean a literal back slash and not a control character.

###Match Examples

http://yourhost:8080/byssn/123-54-4343
http://yourhost:8080/byssn/000-00-0000

Matching Pattern with Static Query

###Given

new GServ().http {
    get "/books/all?region=UK" , {  ->
        write "text/plain", "Books with Uk Region."
    }
}.start(8080);

###Notes The query paramewters are matched verbatim. Any request without query value (region=UK) will NOT match.

###Match Examples

http://yourhost:8080/books/all?region=UK
http://yourhost:8080/books/all?region=US -- FAILS

Matching Pattern with Variable Query

###Given

new GServ().http {
    get "/books/all?region=:region" , { region ->
        write "text/plain", "Books with $region Region."
    }
}.start(8080);

###Notes Any request without query parameter 'region' will NOT match. The parameter value is passed to the closure.

###Match Examples

http://yourhost:8080/books/all?region=UK
http://yourhost:8080/books/all?region=XX
http://yourhost:8080/books/all?region=ANY_THING

Matching Pattern with variable and variable Query

###Given

new GServ().http {
    get "/books/:id:Integer?region=:region" , { bookId, region ->
        write "text/plain", "Book $bookId in region $region."
    }
}.start(8080);

###Notes Variable values are passed in-order to the handler closure.

###Match Examples

http://yourhost:8080/book/25?region=90
http://yourhost:8080/book/77777?region=XX
http://yourhost:8080/book/10101010?region=ANY_THING
http://yourhost:8080/book/0?region=ANY_THING
Clone this wiki locally