Skip to content

Commit 5fc7832

Browse files
committed
Add more init code
1 parent ca5b50c commit 5fc7832

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

enterprise/externalproxy/proxy.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package externalproxy
22

33
import (
4+
"context"
45
"net/http"
56
"net/url"
67
"regexp"
78
"time"
89

10+
"github.com/google/uuid"
11+
12+
"github.com/coder/coder/codersdk"
13+
914
"github.com/coder/coder/buildinfo"
1015

1116
"github.com/prometheus/client_golang/prometheus"
@@ -75,36 +80,56 @@ type Server struct {
7580
// - derpserver
7681

7782
Options *Options
83+
// SDKClient is a client to the primary coderd instance.
84+
// TODO: We really only need 'DialWorkspaceAgent', so maybe just pass that?
85+
SDKClient *codersdk.Client
86+
87+
// Used for graceful shutdown.
88+
// Required for the dialer.
89+
ctx context.Context
90+
cancel context.CancelFunc
7891
}
7992

8093
func New(opts *Options) *Server {
8194
if opts.PrometheusRegistry == nil {
8295
opts.PrometheusRegistry = prometheus.NewRegistry()
8396
}
8497

98+
client := codersdk.New(opts.PrimaryAccessURL)
99+
// TODO: @emyrk we need to implement some form of authentication for the
100+
// external proxy to the the primary. This allows us to make workspace
101+
// connections.
102+
// Ideally we reuse the same client as the cli, but this can be changed.
103+
// If the auth fails, we need some logic to retry and make sure this client
104+
// is always authenticated and usable.
105+
client.SetSessionToken("fake-token")
106+
85107
r := chi.NewRouter()
108+
ctx, cancel := context.WithCancel(context.Background())
86109
s := &Server{
87-
Options: opts,
88-
PrimaryAccessURL: opts.PrimaryAccessURL,
89-
AppServer: &workspaceapps.Server{
90-
Logger: opts.Logger.Named("workspaceapps"),
91-
DashboardURL: opts.PrimaryAccessURL,
92-
AccessURL: opts.AccessURL,
93-
Hostname: opts.AppHostname,
94-
HostnameRegex: opts.AppHostnameRegex,
95-
// TODO: @emyrk We should reduce the options passed in here.
96-
DeploymentValues: nil,
97-
RealIPConfig: opts.RealIPConfig,
98-
// TODO: @emyrk we need to implement this for external token providers.
99-
SignedTokenProvider: nil,
100-
// TODO: @emyrk we need to implement a dialer
101-
WorkspaceConnCache: wsconncache.New(nil, 0),
102-
AppSecurityKey: opts.AppSecurityKey,
103-
},
110+
Options: opts,
111+
PrimaryAccessURL: opts.PrimaryAccessURL,
104112
Logger: opts.Logger.Named("workspace-proxy"),
105113
TracerProvider: opts.Tracing,
106114
PrometheusRegistry: opts.PrometheusRegistry,
107115
Handler: r,
116+
ctx: ctx,
117+
cancel: cancel,
118+
}
119+
120+
s.AppServer = &workspaceapps.Server{
121+
Logger: opts.Logger.Named("workspaceapps"),
122+
DashboardURL: opts.PrimaryAccessURL,
123+
AccessURL: opts.AccessURL,
124+
Hostname: opts.AppHostname,
125+
HostnameRegex: opts.AppHostnameRegex,
126+
// TODO: @emyrk We should reduce the options passed in here.
127+
DeploymentValues: nil,
128+
RealIPConfig: opts.RealIPConfig,
129+
// TODO: @emyrk we need to implement this for external token providers.
130+
SignedTokenProvider: nil,
131+
WorkspaceConnCache: wsconncache.New(s.DialWorkspaceAgent, 0),
132+
AppSecurityKey: opts.AppSecurityKey,
108133
}
109134

110135
// Routes
@@ -157,5 +182,10 @@ func New(opts *Options) *Server {
157182
}
158183

159184
func (s *Server) Close() error {
185+
s.cancel()
160186
return s.AppServer.Close()
161187
}
188+
189+
func (s *Server) DialWorkspaceAgent(id uuid.UUID) (*codersdk.WorkspaceAgentConn, error) {
190+
return s.SDKClient.DialWorkspaceAgent(s.ctx, id, nil)
191+
}

0 commit comments

Comments
 (0)