Skip to content

Commit 57fafad

Browse files
authored
conn string support (#372)
1 parent b10a701 commit 57fafad

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

client/src/actions/connection.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const SET_BACKUP_ENABLED = "connection/SET_BACKUP_ENABLED";
2424
export const SET_QUERY_TIMEOUT = "connection/SET_QUERY_TIMEOUT";
2525
export const SET_SLASH_API_KEY = "connection/SET_SLASH_API_KEY";
2626
export const SET_AUTH_TOKEN = "connection/SET_AUTH_TOKEN";
27+
export const SET_URL_AND_SLASH_API_KEY = "connection/SET_URL_AND_SLASH_API_KEY";
2728
export const REMOVE_URL = "connection/REMOVE_URL";
2829
export const UPDATE_URL = "connection/UPDATE_URL";
2930
export const UPDATE_ACL_STATE = "connection/UPDATE_ACL_STATE";
@@ -64,6 +65,15 @@ export function setSlashApiKey(url, slashApiKey) {
6465
};
6566
}
6667

68+
export function setUrlAndSlashApiKey(connectionString) {
69+
const { url, bearertoken } = helpers.parseDgraphUrl(connectionString);
70+
return {
71+
type: SET_URL_AND_SLASH_API_KEY,
72+
url,
73+
slashApiKey: bearertoken,
74+
};
75+
}
76+
6777
export function setAuthToken(url, authToken) {
6878
return {
6979
type: SET_AUTH_TOKEN,

client/src/components/ServerConnectionModal.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export default function ServerConnectionModal() {
6666
}, CHECK_HEALTH_INTERVAL);
6767

6868
const connectTo = url => {
69+
if (url.startsWith("dgraph://")) {
70+
dispatch(actions.setUrlAndSlashApiKey(url));
71+
onHide();
72+
return;
73+
}
6974
if (!url || !url.trim()) {
7075
setShowError(true);
7176
return;
@@ -217,7 +222,7 @@ export default function ServerConnectionModal() {
217222
}}
218223
>
219224
<Form.Group controlId="serverUrlInput">
220-
<Form.Label>Dgraph Alpha URL:</Form.Label>
225+
<Form.Label>Dgraph Conn String:</Form.Label>
221226
<Form.Control
222227
type="text"
223228
placeholder="https://dgraph.example.com:port"
@@ -265,7 +270,7 @@ export default function ServerConnectionModal() {
265270

266271
const historyDisplay = (
267272
<>
268-
<h6>Recent Alpha Connections</h6>
273+
<h6>Recent Dgraph Connections</h6>
269274
<ListGroup>
270275
{serverHistory.map((s, index) => (
271276
<ListGroup.Item
@@ -334,7 +339,7 @@ export default function ServerConnectionModal() {
334339
<Modal.Header closeButton>
335340
<Modal.Title>
336341
<img src={DgraphLogo} alt="Dgraph Logo" className="logo" />
337-
<span className="title">Dgraph Alpha Connection</span>
342+
<span className="title">Dgraph Connections</span>
338343
</Modal.Title>
339344
</Modal.Header>
340345
<Modal.Body>

client/src/lib/helpers.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ const clientStubOptions = {
7373
},
7474
};
7575

76+
export const parseDgraphUrl = url => {
77+
// Handle dgraph:// protocol
78+
if (url.startsWith("dgraph://")) {
79+
const [_protocol, rest] = url.split("://");
80+
const [host, queryString] = rest.split("?");
81+
const params = new URLSearchParams(queryString || "");
82+
83+
// Remove port number from host if present
84+
const hostWithoutPort = host.split(":")[0];
85+
86+
return {
87+
url: `https://${hostWithoutPort}/dgraph`,
88+
sslmode: params.get("sslmode"),
89+
bearertoken: params.get("bearertoken"),
90+
};
91+
}
92+
93+
// Handle regular http(s) URLs
94+
return {
95+
url,
96+
sslmode: "verify-ca",
97+
bearertoken: null,
98+
};
99+
};
100+
76101
const createDgraphClient = memoizeOne(async url => {
77102
const stub = new dgraph.DgraphClientStub(
78103
url,
@@ -135,6 +160,7 @@ export async function executeQuery(
135160
}
136161

137162
const client = await getDgraphClient();
163+
console.log("client", client);
138164

139165
if (action === "query") {
140166
return client

client/src/reducers/connection.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
UPDATE_SERVER_HEALTH,
2525
UPDATE_SERVER_VERSION,
2626
UPDATE_ZERO_URL,
27+
SET_MULTI_TENANCY_ENABLED,
28+
SET_URL_AND_SLASH_API_KEY,
2729
} from "actions/connection";
2830
import {
2931
MIGRATE_TO_SERVER_CONNECTION,
@@ -48,7 +50,6 @@ import {
4850
SERVER_HISTORY_LENGTH,
4951
Unknown,
5052
} from "lib/constants";
51-
import { SET_MULTI_TENANCY_ENABLED } from "../actions/connection";
5253

5354
const assert = (test, message = "No message") => {
5455
if (!test) {
@@ -192,6 +193,17 @@ export default (state = defaultState, action) =>
192193
setCurrentServerSlashApiKey(activeServer.slashApiKey);
193194
}
194195
break;
196+
case SET_URL_AND_SLASH_API_KEY:
197+
draft.serverHistory = historyPlusServer(
198+
draft.serverHistory,
199+
makeServerRecord(action.url),
200+
);
201+
202+
const newActiveServer = draft.serverHistory[0];
203+
newActiveServer.slashApiKey = action.slashApiKey;
204+
setCurrentServerUrl(newActiveServer.url);
205+
setCurrentServerSlashApiKey(newActiveServer.slashApiKey);
206+
break;
195207
case SET_AUTH_TOKEN:
196208
assert(action.url, "This action requires url " + action.type);
197209
activeServer.authToken = action.authToken;

0 commit comments

Comments
 (0)