@@ -19,6 +19,7 @@ import (
19
19
20
20
"cdr.dev/slog"
21
21
"github.com/coder/coder/buildinfo"
22
+ "github.com/coder/coder/coderd"
22
23
"github.com/coder/coder/coderd/httpapi"
23
24
"github.com/coder/coder/coderd/httpmw"
24
25
"github.com/coder/coder/coderd/tracing"
@@ -186,6 +187,21 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
186
187
SecureAuthCookie : opts .SecureAuthCookie ,
187
188
}
188
189
190
+ // The primary coderd dashboard needs to make some GET requests to
191
+ // the workspace proxies to check latency.
192
+ corsMW := cors .Handler (cors.Options {
193
+ AllowedOrigins : []string {
194
+ // Allow the dashboard to make requests to the proxy for latency
195
+ // checks.
196
+ opts .DashboardURL .String (),
197
+ },
198
+ // Only allow GET requests for latency checks.
199
+ AllowedMethods : []string {http .MethodOptions , http .MethodGet },
200
+ AllowedHeaders : []string {"Accept" , "Content-Type" , "X-LATENCY-CHECK" , "X-CSRF-TOKEN" },
201
+ // Do not send any cookies
202
+ AllowCredentials : false ,
203
+ })
204
+
189
205
// Routes
190
206
apiRateLimiter := httpmw .RateLimit (opts .APIRateLimit , time .Minute )
191
207
// Persistent middlewares to all routes
@@ -198,20 +214,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
198
214
httpmw .ExtractRealIP (s .Options .RealIPConfig ),
199
215
httpmw .Logger (s .Logger ),
200
216
httpmw .Prometheus (s .PrometheusRegistry ),
201
- // The primary coderd dashboard needs to make some GET requests to
202
- // the workspace proxies to check latency.
203
- cors .Handler (cors.Options {
204
- AllowedOrigins : []string {
205
- // Allow the dashboard to make requests to the proxy for latency
206
- // checks.
207
- opts .DashboardURL .String (),
208
- },
209
- // Only allow GET requests for latency checks.
210
- AllowedMethods : []string {http .MethodGet },
211
- AllowedHeaders : []string {"Accept" , "Content-Type" },
212
- // Do not send any cookies
213
- AllowCredentials : false ,
214
- }),
217
+ corsMW ,
215
218
216
219
// HandleSubdomain is a middleware that handles all requests to the
217
220
// subdomain-based workspace apps.
@@ -260,6 +263,13 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
260
263
})
261
264
})
262
265
266
+ // See coderd/coderd.go for why we need this.
267
+ rootRouter := chi .NewRouter ()
268
+ // Make sure to add the cors middleware to the latency check route.
269
+ rootRouter .Get ("/latency-check" , corsMW (coderd .LatencyCheck (s .DashboardURL , s .AppServer .AccessURL )).ServeHTTP )
270
+ rootRouter .Mount ("/" , r )
271
+ s .Handler = rootRouter
272
+
263
273
return s , nil
264
274
}
265
275
0 commit comments