123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import vibe.appmain;
- import vibe.d;
- import std.random;
- const worldSize = 10000;
- const fortunesSize = 100;
- const mongoUrl = "mongodb://127.0.0.1/";
- MongoClient mongo;
- MongoCollection worldCollection;
- MongoCollection fortunesCollection;
- shared static this()
- {
- mongo = connectMongoDB( mongoUrl );
- worldCollection = mongo.getCollection( "hello_world.World" );
- fortunesCollection = mongo.getCollection( "hello_world.Fortunes" );
- auto router = new URLRouter;
- router.get("/plaintext", &plaintext);
- router.get("/json", &json);
- router.get("/db", &db);
- router.get("/queries", &queries);
- router.get("/generate-world", &generateWorld);
- router.get("/generate-fortunes", &generateFortunes);
- router.get("/", staticTemplate!"index.dt");
- auto settings = new HTTPServerSettings;
- settings.port = 8080;
- listenHTTP(settings, router);
- }
- void json(HTTPServerRequest req, HTTPServerResponse res)
- {
- auto helloWorld = Json([
- "message" : *new Json( "Hello, World!" )
- ]);
- res.writeJsonBody( helloWorld );
- }
- void generateWorld(HTTPServerRequest req, HTTPServerResponse res)
- {
- try {
- worldCollection.drop();
- } catch( Exception error ) {}
- for( auto i = 0 ; i < worldSize ; ++i ) {
- worldCollection.insert([
- "_id": i + 1,
- "randomNumber": uniform( 0 , worldSize )
- ]);
- }
- res.writeBody( "Generated" );
- }
- void generateFortunes(HTTPServerRequest req, HTTPServerResponse res)
- {
- try {
- fortunesCollection.drop();
- } catch( Exception error ) {}
- for( uint i = 0 ; i < worldSize ; ++i ) {
- fortunesCollection.insert([
- "_id": new Bson( i + 1 ),
- "message": new Bson( to!string( uniform( 0 , fortunesSize ) ) )
- ]);
- }
- res.writeBody( "Generated" );
- }
- void db(HTTPServerRequest req, HTTPServerResponse res)
- {
- auto data = worldCollection.findOne([
- "_id": uniform( 1 , worldSize + 1 )
- ]);
- res.writeJsonBody( data );
- }
- void queries(HTTPServerRequest req, HTTPServerResponse res)
- {
- auto count = 1;
- try {
- count = to!uint( req.query["queries"] );
- if( !count ) {
- count = 1;
- } else if( count > 500 ) {
- count = 500;
- }
- } catch( Exception error ) { }
-
- auto data = new Bson[ count ];
- for( uint i = 0 ; i < count ; ++i ) {
- data[i] = worldCollection.findOne([
- "_id": uniform( 1 , worldSize + 1 )
- ]);
- }
- res.writeJsonBody( data );
- }
- void plaintext(HTTPServerRequest req, HTTPServerResponse res)
- {
- res.writeBody("Hello, World!");
- }
|