Fast and Friendly Content Management System
A basic, smokin'-fast template-based mini-content management system (CMS) written in C with a MongoDB backend. Administration code is written in PHP.
Yet another CMS. It's perhaps a simple starting point for you if you are new to MongoDB.
FAF CMS Code available at github.
git@github.com:creamy/FAF-CMS.git
The frontend example code is a C interface to MongoDB using the Mongo C Driver. The PHP backend uses the PHP Native MongoDB Driver.
MongoDB is a highly scalable, ultra high performance open source NoSQL document server which stores records in binary JSON (BSON) format.
The C code is linked to mod_fcgid (FastCGI) running on Apache 2.2. FastCGI is a protocol that interfaces to web applications, enabling a highly scalable platform with minimal overhead. FastCGI loads and runs your CGI application in threads. Code outside FCGI_Accept() block is "static". It runs one time each session launch. Code inside the FCGI_Accept() block is dynamic, it runs with each client request.
A simple FastCGI example: Set variable "X" to "foo" outside the FCGI_Accept() block. When the thread is launched, the variable is initialized. Your web application continues to run in the Apache thread, waiting for a client request. If the app crashes, the thread dies, it is automatically restarted. Inside the FCGI_Accept() we read getenv for the request and respond appropriately. A client would "ask" for the value of "X", and the program would respond with "foo".
In the FAF CMS, the layout is loaded outside FCGI_Accept(), and the database connection to MongoDB is established. Upon a client request to the web server, the thread parses the REQUEST_URI set in the environment variable using getenv(), creates a cursor and queries the database for the corresponding document. The document is injected into the layout using a string replace function and returned to the client in the response. If the document does not exist it returns an error page.
The FAF CMS backend PHP script illustrate the simplicity and speed of using MongoDB. We create a web form with the following text input field names:
Simplified Form Code:
<form method="post" action="savepage.php"> Title <input type="text" name="q[title]" /><br /> Keywords <input type="text" name="q[keywords]" /><br /> Description <textarea name="q[description]"></textarea><br /> Content <textarea name="q[content]"></textarea><br /> <input type="submit" name="foo" value="Submit" />
The form data is stored in the array "q". We can retrieve the data and insert a new record into the database with two simple lines of PHP code.
$q=$_REQUEST['q']; if (is_array($q)) $m->save($q);
The save function will create a new unique identifier for the record. We don't have to worry about escaping and preparing SQL statements because the MongoDB format is binary JSON (BSON). The array is serialized and stored.
To access a record by the internal identifier (_id) we must first use MongoId(s) to convert string s into the internal MongoDB format.
Example:
$q = $pages->findOne(array('_id' => new MongoId($_REQUEST['id'])));
This retrieves the document associated with the id parameter in our request URI.
Use GNU GCC to compile the C source into a binay executable for the target machine. It may be helpful to save this configuration in a file such as "mk.sh" for quick re-build, i.e. if you update the source code.
mk.sh
gcc --std=c99 /home/user/software/mongodb-mongo-c-driver-68aa48e/src/*.c \ -Wall -I/home/user/software/mongodb-mongo-c-driver-68aa48e/src/ \ -I/usr/local/include -L/usr/local/lib -lfcgi Layout.c -o c
Replace "/home/user/software" with the path to your C Driver installation.
Further TODO: or really, things you can do for fun.
MongoDB
http://www.mongodb.org/
Mongo C Driver
https://github.com/mongodb/mongo-c-driver
http://www.mongodb.org/display/DOCS/C+Language+Center
http://api.mongodb.org/c/0.4/tutorial.html
NoSQL
http://nosql-database.org/
BSON - Binary JSON
http://bsonspec.org/
FastCGI (mod_fcgid)
http://httpd.apache.org/mod_fcgid/
http://www.fastcgi.com/
http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ch2c.htm
PHP - Personal Home Page
http://www.php.net/
http://php.net/manual/en/book.mongo.php
Git
http://git-scm.com/
https://github.com/
http://forums.freebsd.org/showthread.php?t=10810
Apache 2.2
http://httpd.apache.org/
Copyright 2011 Waitman Gobble · waitman@waitman.net