Waitman Gobble, San Jose California
Hello, I am experimenting with the "Database as a Program" methodology described by D. Richard Hipp in the article SQLite and Tcl
At the moment my application consists of a short and simple main.tcl which decides which code segment to load from an sqlite3 database, executes the code and outputs the result.
I'm using the websh wrapper CGI interface. Note: I would prefer to load mod_websh into Apache but it doesn't appear to work with 2.2.x, will investigate eventually ;-)
Here is some sample code:
The app will process a request URI like http://example.com/main.tcl/hello Strategy: Create a directory on the machine to create all the snippet Tcl files, then package into SQLite database
package.tcl (package all tcl scripts in current directory into SQLite database)
load /usr/local/lib/sqlite3/libtclsqlite3.so Sqlite3 sqlite3 db task.db db eval {CREATE TABLE hd(path TEXT,src TEXT)} set files glob *.tcl foreach x $files { set fp open $x r set src read $fp close $fp set path /$x db eval {INSERT INTO hd VALUES ($path,$src)} } db close
hello.tcl (hello world example)
set content "Hello, World"
main.tcl (process request in URI and load corresponding tcl code)
#!/bin/sh # \ exec /usr/local/bin/websh3.6.0b5 "$0" "$@" set fp open "layout.html" r set layout read $fp close $fp set path web::request PATH_INFO.tcl load /usr/local/lib/sqlite3/libtclsqlite3.so Sqlite3 sqlite3 db task.db set process db onecolumn {SELECT src FROM hd WHERE path=$path} eval $process db close set html regsub "<!--Content-->" $layout $content web::put $html
layout.html (a template)
<!doctype html> <html> <head> <title>boo</title> </head> <body> <!--Content--> </body> </html>
Sample output (note, in development ;-)
#1 display CGI env info
http://creamy.mobi/test-series/testdt/main.tcl/testcmd
testcmd.tcl
set request web::request -names set combine join $request ", " set path web::request PATH_INFO set content "<h1>Request Info</h1> <p>${combine}</p> <p>${path}</p> "
#2 load and display static HTML file. The static page is specified in the query string (parameter pg). For test purposes adds the page request at the top. NOTE: You have to modify glob line in package.tcl, above, to also package html files in the current directory.
http://creamy.mobi/test-series/testdt/main.tcl/static?pg=contact
static.tcl (load html)
web::dispatch -postdata "" -cmd "" set page /web::param pg.html set content "<h1>$page</h1>" append content db onecolumn {SELECT src FROM hd WHERE path=$page}
contact.html
<h1>Contact</h1> <p>Contact us page</p> <p>(650) 396-7580</p>