When technosaurus posted his nifty note-taking html server/client app, I found myself using it quite a bit. Recently it occurred to me that this idea could be expanded to include databases.....
A database seems somewhat complicated - decide on a schema, enter the structure, make up a form for data entry, set up a query operation.
Why not just make up an html form which actually creates and could also query a database?
So here is a simple prototype called "form_db".
Decide on your fields, grab some form html coding (lots of easy on-line form builders and templates available) and just paste the code into Form_db. Now you can just fill in the form and a json database is created and additional entries can be made. To query the database you could have several predefined buttons (each with a "case" statement) to return information based on fields. You could even make a text area to accept a complex query with JQ.
At first I thought I'd have to use Python or maybe Javascript to parse url and read/write json files. But then again, maybe all that could be done in bash and it turns out that it could (although probably lots easier in Python). Now that a Json database has been created, how might it be queried and used in this environment? I then discovered JQ, described as an awk/sed for json here-
http://stedolan.github.io/jq/
Next, how to show the json query return in the open html page?
Handily, there is a javascript widget to display json data. Good, everything we need.
Here's a simple contact form example with a single query button that returns all the contact names.
JQ has extensive ways to return information, including math expressions.
You'll find two scripts which go in $HOME/cgi-bin:
query_string # parses the url and directs action
form_db.cgi # browser form
Also attached is jq below which I compiled in Puppy Precise 5.4.3 and stripped - weighs in at only 247k
To set this up, do the following in a terminal:
Code: Select all
mkdir $HOME/cgi-bin
cd $HOME
httpd
Install "jq.tar.gz" to /
Place the scripts "query_string" and "form_db.cgi" in $HOME/cgi-bin and make executable.
Then point your browser at url "localhost/cgi-bin/form_db.cgi"
Here's a pic (of course, you could add css styling to make this as decorative as you'd like)
Cheers,
s
Code: Select all
#!/bin/sh
# place script in $HOME/cgi-bin/form_db.cgi
# called by browser "localhost/cgi-bin/form_db.cgi"
# seaside 2/9/2014
# form html code follows:
echo '
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h2> Contacts </h2>
<form id="contacts-form" action="/cgi-bin/form_db.cgi">
<body bgcolor="#E6E6FA">
<label>First name:</label>
<div class="field"><input type="text" name="first_name" autofocus /></div>
<label>Last name:</label>
<div class="field"><input type="text" name="last_name" /></div>
<label>Email:</label>
<div class="field"><input type="text" name="email" /></div>
</div>
<input type="radio" name="gender" value="Male" checked> Male
<input type="radio" name="gender" value="Female"> Female
<div class="item button button-default"><br>
<div class="field"><input type="submit" id="contacts-op-save" value="Save" /></div>
<input type="submit" name="id_entry" value="Query All" />
</div>
</div>
</form>'
if [ "${QUERY_STRING/id_entry=Query}" = "${QUERY_STRING}" ] ; then
./query_string # submit item
else # it's a query
input=`cat /root/cgi-bin/contact.json | jq -c '.'`
echo '<!DOCTYPE html>
<script src="http://metawidget.org/js/3.4/metawidget-core.min.js"></script>
<script type="text/javascript">
var body = '$input'
</script>
<div id="metawidget"></div>
<script type="text/javascript">
var mw = new metawidget.Metawidget( document.getElementById( "'metawidget'" ));
mw.toInspect = body;
mw.buildWidgets();
</script>
</html>'
fi
Code: Select all
#!/bin/sh
# place script in $HOME/cgi-bin/query_string
# parses QUERY_STRING from form_db.cgi
# seaside 2/9/2014
[ -z "$QUERY_STRING" ] && exit
QUERY_STRING=$(echo -e $(sed -e 's/%/\\x/g' -e 's/+/ /g' <<<"$QUERY_STRING"))
if [ ! -f $HOME/cgi-bin/contact.json ]; then
oldIFS=$IFS
IFS="&"
jstring='[{'
for item in $QUERY_STRING; do
key='"'${item%=*}'"'
value='"'${item#*=}'"'
jstring="$jstring $key:$value,";
done
IFS=$oldIFS
jstring="${jstring%,}}]" # cut comma, add }]
echo "$jstring" >$HOME/cgi-bin/contact.json
else # additional object
json=`cat $HOME/cgi-bin/contact.json`
json="${json%]}," # cut ] add comma
oldIFS=$IFS
IFS="&"
json_add='{'
for item in $QUERY_STRING; do
key='"'${item%=*}'"'
value='"'${item#*=}'"'
json_add="$json_add $key:$value,";
done
IFS=$oldIFS
json_add="${json_add%,}}]"
echo "$json$json_add" >$HOME/cgi-bin/contact.json
fi