Create a file hello.luan containing:
%> Hello World <%
To run this, type luan hello.luan on the command line. This should print Hello World.
The syntax here is based on JSP. Let's change it a little:
local name = "Bob" %> Hello <%= name %> <%
This should print Hello Bob. Now let's try a more conventional approach:
local Io = require "luan:Io.luan" local print = Io.print print("Hello World")
In Luan, a function call with one string argument doesn't require parenthesis, so print("Hello World") is the same as print "Hello World" and require "luan:Io.luan" is the same as require("luan:Io.luan"). Both require and print are functions.
The require function takes a URI as an argument. Examples of URIs are "luan:Io.luan" and "file:hello.luan". require is used to import a module, which is returned from the require function call. In the case above, we assign the module to the local variable Io. The function print is a member of this module. We could have done Io.print("Hello World") but instead we chose to assign print to a local variable and use that to call the function.
Luan starts with only one defined function: require. You will use require to import whatever you need. This is a little more work, but makes it clear in each file where each function comes from.
Let's a make fancier version:
local Io = require "luan:Io.luan" local print = Io.print local function hello(name) print("Hello "..name) end hello("Bob")
The .. operator does concatenation. This will print Hello Bob.
Now let's make a web page. First we need a directory for our website. So create a directory src. In this directory, create a file hi.html.luan containing:
local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" return function() Io.stdout = Http.response.text_writer() %> <!doctype html> <html> <body> Hello World </body> </html> <% end
Now go back to the parent directory and do luan luan:http/serve.luan src. This will run the Luan web server on port 8080. Try going to http://localhost:8080/. You should see the directory. If you click on hi.html.luan you will see the source. But if you remove the .luan and just go to http://localhost:8080/hi.html then you will run the program which will generate the web page. In fact the page that you are currently looking at tutorial.html is generated from tutorial.html.luan.
The Luan webserver expects the file to return a function and calls it to generate the page. Code of the form %>...<% writes its output to Io.stdout which by default is the standard output of the command line. So in the returned function one usually starts by setting Io.stdout to a
text_writer which writes its output to the HTTP response (to the web browser).
So now you have built your website and you want to publish it to the web. If you have your own domain, create a CNAME record for it pointing to s1.luan.software. If you don't have a domain, just use a domain like bob.s1.luan.software (anything of the form *.s1.luan.software). Assuming your directory is src and you will use the password secret, do the following from the command line:
luan luan:host/push.luan bob.s1.luan.software secret src
The form is luan luan:host/push.luan domain password directory. If you change your site, just run this again and your site will be updated. To delete your site, do luan luan:host/delete.luan domain password.
Hopefully this short tutorial gave you an idea of how to use Luan to make a website.