Differences between revisions 5 and 6
Revision 5 as of 2009-09-20 21:45:11
Size: 1352
Editor: localhost
Comment: converted to 1.6 markup
Revision 6 as of 2018-04-12 14:13:10
Size: 0
Editor: JoanTouzet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#redirect How_to_serve_applications
## page was renamed from ServingAppsFromCouchDb
You can write applications that are written entirely in HTML/CSS and JavaScript and that is stored within CouchDB document attachments. Here's a short script that makes it easy to wrap up a bunch of files and put them into a database.

This is a quick'n'dirty hack. All you need to do is fill the {{{$files}}} array with entries of {{{$file}}} arrays that contain the filename and content-type of that file.

Run with:
$ curl -X PUT http://server:5984/database/document -d "\`php upload.php\`"

{{{
<?php
$file["name"] = "main.html";
$file["contentType"] = "text/html";
$files[] = $file;

$file["name"] = "thread.html";
$file["contentType"] = "text/html";
$files[] = $file;

$file["name"] = "styles.css";
$file["contentType"] = "text/css";
$files[] = $file;

$file["name"] = "DetectWebkit.js";
$file["contentType"] = "application/javascript";
$files[] = $file;

foreach($files AS $file) {
 $file["data"] = base64_encode(file_get_contents($file["name"]));
 
 $attachments[] = <<<EOF
  "$file[name]": {
   "content-type":"$file[contentType]",
   "data":"$file[data]"
  }
EOF;
}

$attachments = implode(",\n\n", $attachments);

$document = <<<EOF
{
 "_attachments": {
  $attachments

 }
}

EOF;
echo $document;
?>
}}}