src/mongrel2.nim
changeset 6 d49437ff8f2f
parent 4 ffb8b9920057
equal deleted inserted replaced
5:c568094abee7 6:d49437ff8f2f
       
     1 # vim: set et sta sw=4 ts=4 :
     1 #
     2 #
     2 # Copyright (c) 2015-2018, Mahlon E. Smith <mahlon@martini.nu>
     3 # Copyright (c) 2015-2021, Mahlon E. Smith <mahlon@martini.nu>
     3 # All rights reserved.
     4 # All rights reserved.
     4 # Redistribution and use in source and binary forms, with or without
     5 # Redistribution and use in source and binary forms, with or without
     5 # modification, are permitted provided that the following conditions are met:
     6 # modification, are permitted provided that the following conditions are met:
     6 #
     7 #
     7 #     * Redistributions of source code must retain the above copyright
     8 #     * Redistributions of source code must retain the above copyright
   134 ##   handler.run
   135 ##   handler.run
   135 ##
   136 ##
   136 
   137 
   137 
   138 
   138 import
   139 import
   139     json,
   140     std/json,
   140     strutils,
   141     std/strutils,
   141     tables,
   142     std/tables,
   142     times,
   143     std/times,
   143     tnetstring,
   144     tnetstring,
   144     zmq
   145     zmq
   145 
   146 
   146 type
   147 type
   147     M2Handler* = ref object of RootObj
   148     M2Handler* = ref object of RootObj
   148         handler_id:         string
   149         handler_id:         string
   149         request_sock:       TConnection
   150         request_sock:       ZConnection
   150         response_sock:      TConnection
   151         response_sock:      ZConnection
   151         action*:            proc ( request: M2Request ): M2Response
   152         action*:            proc ( request: M2Request ): M2Response
   152         disconnect_action*: proc ( request: M2Request )
   153         disconnect_action*: proc ( request: M2Request )
   153 
   154 
   154     M2Request* = ref object of RootObj
   155     M2Request* = ref object of RootObj
   155         sender_id*:   string  ## Mongrel2 app-id
   156         sender_id*:   string  ## Mongrel2 app-id
   618     result                   = request.response
   619     result                   = request.response
   619     result[ "Content-Type" ] = "text/html"
   620     result[ "Content-Type" ] = "text/html"
   620     result.body              = DEFAULT_CONTENT
   621     result.body              = DEFAULT_CONTENT
   621 
   622 
   622 
   623 
       
   624 proc handle_disconnect( request: M2Request ): void =
       
   625     ## This is the default disconnect handler, if the caller didn't install one.
       
   626     return
       
   627 
       
   628 
   623 proc run*( handler: M2Handler ) {. noreturn .} =
   629 proc run*( handler: M2Handler ) {. noreturn .} =
   624     ## Enter the request loop conversation with Mongrel2.
   630     ## Enter the request loop conversation with Mongrel2.
   625     ## If an action() proc is attached, run that to generate
   631     ## If an action() proc is attached, run that to generate
   626     ## a response.  Otherwise, run the default.
   632     ## a response.  Otherwise, run the default.
   627     while true:
   633     while true:
   634 
   640 
   635         # Ignore disconnects unless there's a separate
   641         # Ignore disconnects unless there's a separate
   636         # disconnect_action.
   642         # disconnect_action.
   637         #
   643         #
   638         if request.is_disconnect:
   644         if request.is_disconnect:
   639             if not isNil( handler.disconnect_action ):
   645             if isNil( handler.disconnect_action ):
   640                 discard handler.disconnect_action
   646                 handler.disconnect_action = handle_disconnect
   641             continue
   647             handler.disconnect_action( request )
   642 
   648 
   643         # Defer regular response content to the handler action.
   649         # Defer regular response content to the handler action.
   644         #
   650         #
   645         if isNil( handler.action ):
   651         if isNil( handler.action ):
   646             handler.action = handle_default
   652             handler.action = handle_default
   657             request.meth,
   663             request.meth,
   658             request.uri
   664             request.uri
   659         ]
   665         ]
   660 
   666 
   661         echo "$1: $2 --> $3 $4" % [
   667         echo "$1: $2 --> $3 $4" % [
   662             $(get_localtime(getTime())),
   668             $(get_time()),
   663             info,
   669             info,
   664             $( response.status ),
   670             $( response.status ),
   665             HTTPCODE[ response.status ].label
   671             HTTPCODE[ response.status ].label
   666         ]
   672         ]
   667 
   673