Thread overview
Help, in vibe.d, how to get configure var of mongodb in heroku?
May 13, 2022
MichaelBi
May 13, 2022
rikki cattermole
May 13, 2022
MichaelBi
May 13, 2022
rikki cattermole
May 13, 2022
MichaelBi
May 13, 2022
rikki cattermole
May 13, 2022
MichaelBi
May 13, 2022
rikki cattermole
May 13, 2022
MichaelBi
May 13, 2022
rikki cattermole
May 13, 2022

there are online documents of heroku on how to access config var value from code, and several code samples without D. link here: https://devcenter.heroku.com/articles/config-vars#accessing-config-var-values-from-code.

i have code here:
    auto uri = environment.get("MONGODB_URI");
    MongoClient conn = connectMongoDB(uri);
    MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");

the "MONGODB_URI" showed above already put into heroku's app config as the 'key' and there is a paired 'value'. so the uri is to extract the value which is to use for app to establish mongodb connection.

and above code with error msg here: Error: getenv cannot be interpreted at compile time, because it has no available source code

so don't know how to make change. thanks in advance!

May 13, 2022
On 13/05/2022 5:18 PM, MichaelBi wrote:
>      i have code here:
>          auto uri = environment.get("MONGODB_URI");
>          MongoClient conn = connectMongoDB(uri);
>          MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");
> 
> the "MONGODB_URI" showed above already put into heroku's app config as the 'key' and there is a paired 'value'. so the uri is to extract the value which is to use for app to establish mongodb connection.
> 
> and above code with error msg here: Error: `getenv` cannot be interpreted at compile time, because it has no available source code

That part of the code is probably fine.

Basically you have to be careful that:

auto uri = environment.get("MONGODB_URI");

Isn't being executed during compilation.

Stuff that typically cause this is anything that initializes a variable.

Globals:

shared Foo foo = Foo(...);

Class fields:

class Foo {
	Bar bar = Bar(...);
}

May 13, 2022
On Friday, 13 May 2022 at 05:41:33 UTC, rikki cattermole wrote:
> On 13/05/2022 5:18 PM, MichaelBi wrote:
>>      i have code here:
>>          auto uri = environment.get("MONGODB_URI");
>>          MongoClient conn = connectMongoDB(uri);
>>          MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");
>> 
>> the "MONGODB_URI" showed above already put into heroku's app config as the 'key' and there is a paired 'value'. so the uri is to extract the value which is to use for app to establish mongodb connection.
>> 
>> and above code with error msg here: Error: `getenv` cannot be interpreted at compile time, because it has no available source code
>
> That part of the code is probably fine.
>
> Basically you have to be careful that:
>
> auto uri = environment.get("MONGODB_URI");
>
> Isn't being executed during compilation.
>
> Stuff that typically cause this is anything that initializes a variable.
>
> Globals:
>
> shared Foo foo = Foo(...);
>
> Class fields:
>
> class Foo {
> 	Bar bar = Bar(...);
> }

yes, then should be this?

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

the structure is mapping of database field structure. how to resolve?
May 13, 2022
On 13/05/2022 5:52 PM, MichaelBi wrote:
> struct Camera{
>      @name("_id") BsonObjectID id; // represented as "_id" in the database
>      string brand;
>      string model;
> }
> 
> the structure is mapping of database field structure. how to resolve?

That code isn't the cause of your issue (its fine, no CTFE needed).

We would need see more of your code surrounding:

        auto uri = environment.get("MONGODB_URI");
        MongoClient conn = connectMongoDB(uri);
        MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");
May 13, 2022
On Friday, 13 May 2022 at 06:01:29 UTC, rikki cattermole wrote:
>
> On 13/05/2022 5:52 PM, MichaelBi wrote:
>> struct Camera{
>>      @name("_id") BsonObjectID id; // represented as "_id" in the database
>>      string brand;
>>      string model;
>> }
>> 
>> the structure is mapping of database field structure. how to resolve?
>
> That code isn't the cause of your issue (its fine, no CTFE needed).
>
> We would need see more of your code surrounding:
>
>         auto uri = environment.get("MONGODB_URI");
>         MongoClient conn = connectMongoDB(uri);
>         MongoDatabase eqpdb = conn.getDatabase("MbEqpHeroku");

here is the code:

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;
}


May 13, 2022
Okay that is fine, now we need to see where showData is being called.
May 13, 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;
}
May 13, 2022
On 13/05/2022 6:23 PM, MichaelBi wrote:
>          render!("index.dt", showData());

There ya go, template arguments are run at compile time.
May 13, 2022
On Friday, 13 May 2022 at 06:43:30 UTC, rikki cattermole wrote:
>
> On 13/05/2022 6:23 PM, MichaelBi wrote:
>>          render!("index.dt", showData());
>
> There ya go, template arguments are run at compile time.

Unh, then how to dynamically generate pages by using vibe.d....
May 13, 2022
On 13/05/2022 7:03 PM, MichaelBi wrote:
> On Friday, 13 May 2022 at 06:43:30 UTC, rikki cattermole wrote:
>>
>> On 13/05/2022 6:23 PM, MichaelBi wrote:
>>>          render!("index.dt", showData());
>>
>> There ya go, template arguments are run at compile time.
> 
> Unh, then how to dynamically generate pages by using vibe.d....

Its been a while since I used vibe.d but I think just storing it in a variable first would be enough.

https://vibed.org/api/vibe.http.server/render