Skip to content

Commit 357c044

Browse files
committed
Enable swagger conditionally
1 parent 09ca65a commit 357c044

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

cli/deployment/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ func newConfig() *codersdk.DeploymentConfig {
430430
Flag: "max-token-lifetime",
431431
Default: 24 * 30 * time.Hour,
432432
},
433+
Swagger: &codersdk.SwaggerConfig{
434+
Enabled: &codersdk.DeploymentConfigField[bool]{
435+
Name: "Enable swagger endpoint",
436+
Usage: "Expose the swagger endpoint via /swagger.",
437+
Flag: "swagger-enabled",
438+
Default: flag.Lookup("test.v") == nil && !buildinfo.IsDev(),
439+
},
440+
},
433441
}
434442
}
435443

cli/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
604604
), cfg.Prometheus.Address.Value, "prometheus")()
605605
}
606606

607+
if cfg.Swagger.Enabled.Value {
608+
options.SwaggerEndpointEnabled = cfg.Swagger.Enabled.Value
609+
}
610+
607611
// We use a separate coderAPICloser so the Enterprise API
608612
// can have it's own close functions. This is cleaner
609613
// than abstracting the Coder API itself.

cli/testdata/coder_server_--help.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ Flags:
168168
"ecdsa", or "rsa4096".
169169
Consumes $CODER_SSH_KEYGEN_ALGORITHM
170170
(default "ed25519")
171+
--swagger-enabled Expose the swagger endpoint via /swagger.
172+
Consumes $CODER_SWAGGER_ENABLED
171173
--telemetry Whether telemetry is enabled or not.
172174
Coder collects anonymized usage data to
173175
help improve our product.

coderd/coderd.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type Options struct {
111111
Experimental bool
112112
DeploymentConfig *codersdk.DeploymentConfig
113113
UpdateCheckOptions *updatecheck.Options // Set non-nil to enable update checking.
114+
115+
SwaggerEndpointEnabled bool
114116
}
115117

116118
// @title Coderd API
@@ -596,10 +598,13 @@ func New(options *Options) *API {
596598
})
597599
})
598600
})
599-
// Swagger UI requires the URL trailing slash. Otherwise, the browser tries to load /assets
600-
// from http://localhost:8080/assets instead of http://localhost:8080/swagger/assets.
601-
r.Get("/swagger", http.RedirectHandler("/swagger/", http.StatusSeeOther).ServeHTTP)
602-
r.Get("/swagger/*", httpSwagger.Handler(httpSwagger.URL("/swagger/doc.json")))
601+
602+
if options.SwaggerEndpointEnabled {
603+
// Swagger UI requires the URL trailing slash. Otherwise, the browser tries to load /assets
604+
// from http://localhost:8080/assets instead of http://localhost:8080/swagger/assets.
605+
r.Get("/swagger", http.RedirectHandler("/swagger/", http.StatusSeeOther).ServeHTTP)
606+
r.Get("/swagger/*", httpSwagger.Handler(httpSwagger.URL("/swagger/doc.json")))
607+
}
603608

604609
r.NotFound(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP)).ServeHTTP)
605610
return api

codersdk/deploymentconfig.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type DeploymentConfig struct {
4343
Experimental *DeploymentConfigField[bool] `json:"experimental" typescript:",notnull"`
4444
UpdateCheck *DeploymentConfigField[bool] `json:"update_check" typescript:",notnull"`
4545
MaxTokenLifetime *DeploymentConfigField[time.Duration] `json:"max_token_lifetime" typescript:",notnull"`
46+
Swagger *SwaggerConfig `json:"swagger" typescript:",notnull"`
4647
}
4748

4849
type DERP struct {
@@ -141,6 +142,10 @@ type ProvisionerConfig struct {
141142
ForceCancelInterval *DeploymentConfigField[time.Duration] `json:"force_cancel_interval" typescript:",notnull"`
142143
}
143144

145+
type SwaggerConfig struct {
146+
Enabled *DeploymentConfigField[bool] `json:"enabled",typescript:",notnull"`
147+
}
148+
144149
type Flaggable interface {
145150
string | time.Duration | bool | int | []string | []GitAuthConfig
146151
}

0 commit comments

Comments
 (0)