Skip to content

Commit 624df8a

Browse files
authored
Merge pull request #123 from Coding/platform-specific
handle workspace creation redirected from coding.net
2 parents 0d0f87a + 3448b27 commit 624df8a

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

app/backendAPI/workspaceAPI.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ export function getUserProfile () {
4949
{ headers: { Accept: '*/*' } }
5050
).then(res => res.data)
5151
}
52+
53+
export function findSpaceKey ({ ownerName, projectName }) {
54+
return request.get(`/ws/find/coding/${ownerName}/${projectName}`, null,
55+
{ headers: { Accept: '*/*' } }
56+
).then(res => res.data)
57+
}

app/initialize.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,35 @@ async function initialize () {
1515
const step = stepFactory()
1616
const urlPath = window.location.pathname
1717

18-
await step('[0] Get spaceKey from window.location', () => {
18+
await step('[0] Get spaceKey from window.location', async () => {
19+
// case 1: spaceKey in url
1920
let spaceKey = null
20-
const wsPathPattern = /^\/ws\/([^\/]+)\/?$/
21+
const wsPathPattern = /^\/ws\/([^/]+)\/?$/
2122
const match = wsPathPattern.exec(urlPath)
2223
if (match) spaceKey = match[1]
24+
if (spaceKey) return config.spaceKey = spaceKey
2325

24-
if (!spaceKey) spaceKey = qs.parse(window.location.hash.slice(1)).spaceKey
25-
if (spaceKey) config.spaceKey = spaceKey
26-
return true
26+
// case 2: spaceKey in querystring
27+
const qsParsed = qs.parse(window.location.search.slice(1))
28+
spaceKey = qsParsed.spaceKey
29+
if (spaceKey) return config.spaceKey = spaceKey
30+
31+
// case 3: get spaceKey by ownerName and projectName
32+
const { ownerName, projectName } = qsParsed
33+
if (config.isPlatform && ownerName && projectName) {
34+
spaceKey = await api.findSpaceKey({ ownerName, projectName })
35+
if (spaceKey) {
36+
config.spaceKey = spaceKey
37+
const redirectUrl = `${location.origin}/ws/${config.spaceKey}`
38+
if (window.history.pushState) {
39+
window.history.pushState(null, null, redirectUrl)
40+
} else {
41+
window.location = redirectUrl
42+
}
43+
return true
44+
}
45+
}
46+
return true // MISSING OF SPACEKEY SHOULD NOT BLOCK
2747
})
2848

2949
if (config.spaceKey) {
@@ -42,20 +62,23 @@ async function initialize () {
4262
const queryEntryPathPattern = /^\/ws\/?$/
4363
const isFromQueryEntryPath = queryEntryPathPattern.test(urlPath)
4464
if (isFromQueryEntryPath) {
45-
const parsed = qs.parse(location.search.substring(1))
46-
config.openFile = parsed.openFile
65+
const qsParsed = qs.parse(location.search.substring(1))
66+
config.openFile = qsParsed.openFile
4767
const options = {
48-
ownerName: parsed.ownerName,
49-
projectName: parsed.projectName,
50-
host: parsed.host,
68+
ownerName: qsParsed.ownerName,
69+
projectName: qsParsed.projectName,
70+
host: qsParsed.host,
71+
cpuLimit: 1,
72+
memory: 128,
73+
storage: 1
5174
}
52-
if (parsed.envId) options.envId = parsed.envId
53-
if (parsed.isTry) options.try = true
75+
if (qsParsed.envId) options.envId = qsParsed.envId
76+
if (qsParsed.isTry) options.try = true
5477
return api.createWorkspace(options).then((res) => {
5578
extendObservable(config, res)
5679
if (config.project && config.project.name) { config.projectName = config.project.name }
57-
if (history.pushState) {
58-
history.pushState(null, null,
80+
if (window.history.pushState) {
81+
window.history.pushState(null, null,
5982
`${location.origin}/ws/${config.spaceKey}`)
6083
}
6184
return true
@@ -80,6 +103,9 @@ async function initialize () {
80103
)
81104
}
82105

106+
/* @TODO: websocket connection is not a must, shouldn't block
107+
* also, terminal connection is optional, only connect when terminal panel is shown
108+
* */
83109
await step('[4] Connect websocket', () =>
84110
api.connectWebsocketClient()
85111
)

0 commit comments

Comments
 (0)