Skip to content

Commit

Permalink
Environment fallback for config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Jul 20, 2018
1 parent c920131 commit d2ac4fe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
24 changes: 24 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
"GENERATE_DEFAULT_DATA": {
"description": "Whether the database should be pre-filled. Use '0' for no.",
"required": false
},
"GITHUB_USER": {
"description": "GitHub user for authenticated access",
"required": false
},
"GITHUB_PASSWORD": {
"description": "GitHub password for authenticated access",
"required": false
},
"GITLAB_URL": {
"description": "GitLab URL for authenticated access",
"required": false
},
"GITLAB_AUTH": {
"description": "GitLab authentication for authenticated access",
"required": false
},
"BITBUCKET_USER": {
"description": "BitBucket user for authenticated access",
"required": false
},
"BITBUCKET_PASSWORD": {
"description": "BitBucket password for authenticated access",
"required": false
}
}
}
45 changes: 33 additions & 12 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ void defaultInit(UserManController userMan, DubRegistry registry)
}
}

struct AppConfig
{
string ghuser, ghpassword,
glurl, glauth,
bbuser, bbpassword;
void init()
{
import dub.internal.utils : jsonFromFile;
auto regsettingsjson = jsonFromFile(NativePath("settings.json"), true);
static immutable variables = [
["ghuser", "github-user"],
["ghpassword", "github-password"],
["glurl", "gitlab-url"],
["glauth", "gitlab-auth"],
["bbuser", "bitbucket-user"],
["bbpassword", "bitbucket-password"],
];
static foreach (var; variables)
{
// fallback to environment variables
mixin(var[0] ~ ` = regsettingsjson["`~var[1]~`"].opt!string(
environment.get("`~var[1]~`".replace("-", "_").toUpper)
);`);
}
}
}

void main()
{
bool noMonitoring;
Expand All @@ -93,18 +120,12 @@ void main()
//});
}

import dub.internal.utils : jsonFromFile;
auto regsettingsjson = jsonFromFile(NativePath("settings.json"), true);
auto ghuser = regsettingsjson["github-user"].opt!string;
auto ghpassword = regsettingsjson["github-password"].opt!string;
auto glurl = regsettingsjson["gitlab-url"].opt!string;
auto glauth = regsettingsjson["gitlab-auth"].opt!string;
auto bbuser = regsettingsjson["bitbucket-user"].opt!string;
auto bbpassword = regsettingsjson["bitbucket-password"].opt!string;

GithubRepository.register(ghuser, ghpassword);
BitbucketRepository.register(bbuser, bbpassword);
if (glurl.length) GitLabRepository.register(glauth, glurl);
AppConfig appConfig;
appConfig.init();

GithubRepository.register(appConfig.ghuser, appConfig.ghpassword);
BitbucketRepository.register(appConfig.bbuser, appConfig.bbpassword);
if (appConfig.glurl.length) GitLabRepository.register(appConfig.glauth, appConfig.glurl);

auto router = new URLRouter;
if (s_mirror.length) router.any("*", (req, res) { req.params["mirror"] = s_mirror; });
Expand Down

0 comments on commit d2ac4fe

Please sign in to comment.