Skip to content

akalend/libscgi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The libscgi is lightweight framework realise the scgi prootocol http://en.wikipedia.org/wiki/SCGI It is simple scgi server based on libevent. You can use as backend to Apache/lighttpd/nginix ( http://nginx.org )

The field of use is lightweight scripts as quickly find, script of autocomplete, online dictionary.

Configure nginx scgi_module: 

	location /dictionary {
		scgi_pass localhost:8080;
		include scgi_params;
	 }
You can to change host/port.

The example of  code:

/**
* the cackback handler
* example Users logic
*/
class Handler: IScgiHandler {

	void run(map< string,string > * parms, char * buffUot) {
		string parm = getParam("REQUEST_METHOD",parms);
		// if method is POST 
		if ( parm == "POST" ) {
			// return to WEB-client the POST data
			strcpy(buffUot, getParam("POST_DATA",parms).c_str());
			return;	
		}
		// or return to WEB-client the QUERY_STRING parameter
		strcpy(buffUot, getParam("QUERY_STRING",parms).c_str());		  
	}
};


int main(int argc, char **argv)
{

	scgiServer scgi;

	if (scgi.checkPid()) {
		cerr << "the pid file exist or daemon already started\n" << endl;
		return 1;
	}
	
	if ((pid = scgi.demonize()) < 1) {
		if (pid == -1) {
			cerr << "demonize error\n";
			return 1;
		}	
		return 0;
	}
	
	if (scgi.init("127.0.0.1", 8080) ) {
		cerr << "server stopped\n";
		return 1;
	}
	
	scgi.addHandler("/dictionary",  reinterpret_cast<IScgiHandler *>(new Handler()));
	scgi.run();
}

INSTALL

make liby - compile the scgi-library
make  - compile the scgi-bin example
make ac - compile the city autocompile programm


City Autocomplete programm (as example)

The practical example is city autocomplete. See source of the autocomplete.cpp.
The autocomplete program read the city names file. The format is csv: city lowcase name or shortname, city official name, range, id. The example:

"new-york","New York",25,1267
"newyork","New York",25,1267
"washington",""Washington",10,1121
etc...

The range is sort order variable. For example:
Moscow - is Russian capital, must be first in the padding list, beginning with letter "m".
Mogilev - is region center city.
Malinovka - is country, but if show by abc, the name "Malinovka" will showed before Moscow. Using range variable , we creale ordered list: Moscow,Mogilev,Malinovka.

The run script: ./server /path/to/cityNameFile.csv

Input data: send post data (one-two-three symbols from word) to url http://hostname/cityes
The output is in json format: {"cityes" : [ { "name": "New York", "id":90},{ "name": "New Vasyki", "id":808},{ "name": "Nazareth", "id":1505},{ "name": "Neaple", "id":1965}],"time":304}

The limitation of the output is ten items. This limitation can change by definition constant MAXITEMS in source before compilation. The "time" parametr is processing time for search an range in microseconds.


LICENSE
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 






















About

the lightweight scgi server framework based on libevent

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages