handler.nim
changeset 4 ffb8b9920057
parent 0 f480e159f575
equal deleted inserted replaced
3:ecde1a332692 4:ffb8b9920057
     1 
     1 
     2 import
     2 import src/mongrel2
     3     mongrel2,
       
     4     json,
       
     5     re
       
     6 
     3 
     7 let handler = newM2Handler( "app", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" )
     4 let handler = newM2Handler( "mlist", "tcp://127.0.0.1:9019", "tcp://127.0.0.1:9018" )
     8 var data    = %*[] ## the JSON data to "remember"
       
     9 
     5 
    10 
     6 proc woo( request: M2Request ): M2Response =
    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
     7     var response = request.response
    19 
     8 
    20     case request.meth
     9     response[ "Content-Type" ] = "text/plain"
    21 
    10     response.body   = "Hi there."
    22     # For GET requests, display the current JSON structure.
    11     response.status = HTTP_OK
    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
    12     return response
    60 
    13 
    61 
    14 # handler.action = woo
    62 # Attach the proc reference to the handler action.
       
    63 handler.action = demo
       
    64 
       
    65 # Start 'er up!
       
    66 handler.run
    15 handler.run
    67 
    16