@@ -19,6 +19,7 @@ import (
1919
2020"cdr.dev/slog"
2121"github.com/coder/coder/buildinfo"
22+ "github.com/coder/coder/coderd"
2223"github.com/coder/coder/coderd/httpapi"
2324"github.com/coder/coder/coderd/httpmw"
2425"github.com/coder/coder/coderd/tracing"
@@ -186,6 +187,21 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
186187SecureAuthCookie :opts .SecureAuthCookie ,
187188}
188189
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+
189205// Routes
190206apiRateLimiter := httpmw .RateLimit (opts .APIRateLimit ,time .Minute )
191207// Persistent middlewares to all routes
@@ -198,20 +214,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
198214httpmw .ExtractRealIP (s .Options .RealIPConfig ),
199215httpmw .Logger (s .Logger ),
200216httpmw .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 ,
215218
216219// HandleSubdomain is a middleware that handles all requests to the
217220// subdomain-based workspace apps.
@@ -260,6 +263,13 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
260263})
261264})
262265
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+
263273return s ,nil
264274}
265275