Skip to content

Commit

Permalink
feat(native): Initial support for native api-gateway (#8472)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr authored Jul 22, 2024
1 parent f3f9eb4 commit d917d6f
Show file tree
Hide file tree
Showing 25 changed files with 615 additions and 150 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"url": "https://github.com/cube-js/cube.git"
},
"resolutions": {
"@types/node": "^16",
"@types/node": "^18",
"@types/ramda": "0.27.40"
},
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-api-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"graphql": "^15.8.0",
"graphql-scalars": "^1.10.0",
"graphql-tag": "^2.12.6",
"http-proxy-middleware": "^3.0.0",
"inflection": "^1.12.0",
"joi": "^17.8.3",
"jsonwebtoken": "^8.3.0",
Expand Down
16 changes: 16 additions & 0 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type {
Request as ExpressRequest,
Response as ExpressResponse,
} from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';

import {
QueryType,
ApiScopes,
Expand Down Expand Up @@ -500,6 +502,20 @@ class ApiGateway {
}));
}

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

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

app.use(
`${this.basePath}/v2`,
proxyMiddleware as any
);
}

app.use(this.handleErrorMiddleware);
}

Expand Down
19 changes: 16 additions & 3 deletions packages/cubejs-api-gateway/src/sql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type SQLServerOptions = {
canSwitchSqlUser?: CanSwitchSQLUserFn,
sqlPort?: number,
pgSqlPort?: number,
sqlNonce?: string,
gatewayPort?: number,
sqlUser?: string,
sqlSuperUser?: string,
sqlPassword?: string,
Expand All @@ -27,6 +27,8 @@ export type SQLServerOptions = {
export class SQLServer {
protected sqlInterfaceInstance: SqlInterfaceInstance | null = null;

protected gatewayPort: number = 7575;

public constructor(
protected readonly apiGateway: ApiGateway,
) {
Expand All @@ -36,6 +38,14 @@ export class SQLServer {
);
}

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

throw new Error('Native ApiGateway is not enabled');
}

public async execSql(sqlQuery: string, stream: any, securityContext?: any) {
await execSql(this.sqlInterfaceInstance!, sqlQuery, stream, securityContext);
}
Expand All @@ -45,6 +55,10 @@ 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 Expand Up @@ -73,9 +87,8 @@ export class SQLServer {
const canSwitchUserForSession = async (session, user) => session.superuser || canSwitchSqlUser(session.user, user);

this.sqlInterfaceInstance = await registerInterface({
port: options.sqlPort,
gatewayPort: this.gatewayPort,
pgPort: options.pgSqlPort,
nonce: options.sqlNonce,
checkAuth: async ({ request, user, password }) => {
const { password: returnedPassword, superuser, securityContext, skipPasswordCheck } = await checkSqlAuth(request, user, password);

Expand Down
3 changes: 1 addition & 2 deletions packages/cubejs-backend-cloud/src/live-preview.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import chokidar from 'chokidar';
import { FSWatcher } from 'fs';

import { internalExceptions } from '@cubejs-backend/shared';

import { CubeCloudClient, AuthObject } from './cloud';
import { DeployController } from './deploy';

export class LivePreviewWatcher {
private watcher: FSWatcher | null = null;
private watcher: chokidar.FSWatcher | null = null;

private handleQueueTimeout: NodeJS.Timeout | null = null;

Expand Down
Loading

0 comments on commit d917d6f

Please sign in to comment.