Skip to content

Commit

Permalink
Support Internal CouchDB URL different than external (ingress usage)
Browse files Browse the repository at this point in the history
  • Loading branch information
davideshay committed Apr 1, 2023
1 parent e37fd8f commit e3f41fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions server/src/apicalls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const couchdbUrl = (process.env.COUCHDB_URL == undefined) ? "" : process.env.COUCHDB_URL.endsWith("/") ? process.env.COUCHDB_URL.slice(0,-1): process.env.COUCHDB_URL;
export const couchdbInternalUrl = (process.env.COUCHDB_INTERNAL_URL == undefined) ? couchdbUrl : process.env.COUCHDB_INTERAL_URL?.endsWith("/") ? process.env.COUCHDB_INTERNAL_URL.slice(0,-1): process.env.COUCHDB_INTERNAL_URL;
export const couchDatabase = (process.env.COUCHDB_DATABASE == undefined) ? "" : process.env.COUCHDB_DATABASE;
export const couchKey = process.env.COUCHDB_HMAC_KEY;
export const couchAdminUser = process.env.COUCHDB_ADMIN_USER;
Expand Down Expand Up @@ -30,7 +31,7 @@ const smtpOptions: SMTPTransport.Options= {
import nodemailer from 'nodemailer';
import nanoAdmin, { DocumentListResponse, MangoResponse, MaybeDocument } from 'nano';
const nanoAdminOpts = {
url: couchdbUrl,
url: couchdbInternalUrl,
requestDefaults: {
headers: { Authorization: "Basic "+ Buffer.from(couchAdminUser+":"+couchAdminPassword).toString('base64') }
}
Expand Down Expand Up @@ -102,8 +103,8 @@ export async function issueToken(req: Request, res: Response) {
loginRoles: [],
refreshJWT: "",
accessJWT: "",
couchdbUrl: process.env.COUCHDB_URL,
couchdbDatabase: process.env.COUCHDB_DATABASE
couchdbUrl: couchdbUrl,
couchdbDatabase: couchDatabase
}
let loginResponse = await couchLogin(username,password);
if (!loginResponse.loginSuccessful) {
Expand Down Expand Up @@ -199,8 +200,8 @@ export async function registerNewUser(req: CustomRequest<NewUserReqBody>, res: R
idCreated: "",
refreshJWT: "",
accessJWT: "",
couchdbUrl: process.env.COUCHDB_URL,
couchdbDatabase: process.env.COUCHDB_DATABASE,
couchdbUrl: couchdbUrl,
couchdbDatabase: couchDatabase,
email: email,
fullname: fullname
}
Expand Down
10 changes: 6 additions & 4 deletions server/src/dbstartup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { todosNanoAsAdmin, usersNanoAsAdmin, couchDatabase, couchAdminPassword, couchAdminUser, couchdbUrl, couchStandardRole,
import { todosNanoAsAdmin, usersNanoAsAdmin, couchDatabase, couchAdminPassword, couchAdminUser, couchdbUrl, couchdbInternalUrl, couchStandardRole,
couchAdminRole, conflictsViewID, conflictsViewName, utilitiesViewID, refreshTokenExpires, accessTokenExpires,
enableScheduling, resolveConflictsFrequencyMinutes,expireJWTFrequencyMinutes } from "./apicalls";
import { resolveConflicts } from "./apicalls";
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function couchLogin(username: string, password: string) {
}
const config: AxiosRequestConfig = {
method: 'get',
url: couchdbUrl+"/_session",
url: couchdbInternalUrl+"/_session",
auth: { username: username, password: password},
responseType: 'json'
}
Expand Down Expand Up @@ -91,7 +91,7 @@ async function setDBSecurity() {
let errorSettingSecurity = false;
let config: AxiosRequestConfig = {
method: 'get',
url: couchdbUrl+"/"+couchDatabase+"/_security",
url: couchdbInternalUrl+"/"+couchDatabase+"/_security",
auth: {username: String(couchAdminUser), password: String(couchAdminPassword)},
responseType: 'json'
}
Expand Down Expand Up @@ -125,7 +125,7 @@ async function setDBSecurity() {
}
let configSec: any = {
method: 'put',
url: couchdbUrl+"/"+couchDatabase+"/_security",
url: couchdbInternalUrl+"/"+couchDatabase+"/_security",
auth: {username: couchAdminUser, password: couchAdminPassword},
responseType: 'json',
data: newSecurity
Expand Down Expand Up @@ -531,7 +531,9 @@ export async function dbStartup() {
console.log("STATUS: App Version: ",appVersion);
console.log("STATUS: Database Schema Version:",maxAppSupportedSchemaVersion);
if (couchdbUrl == "") {console.log("ERROR: No environment variable for CouchDB URL"); return false;}
if (couchdbInternalUrl == "") {console.log("ERROR: No environment variable for internal CouchDB URL"); return false;}
console.log("STATUS: Database URL: ",couchdbUrl);
console.log("STATUS: Internal Database URL: ",couchdbInternalUrl);
if (couchDatabase == "") { console.log("ERROR: No CouchDatabase environment variable."); return false;}
console.log("STATUS: Using database: ",couchDatabase);
console.log("STATUS: Refresh token expires in ",refreshTokenExpires);
Expand Down

0 comments on commit e3f41fa

Please sign in to comment.