这是 gwan + mongoDB 的一个小示例
首先:安装mongoDB c驱动(https://github.com/mongodb/mongo-c-driver)
$ git clone https://github.com/mongodb/mongo-c-driver.git
$ cd mongo-c-driver
$ scons
$ sudo scons install
它将安装在 /usr/local 文件夹中,如果需要,您需要对其进行调整,或者只需将生成的文件复制到 /usr/lib 和 /usr/include/
然后以 root 身份运行 ldconfig
$ sudo ldconfig
一旦安装了 mongodb c 驱动程序,只需尝试这个简单的示例:
// ============================================================================
// C servlet sample for the G-WAN Web Application Server (http://trustleap.ch/)
// ----------------------------------------------------------------------------
// mongodb.c: querying mongoDB from G-WAM
// ============================================================================
#pragma link "mongoc"
#pragma link "bson"
#include <mongo.h>
#include <bson.h>
#include "gwan.h" // G-WAN exported functions
int main(int argc, char *argv[])
{
xbuf_t *reply = get_reply(argv);
mongo conn[1];
int status = mongo_client( conn, "localhost", 27017 );
if( status != MONGO_OK ) {
switch ( conn->err ) {
case MONGO_CONN_NO_SOCKET: printf( "no socket\n" ); return 1;
case MONGO_CONN_FAIL: printf( "connection failed\n" ); return 1;
case MONGO_CONN_NOT_MASTER: printf( "not master\n" ); return 1;
}
}
int i = 0;
for(i=0; i<100;i++) {
bson b[1];
bson_init( b );
bson_append_new_oid( b, "_id" );
bson_append_string( b, "name", "Joe" );
bson_append_int( b, "age", 33 );
bson_finish( b );
mongo_insert( conn, "tutorial.people", b, NULL );
bson_destroy( b );
}
xbuf_xcat(reply, "Hello Mongo !! %d %d", status, i);
return 200;
}
// ============================================================================
// End of Source Code
// ============================================================================
享受 :)