Skip to content

Commit

Permalink
chore(server): Configure native API gateway port via env variable (#8509
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ovr authored Jul 24, 2024
1 parent 29a7390 commit 64440dd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 31 deletions.
35 changes: 18 additions & 17 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import {
} from './query';
import { cachedHandler } from './cached-handler';
import { createJWKsFetcher } from './jwk';
import { SQLServer } from './sql-server';
import { SQLServer, SQLServerConstructorOptions } from './sql-server';
import { getJsonQueryFromGraphQLQuery, makeSchema } from './graphql';
import { ConfigItem, prepareAnnotation } from './helpers/prepareAnnotation';
import transformData from './helpers/transformData';
Expand Down Expand Up @@ -160,6 +160,8 @@ class ApiGateway {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected readonly event: (name: string, props?: object) => void;

protected readonly sqlServer: SQLServer;

public constructor(
protected readonly apiSecret: string,
protected readonly compilerApi: (ctx: RequestContext) => Promise<any>,
Expand Down Expand Up @@ -192,6 +194,19 @@ class ApiGateway {
this.wsContextAcceptor = options.wsContextAcceptor || (() => ({ accepted: true }));
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.event = options.event || function dummyEvent() {};
this.sqlServer = this.createSQLServerInstance({
gatewayPort: options.gatewayPort,
});
}

public getSQLServer(): SQLServer {
return this.sqlServer;
}

protected createSQLServerInstance(options: SQLServerConstructorOptions): SQLServer {
return new SQLServer(this, {
gatewayPort: options.gatewayPort,
});
}

public initApp(app: ExpressApplication) {
Expand Down Expand Up @@ -365,13 +380,11 @@ class ApiGateway {
`${this.basePath}/v1/cubesql`,
userMiddlewares,
userAsyncHandler(async (req, res) => {
const server = this.initSQLServer();

res.setHeader('Content-Type', 'application/json');
res.setHeader('Transfer-Encoding', 'chunked');

try {
await server.execSql(req.body.query, res, req.context?.securityContext);
await this.sqlServer.execSql(req.body.query, res, req.context?.securityContext);
} catch (e: any) {
this.handleError({
e,
Expand Down Expand Up @@ -503,10 +516,8 @@ class ApiGateway {
}

if (getEnv('nativeApiGateway')) {
const server = this.initSQLServer();

const proxyMiddleware = createProxyMiddleware<Request, Response>({
target: `http://127.0.0.1:${server.getNativeGatewayPort()}/v2`,
target: `http://127.0.0.1:${this.sqlServer.getNativeGatewayPort()}/v2`,
changeOrigin: true,
});

Expand All @@ -519,16 +530,6 @@ class ApiGateway {
app.use(this.handleErrorMiddleware);
}

protected _sqlServer: SQLServer | undefined;

public initSQLServer() {
if (!this._sqlServer) {
this._sqlServer = new SQLServer(this);
}

return this._sqlServer;
}

public initSubscriptionServer(sendMessage: WebSocketSendMessageFn) {
return new SubscriptionServer(this, sendMessage, this.subscriptionStore, this.wsContextAcceptor);
}
Expand Down
21 changes: 11 additions & 10 deletions packages/cubejs-api-gateway/src/sql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export type SQLServerOptions = {
canSwitchSqlUser?: CanSwitchSQLUserFn,
sqlPort?: number,
pgSqlPort?: number,
gatewayPort?: number,
sqlUser?: string,
sqlSuperUser?: string,
sqlPassword?: string,
gatewayPort?: number,
};

export type SQLServerConstructorOptions = {
gatewayPort?: number,
};

export class SQLServer {
Expand All @@ -31,19 +35,20 @@ export class SQLServer {

public constructor(
protected readonly apiGateway: ApiGateway,
options: SQLServerConstructorOptions,
) {
setupLogger(
({ event }) => apiGateway.log(event),
process.env.CUBEJS_LOG_LEVEL === 'trace' ? 'trace' : 'warn'
);
}

public getNativeGatewayPort(): number {
if (this.gatewayPort) {
return this.gatewayPort;
if (options.gatewayPort) {
this.gatewayPort = options.gatewayPort;
}
}

throw new Error('Native ApiGateway is not enabled');
public getNativeGatewayPort(): number {
return this.gatewayPort;
}

public async execSql(sqlQuery: string, stream: any, securityContext?: any) {
Expand All @@ -55,10 +60,6 @@ export class SQLServer {
throw new Error('Unable to start SQL interface two times');
}

if (options.gatewayPort) {
this.gatewayPort = options.gatewayPort;
}

const checkSqlAuth: CheckSQLAuthFn = (options.checkSqlAuth && this.wrapCheckSqlAuthFn(options.checkSqlAuth))
|| this.createDefaultCheckSqlAuthFn(options);

Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-api-gateway/src/types/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ScheduledRefreshContextsFn =
*/
interface ApiGatewayOptions {
standalone: boolean;
gatewayPort?: number,
dataSourceStorage: any;
refreshScheduler: any;
scheduledRefreshContexts?: ScheduledRefreshContextsFn;
Expand Down
14 changes: 13 additions & 1 deletion packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,18 @@ const variables: Record<string, (...args: any) => any> = {

return undefined;
},
nativeApiGatewayPort: () => {
if (process.env.CUBEJS_NATIVE_API_GATEWAY_PORT === 'false') {
return undefined;
}

const port = asFalseOrPort(process.env.CUBEJS_NATIVE_API_GATEWAY_PORT || 'false', 'CUBEJS_NATIVE_API_GATEWAY_PORT');
if (port) {
return port;
}

return undefined;
},
pgSqlPort: () => {
if (process.env.CUBEJS_PG_SQL_PORT === 'false') {
return undefined;
Expand Down Expand Up @@ -1727,7 +1739,7 @@ const variables: Record<string, (...args: any) => any> = {
sqlUser: () => get('CUBEJS_SQL_USER').asString(),
sqlPassword: () => get('CUBEJS_SQL_PASSWORD').asString(),
sqlSuperUser: () => get('CUBEJS_SQL_SUPER_USER').asString(),
// Internal testing
// Internal testing, please don't enable it. It's not ready for public preview
nativeApiGateway: () => get('CUBE_JS_NATIVE_API_GATEWAY_INTERNAL')
.asBool(),
// Experiments & Preview flags
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-server-core/src/core/optionsValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const schemaOptions = Joi.object().keys({
// SQL API
sqlPort: Joi.number(),
pgSqlPort: Joi.number(),
gatewayPort: Joi.number(),
sqlSuperUser: Joi.string(),
checkSqlAuth: Joi.func(),
canSwitchSqlUser: Joi.func(),
Expand Down
5 changes: 3 additions & 2 deletions packages/cubejs-server-core/src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class CubejsServerCore {

public initSQLServer() {
const apiGateway = this.apiGateway();
return apiGateway.initSQLServer();
return apiGateway.getSQLServer();
}

protected apiGateway(): ApiGateway {
Expand Down Expand Up @@ -457,6 +457,7 @@ export class CubejsServerCore {
scheduledRefreshTimeZones: this.options.scheduledRefreshTimeZones,
serverCoreVersion: this.coreServerVersion,
contextToApiScopes: this.options.contextToApiScopes,
gatewayPort: this.options.gatewayPort,
event: this.event,
}
));
Expand Down Expand Up @@ -530,7 +531,7 @@ export class CubejsServerCore {

this.repository = new FileRepository(this.options.schemaPath);
this.repositoryFactory = this.options.repositoryFactory || (() => this.repository);

this.startScheduledRefreshTimer();
}

Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-server-core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export interface CreateOptions {
checkSqlAuth?: CheckSQLAuthFn;
canSwitchSqlUser?: CanSwitchSQLUserFn;
jwt?: JWTOptions;
gatewayPort?: number;
// @deprecated Please use queryRewrite
queryTransformer?: QueryRewriteFn;
queryRewrite?: QueryRewriteFn;
Expand Down
3 changes: 2 additions & 1 deletion packages/cubejs-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class CubejsServer {
webSockets: config.webSockets || getEnv('webSockets'),
sqlPort: config.sqlPort || getEnv('sqlPort'),
pgSqlPort: config.pgSqlPort || getEnv('pgSqlPort'),
gatewayPort: config.gatewayPort || getEnv('nativeApiGatewayPort'),
http: {
...config.http,
cors: {
Expand All @@ -72,7 +73,7 @@ export class CubejsServer {
},
};

this.core = this.createCoreInstance(config, systemOptions);
this.core = this.createCoreInstance(this.config, systemOptions);
this.server = null;
}

Expand Down

0 comments on commit 64440dd

Please sign in to comment.