Skip to content

Http for database account endpoint #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import Item from "./item";

export default class Account extends Item {
databases: Databases;

ssl: boolean;

constructor(host: string, port: number) {
constructor(host: string, port: number, ssl: boolean) {
super({
_rid: host,
_self: "",
Expand All @@ -16,13 +18,13 @@ export default class Account extends Item {
writableLocations: [
{
name: "South Central US",
databaseAccountEndpoint: `https://${host}:${port}/`
databaseAccountEndpoint: `http${ssl ? "s" : ""}://${host}:${port}/`
}
],
readableLocations: [
{
name: "South Central US",
databaseAccountEndpoint: `https://${host}:${port}/`
databaseAccountEndpoint: `http${ssl ? "s" : ""}://${host}:${port}/`
}
],
enableMultipleWriteLocations: false,
Expand All @@ -45,9 +47,17 @@ export default class Account extends Item {
queryEngineConfiguration:
'{"maxSqlQueryInputLength":262144,"maxJoinsPerSqlQuery":5,"maxLogicalAndPerSqlQuery":500,"maxLogicalOrPerSqlQuery":500,"maxUdfRefPerSqlQuery":10,"maxInExpressionItemsCount":16000,"queryMaxInMemorySortDocumentCount":500,"maxQueryRequestTimeoutFraction":0.9,"sqlAllowNonFiniteNumbers":false,"sqlAllowAggregateFunctions":true,"sqlAllowSubQuery":true,"sqlAllowScalarSubQuery":true,"allowNewKeywords":true,"sqlAllowLike":false,"maxSpatialQueryCells":12,"spatialMaxGeometryPointCount":256,"sqlAllowTop":true,"enableSpatialIndexing":true}'
});
this.ssl = ssl;
this.databases = new Databases(this, ["/id"]);
}

updateHostName(host: string){
// when running this server in a dockerized environment, the hostname becomes the servername
// and databaseAccountEndpoint needs to point to the hostname provided by the caller
this._data.writableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`;
this._data.readableLocations[0].databaseAccountEndpoint = `http${this.ssl ? "s" : ""}://${host}/`;
}

database(idOrRid: string) {
return this.databases._item("/id", idOrRid, idOrRid);
}
Expand Down
6 changes: 5 additions & 1 deletion src/handler/read-meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as http from "http";
import Account from "../account";

export default (account: Account) => account.read();
export default (account: Account, req: http.IncomingMessage) => {
account.updateHostName(req.headers.host);
return account.read();
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ const handleRequest = (
});
};

const createAccount = (address: string | net.AddressInfo) => {
const createAccount = (address: string | net.AddressInfo, ssl: boolean) => {
if (!address || typeof address !== "object") {
throw new Error(`Unexpected address type: ${address}`);
}

const { address: host, port } = address as net.AddressInfo;
const hostname = host === "0.0.0.0" || host === "::" ? "localhost" : host;

return new Account(hostname, port);
return new Account(hostname, port, ssl);
};

export function createHttpServer(opts?: http.ServerOptions) {
Expand All @@ -87,7 +87,7 @@ export function createHttpServer(opts?: http.ServerOptions) {
handleRequest(account, req, res);
})
.on("listening", () => {
account = createAccount(server.address());
account = createAccount(server.address(), false);
});

return server;
Expand All @@ -110,7 +110,7 @@ export function createHttpsServer(opts?: https.ServerOptions) {
handleRequest(account, req, res);
})
.on("listening", () => {
account = createAccount(server.address());
account = createAccount(server.address(), true);
});

return server;
Expand Down