lib/nitcorn: add example and test for the restful annotation
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 29 Nov 2015 01:10:15 +0000 (20:10 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sun, 29 Nov 2015 05:15:52 +0000 (00:15 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/nitcorn/examples/.gitignore [new file with mode: 0644]
lib/nitcorn/examples/Makefile
lib/nitcorn/examples/src/restful_annot.nit [new file with mode: 0644]

diff --git a/lib/nitcorn/examples/.gitignore b/lib/nitcorn/examples/.gitignore
new file mode 100644 (file)
index 0000000..c704121
--- /dev/null
@@ -0,0 +1 @@
+src/restful_annot_gen.nit
index 9597941..d1bdeae 100644 (file)
@@ -1,7 +1,15 @@
-all:
+all: bin/restful_annot
        mkdir -p bin/
        ../../../bin/nitc --dir bin src/nitcorn_hello_world.nit src/simple_file_server.nit
 
 xymus.net:
        mkdir -p bin/
        ../../../bin/nitc --dir bin/ src/xymus_net.nit
+
+pre-build: src/restful_annot_gen.nit
+src/restful_annot_gen.nit:
+       ../../../bin/nitrestful -o $@ src/restful_annot.nit
+
+bin/restful_annot: src/restful_annot_gen.nit
+       mkdir -p bin/
+       ../../../bin/nitc -o $@ src/restful_annot_gen.nit
diff --git a/lib/nitcorn/examples/src/restful_annot.nit b/lib/nitcorn/examples/src/restful_annot.nit
new file mode 100644 (file)
index 0000000..3657031
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import nitcorn::restful
+
+class MyAction
+       super RestfulAction
+
+       # Method answering requests like `foo?s=some_string&i=42&b=true`
+       fun foo(s: String, i: Int, b: Bool): HttpResponse
+       is restful do
+               var resp = new HttpResponse(200)
+               resp.body = "foo {s} {i} {b}"
+               return resp
+       end
+
+       # Method answering requests like `bar?s=these_arguments_are_optional`
+       fun bar(s: nullable String, i: nullable Int, b: nullable Bool): HttpResponse
+       is restful do
+               var resp = new HttpResponse(200)
+               resp.body = "bar {s or else "null"} {i or else "null"} {b or else "null"}"
+               return resp
+       end
+end
+
+var vh = new VirtualHost("localhost:8080")
+
+# Serve everything with our restful action
+vh.routes.add new Route(null, new MyAction)
+
+# Avoid executing when running tests
+if "NIT_TESTING".environ == "true" then exit 0
+
+var factory = new HttpFactory.and_libevent
+factory.config.virtual_hosts.add vh
+factory.run