Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/code-desktop/status/main.go
2500 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package main
6
7
import (
8
"context"
9
"encoding/json"
10
"fmt"
11
"log"
12
"net/http"
13
"net/url"
14
"os"
15
16
"golang.org/x/xerrors"
17
"google.golang.org/grpc"
18
"google.golang.org/grpc/credentials/insecure"
19
20
"github.com/gitpod-io/gitpod/common-go/util"
21
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
22
)
23
24
func main() {
25
if len(os.Args) < 2 {
26
fmt.Printf("Usage: %s <port> [<link label>] [<schema>]\n", os.Args[0])
27
os.Exit(1)
28
}
29
port := os.Args[1]
30
31
label := "Open in VS Code Desktop"
32
if len(os.Args) > 2 {
33
label = os.Args[2]
34
}
35
36
schema := "vscode"
37
if len(os.Args) > 3 {
38
schema = os.Args[3]
39
}
40
41
errlog := log.New(os.Stderr, "VS Code Desktop status: ", log.LstdFlags)
42
43
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
44
wsInfo, err := GetWSInfo(context.Background())
45
if err != nil {
46
errlog.Printf("cannot get workspace info: %v\n", err)
47
http.Error(w, err.Error(), http.StatusServiceUnavailable)
48
}
49
50
type Query struct {
51
InstanceId string `json:"instanceId"`
52
WorkspaceId string `json:"workspaceId"`
53
GitpodHost string `json:"gitpodHost"`
54
DebugWorkspace bool `json:"debugWorkspace"`
55
}
56
debugWorkspace := false
57
if wsInfo.GetDebugWorkspaceType() != supervisor.DebugWorkspaceType_noDebug {
58
debugWorkspace = true
59
}
60
query := &Query{
61
InstanceId: wsInfo.InstanceId,
62
WorkspaceId: wsInfo.WorkspaceId,
63
GitpodHost: wsInfo.GitpodHost,
64
DebugWorkspace: debugWorkspace,
65
}
66
b, err := json.Marshal(query)
67
if err != nil {
68
errlog.Printf("cannot marshal query: %v\n", err)
69
http.Error(w, err.Error(), http.StatusServiceUnavailable)
70
}
71
queryString := string(b)
72
73
workspaceLocation := wsInfo.GetWorkspaceLocationFile()
74
if workspaceLocation == "" {
75
workspaceLocation = wsInfo.GetWorkspaceLocationFolder()
76
}
77
78
link := url.URL{
79
Scheme: schema,
80
Host: "gitpod.gitpod-desktop",
81
Path: workspaceLocation,
82
RawQuery: url.QueryEscape(queryString),
83
}
84
85
response := make(map[string]string)
86
response["link"] = link.String()
87
response["label"] = label
88
response["clientID"] = schema
89
response["kind"] = "code-desktop"
90
w.Header().Set("Content-Type", "application/json")
91
json.NewEncoder(w).Encode(response)
92
})
93
94
fmt.Printf("Starting status proxy for desktop IDE at port %s\n", port)
95
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
96
log.Fatal(err)
97
}
98
}
99
100
func GetWSInfo(ctx context.Context) (*supervisor.WorkspaceInfoResponse, error) {
101
supervisorConn, err := grpc.Dial(util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
102
if err != nil {
103
return nil, xerrors.Errorf("failed connecting to supervisor: %w", err)
104
}
105
defer supervisorConn.Close()
106
wsinfo, err := supervisor.NewInfoServiceClient(supervisorConn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
107
if err != nil {
108
return nil, xerrors.Errorf("failed getting workspace info from supervisor: %w", err)
109
}
110
return wsinfo, nil
111
}
112
113