handler.nim
author Mahlon E. Smith <mahlon@martini.nu>
Tue, 26 May 2015 15:40:11 -0700
changeset 0 f480e159f575
child 4 ffb8b9920057
permissions -rw-r--r--
Initial commit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
import
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
    mongrel2,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
    json,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
    re
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
let handler = newM2Handler( "app", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
var data    = %*[] ## the JSON data to "remember"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
proc demo( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
    ## This is a demonstraction handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
    ##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
    ## It accepts and stores a JSON data structure
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
    ## on POST, and returns it on GET.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
    # Create a response object for the current request.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
    var response = request.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
    case request.meth
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
    # For GET requests, display the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
    of "GET":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
        if request[ "Accept" ].match( re(".*text/(html|plain).*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
            response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
            response.body   = "Hi there.  POST some JSON to me and I'll remember it.\n\n" & $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
            response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
        elif request[ "Accept" ].match( re("application/json") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
            response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
            response.body   = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
            response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
        else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
            response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
    # Overwrite the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
    of "POST":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
        if request[ "Content-Type" ].match( re(".*application/json.*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
            try:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
                data = request.body.parse_json
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
                response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
                response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
                response.body = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
            except:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
                response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
                response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
                response.body = request.body
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
        else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
            response.body   = "I only accept valid JSON strings."
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
            response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
    else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
        response.status = HTTP_NOT_ACCEPTABLE
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
    return response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
# Attach the proc reference to the handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
handler.action = demo
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
# Start 'er up!
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
handler.run
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67