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