Build template-able web page and more.
Start the httpd in debug mode:
$ busybox httpd -f -vvv -c ./httpd.conf
See config:
$ cat httpd.conf:
I:test.tt
*.tt:/usr/local/bin/haserl
/:duke:$1$lsqrABnV$g1231mabcDIsnR4mFUW1F/
- Line 1 set the default page, default is index.html
- Line 2 set xxx.tt run through an interpreter
- Line 3 set / requested user name & passwd, user name here is duke, and the passwd is general by command 'busybox httpd -m passwd-string'
only the line 2 is all we need in this article, more busybox httpd usage can be found in busybox source code: ./busybox-1.21.0/networking/httpd.c
Haserl is a small program that uses shell or Lua script to create cgi web scripts. It is intended for environments where PHP or ruby are too big.
$ haserl -v
This is haserl version 0.9.30 (http://haserl.sourceforge.net)
basic usage:
$ cat test.tt
#!/usr/local/bin/haserl
Content-Type: text/html
<html>
<body>
<% if [ $REQUEST_METHOD == "POST" ]; then %>
<% if [ $POST_twins == "sagill" ]; then %>
correct
<% else %>
error
<% fi %>
<% else %>
<form method="post" action="./test.tt" >
twins: <input type="text" name="twins"/><br/>
<input type="submit" value="Submit" />
</form>
<% fi %>
</body>
</html>
Tips 1, dynamic header & contents
#!/usr/local/bin/haserl
Content-Type: application/json
[{"a": "aa"}, {"b": "bb"}]
the Content-Type normally must on the top without blank line, and the contents must after at least one blank line. if we want to dynamic generation the Content-Type, we try:
#!/usr/local/bin/haserl
<% if [ 1 == 1 ]; then %>
Content-Type: application/json
[{"a": "aa"}, {"b": "bb"}]
<% else %>
Content-Type: text/html
normal test...
<% fi %>
but we got empty header, and the Content-Type is parsed as contents, you can checkout this using build-in debug console in chrome (Ctrl + Shift + c).
the issue can be solved in this way:
#!/usr/local/bin/haserl
<% if [ 1 == 1 ]; then %>
<% echo Content-Type: application/json %>
[{"a": "aa"}, {"b": "bb"}]
<% fi %>
if you want to dynamic generation the contents at the same time:
#!/usr/local/bin/haserl
<% if [ 1 == 1 ]; then %>
<% echo Content-Type: application/json %>
<% echo '[{"a": "aa"}, {"b": "bb"}]' %>
<% fi %>
this time, we got empty contents, and the contents we echo is parsed as header. the correct one is:
#!/usr/local/bin/haserl
<% if [ 1 == 1 ]; then %>
<% echo Content-Type: application/json %>
<% echo "" %>
<% echo '[{"a": "aa"}, {"b": "bb"}]' %>
<% fi %>
there are more than one line outputs, you can clean those blank line by:
<% echo -n '[{"a": "aa"}, {"b": "bb"}]'; exit %>
Tips 2, using template write template wrapper:
$ cat main-layout.tt
#!/usr/local/bin/haserl
Content-Type: text/html
<html>
<body>
<% haserl --accept-none "./tpl$REQUEST_URI" %>
</body>
</html>
write the sub page in the sub spl dir:
$ cat spl/page1.tt
#!/usr/local/bin/haserl
<p>this is sub page1</p>
then link the main-layout.tt to same name sub page in top dir:
$ ln -s main-layout.tt page1.tt
now the tree is:
$ tree
.
├── httpd.conf
├── page1.tt -> tpl-main-layout.tt
├── tpl
│ └── page1.tt
└── tpl-main-layout.tt
now access /page.tt we got:
<html>
<body>
<p>this is sub page1</p>
</body>
</html>
Tips 3, background work if you want start some work at the background, you may try:
#!/usr/local/bin/haserl
Content-Type: text/html
some text
<% sleep 30 & %>
but the page is pending until 30 seconds past. using tool nohup, screen or something else can solve this:
<% nohup sleep 30 > /dev/null & %>
http://manpages.ubuntu.com/manpages/bionic/man1/haserl.1.html