Version 4 of Toolatra

Updated 2019-11-07 00:13:42 by escargo

https://raw.githubusercontent.com/timkoi/toolatra/master/logo.png

Toolatra

Toolatra is a modern Sinatra -like micro web framework for writing HTTP Tcl web applications.

Features

  • Sinatra-like DSL
  • does not depend on anything, except for Tcllib (though, Toolatra works only with Tcl 8.5 and newer)
  • a built-in template engine and layout engine
  • a built-in web server that easily integrates with Apache or nginx

Some examples

Hello world

app.tcl code:

#!/usr/bin/env tclsh
package require Toolatra

get / {
     render {Hello world!}
}

get /+name {
     render "Hello, [dict get $params name]!"
}

post / {
     if {$rawData != {}} {
         render "Hello, $rawData (via POST request)!" 
     } else {
         render {Hello world (via POST request)!} 
     }
}

run

How to run:

$ tclsh8.5 app.tcl

The app will be running at http://localhost:5050

  • If you go to http://localhost:5050 via your web browser, you should see Hello world!
  • If you go to http://localhost:5050/Tim via your web browser, you should see Hello, Tim!
  • If you send an empty POST request to http://localhost:5050 , you should get Hello world (via POST request)!
  • If you send a POST request with Jane sent as the data, you should get Hello, Jane (via POST request)!

Cat Language Translator

Both the current and the previous version of Cat Language Translator are written 100% in Tcl and Toolatra. The source code of the first version can be found here: https://github.com/timkoi/catlanguage-web

Serving Toolatra web apps with Apache (on Linux)

First, specify the port where Toolatra's HTTP server shall be running. In the application, specify the port as the argument to the run command, like this:

run 5056

Replace 5056 with your preferred port number.

Now, create a user and a systemd service for the web app:

$ sudo useradd -M -N toolatrarunner
$ sudo chsh -s /sbin/nologin toolatrarunner
$ sudo mkdir -pv /srv/mytoolatraapp
$ mv -v * /srv/mytoolatraapp
$ sudo chown -v -R toolatrarunner:users /srv/mytoolatraapp
$ cat >> /etc/systemd/system/mytoolatraapp.service << EOF
[Unit]
Description=My Toolatra web app
After=httpd.service

[Service]
Type=simple
ExecStart=/bin/sh -c "cd /srv/mytoolatraapp && LC_ALL=C /usr/bin/env tclsh app.tcl"
User=toolatrarunner

[Install]
WantedBy=multi-user.target
EOF
$ sudo systemctl enable mytoolatraapp
$ sudo systemctl start mytoolatraapp

Obviously, replace mytoolatraapp with the short name of your Toolatra web app.

Now create a reverse proxy with Apache:

$ sudo sh
$ echo 'ProxyPreserveHost On' >> /etc/httpd/conf/httpd.conf
$ echo 'ProxyPass /mytoolatraapp/ http://localhost:5056/' >> /etc/httpd/conf/httpd.conf
$ echo 'ProxyPassReverse /mytoolatraapp/ http://localhost:5056/' >> /etc/httpd/conf/httpd.conf
$ systemctl restart httpd
$ exit

Obviously, replace 5056 with the port you've specified and mytoolatraapp with the name of your web app.

That's it!

Links