nitcorn: intro RestfulTask
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 14 Oct 2016 13:29:10 +0000 (09:29 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 3 Feb 2017 22:45:03 +0000 (17:45 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/nitcorn/restful.nit

index 886d31b..e46bf13 100644 (file)
@@ -54,6 +54,7 @@ module restful is new_annotation(restful)
 
 import nitcorn
 import json
+import pthreads
 
 # Action with `restful` methods
 class RestfulAction
@@ -82,3 +83,30 @@ class RestfulAction
                return obj
        end
 end
+
+# Thread dedicated to a single `request`
+abstract class RestfulTask
+       super Task
+
+       # Type of `action`
+       type A: RestfulAction
+
+       # Receiver action
+       var action: A
+
+       # Request that created this thread
+       var request: HttpRequest
+
+       # Server handling the `request`
+       var http_server: HttpServer
+
+       # Indirection to the real method in `action`
+       protected fun indirect_restful_method: HttpResponse is abstract
+
+       redef fun main
+       do
+               var response = indirect_restful_method
+               http_server.respond response
+               http_server.close
+       end
+end