From 320313b611594eb25c71d5992112d07c7456fe8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Thu, 24 Jul 2014 15:04:45 -0400 Subject: [PATCH] examples: add two nitcorn examples MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- examples/nitcorn/Makefile | 3 ++ examples/nitcorn/src/file_server_on_port_80.nit | 52 ++++++++++++++++++ examples/nitcorn/src/nitcorn_hello_world.nit | 65 +++++++++++++++++++++++ examples/nitcorn/www/hello_world/dir/a.txt | 1 + examples/nitcorn/www/hello_world/dir/b.txt | 1 + examples/nitcorn/www/hello_world/favicon.ico | Bin 0 -> 1150 bytes tests/sav/file_server_on_port_80.res | 4 ++ 7 files changed, 126 insertions(+) create mode 100644 examples/nitcorn/Makefile create mode 100644 examples/nitcorn/src/file_server_on_port_80.nit create mode 100644 examples/nitcorn/src/nitcorn_hello_world.nit create mode 100644 examples/nitcorn/www/hello_world/dir/a.txt create mode 100644 examples/nitcorn/www/hello_world/dir/b.txt create mode 100644 examples/nitcorn/www/hello_world/favicon.ico create mode 100644 tests/sav/file_server_on_port_80.res diff --git a/examples/nitcorn/Makefile b/examples/nitcorn/Makefile new file mode 100644 index 0000000..96a6ee3 --- /dev/null +++ b/examples/nitcorn/Makefile @@ -0,0 +1,3 @@ +all: + mkdir -p bin/ + ../../bin/nitg --dir bin src/nitcorn_hello_world.nit src/file_server_on_port_80.nit diff --git a/examples/nitcorn/src/file_server_on_port_80.nit b/examples/nitcorn/src/file_server_on_port_80.nit new file mode 100644 index 0000000..cf91d9f --- /dev/null +++ b/examples/nitcorn/src/file_server_on_port_80.nit @@ -0,0 +1,52 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014 Alexis Laferrière +# +# 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. + +# Basic file server on port 80, usually requires `root` to execute +# +# To be safe, it is recommended to run this program with its own username: +# `sudo file_server -u nitcorn:www` +module file_server_on_port_80 + +import nitcorn +import privileges + +# Prepare options +var opts = new OptionContext +var opt_drop = new OptionUserAndGroup.for_dropping_privileges +opt_drop.mandatory = true +var opt_help = new OptionBool("Print this message", "--help", "-h") +opts.add_option(opt_drop, opt_help) +opts.parse(args) + +# Check options errors and help +if not opts.errors.is_empty or opt_help.value then + print opts.errors + print "Usage: file_server [Options]" + opts.usage + exit 1 +end + +# Serve everything with a standard FilesHandler +var vh = new VirtualHost("localhost:80") +vh.routes.add new Route(null, new FileServer("www/hello_world/")) +var factory = new HttpFactory.and_libevent +factory.config.virtual_hosts.add vh + +# Drop to a low-privileged user +var user_group = opt_drop.value +if user_group != null then user_group.drop_privileges + +factory.run diff --git a/examples/nitcorn/src/nitcorn_hello_world.nit b/examples/nitcorn/src/nitcorn_hello_world.nit new file mode 100644 index 0000000..fd7fe73 --- /dev/null +++ b/examples/nitcorn/src/nitcorn_hello_world.nit @@ -0,0 +1,65 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014 Alexis Laferrière +# +# 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. + +# Hello World Web server example +# +# The main page, `index.html`, is served dynamicly with `MyAction`. +# The rest of the Web site fetches files from the local directory +# `www/hello_world/`. +module nitcorn_hello_world + +import nitcorn + +class MyAction + super Action + + redef fun answer(http_request, turi) + do + var response = new HttpResponse(200) + var title = "Hello World from Nitcorn!" + response.body = """ + + + + + + {{{title}}} + + +
+

{{{title}}}

+

See also a directory.

+
+ +""" + return response + end +end + +var vh = new VirtualHost("localhost:8080") + +# Serve index.html with our custom handler +vh.routes.add new Route("/index.html", new MyAction) + +# Serve everything else with a standard `FileServer` with a root at "www/hello_world/" +vh.routes.add new Route(null, new FileServer("www/hello_world/")) + +# 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 diff --git a/examples/nitcorn/www/hello_world/dir/a.txt b/examples/nitcorn/www/hello_world/dir/a.txt new file mode 100644 index 0000000..8e512cf --- /dev/null +++ b/examples/nitcorn/www/hello_world/dir/a.txt @@ -0,0 +1 @@ +aaaAAAAAaaaa diff --git a/examples/nitcorn/www/hello_world/dir/b.txt b/examples/nitcorn/www/hello_world/dir/b.txt new file mode 100644 index 0000000..55fe151 --- /dev/null +++ b/examples/nitcorn/www/hello_world/dir/b.txt @@ -0,0 +1 @@ +BBBBBBbbbbbbbbbbbbbbb diff --git a/examples/nitcorn/www/hello_world/favicon.ico b/examples/nitcorn/www/hello_world/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..0f66f25afd4fb7834c5997839e918979fb2d0024 GIT binary patch literal 1150 zcmc(fK@LDr3`9p_E3vX>=V-3t)U@O!%_J5=_~O5IrmfF{)Wp^Iihb15N`s_1?vO$` z^?($fuW9YJx9%-g&VtM(hv=0@lWoj0`SQHXhs-C3=#}aD`ttepEMLa{&VT0hdlN&> p