Help, in vibe.d, how to get configure var of mongodb in heroku?

MichaelBi shunjie.bi at gmail.com
Fri May 13 06:23:41 UTC 2022


On Friday, 13 May 2022 at 06:12:01 UTC, rikki cattermole wrote:
>
> Okay that is fine, now we need to see where showData is being 
> called.

thanks, here's the code:

import vibe.vibe;
import std.process;
import std.conv : to;

void main()
{
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
         settings.bindAddresses = ["0.0.0.0"];
         readOption("port|p", &settings.port, "Sets the port used 
for serving HTTP.");
         readOption("bind-address|bind", 
&settings.bindAddresses[0], "Sets the address used for serving 
HTTP.");
	auto router = new URLRouter;
	router.get("*", serveStaticFiles("public/"));
   	router.registerWebInterface(new ContentController);
	auto listener = listenHTTP(settings, router);

	scope (exit)
	{
		listener.stopListening();
	}

	runApplication();
}

class ContentController
{
	void index()
	{
		render!("index.dt", showData());
	}
}

struct Camera{
	@name("_id") BsonObjectID id; // represented as "_id" in the 
database
	string brand;
	string model;
}

Camera[] showData(){
	auto uri = environment.get("MONGODB_URI");
         MongoClient conn = connectMongoDB(uri);
         MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");
         MongoCollection cameras = eqpdb["cameras"];
	Camera[] cs;
         auto results = cameras.find();
         foreach(rec;results) cs ~= deserializeBson!Camera(rec);
	return cs;
}


More information about the Digitalmars-d-learn mailing list