Allow to disable swagger (#2093)

https://github.com/woodpecker-ci/woodpecker/issues/1944
This commit is contained in:
qwerty287 2023-08-03 02:42:30 +02:00 committed by GitHub
parent f06e0acdc6
commit 1a7f7a9427
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 9 deletions

View file

@ -272,6 +272,11 @@ var flags = []cli.Flag{
Name: "migrations-allow-long", Name: "migrations-allow-long",
Value: false, Value: false,
}, },
&cli.BoolFlag{
EnvVars: []string{"WOODPECKER_ENABLE_SWAGGER"},
Name: "enable-swagger",
Value: true,
},
// //
// resource limit parameters // resource limit parameters
// //

View file

@ -364,6 +364,7 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) {
server.Config.Pipeline.Volumes = c.StringSlice("volume") server.Config.Pipeline.Volumes = c.StringSlice("volume")
server.Config.Pipeline.Privileged = c.StringSlice("escalate") server.Config.Pipeline.Privileged = c.StringSlice("escalate")
server.Config.Server.Migrations.AllowLong = c.Bool("migrations-allow-long") server.Config.Server.Migrations.AllowLong = c.Bool("migrations-allow-long")
server.Config.Server.EnableSwagger = c.Bool("enable-swagger")
// prometheus // prometheus
server.Config.Prometheus.AuthToken = c.String("prometheus-auth-token") server.Config.Prometheus.AuthToken = c.String("prometheus-auth-token")

View file

@ -535,6 +535,11 @@ Server URL path prefix (used for statics loading when having a url path prefix),
Example: `WOODPECKER_ROOT_URL=/woodpecker` Example: `WOODPECKER_ROOT_URL=/woodpecker`
### `WOODPECKER_ENABLE_SWAGGER`
> Default: true
Enable the Swagger UI for API documentation.
--- ---
### `WOODPECKER_GITHUB_...` ### `WOODPECKER_GITHUB_...`

View file

@ -73,6 +73,7 @@ var Config = struct {
Migrations struct { Migrations struct {
AllowLong bool AllowLong bool
} }
EnableSwagger bool
// Open bool // Open bool
// Orgs map[string]struct{} // Orgs map[string]struct{}
// Admins map[string]struct{} // Admins map[string]struct{}

View file

@ -69,7 +69,9 @@ func Load(noRouteHandler http.HandlerFunc, middleware ...gin.HandlerFunc) http.H
e.GET("/healthz", api.Health) e.GET("/healthz", api.Health)
apiRoutes(e) apiRoutes(e)
setupSwaggerConfigAndRoutes(e) if server.Config.Server.EnableSwagger {
setupSwaggerConfigAndRoutes(e)
}
return e return e
} }

View file

@ -40,12 +40,13 @@ func Config(c *gin.Context) {
} }
configData := map[string]interface{}{ configData := map[string]interface{}{
"user": user, "user": user,
"csrf": csrf, "csrf": csrf,
"docs": server.Config.Server.Docs, "docs": server.Config.Server.Docs,
"version": version.String(), "version": version.String(),
"forge": server.Config.Services.Forge.Name(), "forge": server.Config.Services.Forge.Name(),
"root_url": server.Config.Server.RootURL, "root_url": server.Config.Server.RootURL,
"enable_swagger": server.Config.Server.EnableSwagger,
} }
// default func map with json parser. // default func map with json parser.
@ -75,4 +76,5 @@ window.WOODPECKER_VERSION = "{{ .version }}";
window.WOODPECKER_DOCS = "{{ .docs }}"; window.WOODPECKER_DOCS = "{{ .docs }}";
window.WOODPECKER_FORGE = "{{ .forge }}"; window.WOODPECKER_FORGE = "{{ .forge }}";
window.WOODPECKER_ROOT_URL = "{{ .root_url }}"; window.WOODPECKER_ROOT_URL = "{{ .root_url }}";
window.WOODPECKER_ENABLE_SWAGGER = {{ .enable_swagger }};
` `

View file

@ -18,7 +18,9 @@
<!-- Docs Link --> <!-- Docs Link -->
<a :href="docsUrl" target="_blank" class="navbar-link navbar-clickable hidden md:flex">{{ $t('docs') }}</a> <a :href="docsUrl" target="_blank" class="navbar-link navbar-clickable hidden md:flex">{{ $t('docs') }}</a>
<!-- API Link --> <!-- API Link -->
<a :href="apiUrl" target="_blank" class="navbar-link navbar-clickable hidden md:flex">{{ $t('api') }}</a> <a v-if="enableSwagger" :href="apiUrl" target="_blank" class="navbar-link navbar-clickable hidden md:flex">{{
$t('api')
}}</a>
</div> </div>
<!-- Right Icons Box --> <!-- Right Icons Box -->
<div class="flex ml-auto -m-1.5 items-center space-x-2"> <div class="flex ml-auto -m-1.5 items-center space-x-2">
@ -82,7 +84,15 @@ export default defineComponent({
const version = config.version?.startsWith('next') ? 'next' : config.version; const version = config.version?.startsWith('next') ? 'next' : config.version;
return { darkMode, user: authentication.user, doLogin, docsUrl, version, apiUrl }; return {
darkMode,
user: authentication.user,
doLogin,
docsUrl,
version,
apiUrl,
enableSwagger: config.enableSwagger,
};
}, },
}); });
</script> </script>

View file

@ -8,6 +8,7 @@ declare global {
WOODPECKER_CSRF: string | undefined; WOODPECKER_CSRF: string | undefined;
WOODPECKER_FORGE: string | undefined; WOODPECKER_FORGE: string | undefined;
WOODPECKER_ROOT_URL: string | undefined; WOODPECKER_ROOT_URL: string | undefined;
WOODPECKER_ENABLE_SWAGGER: boolean | undefined;
} }
} }
@ -18,4 +19,5 @@ export default () => ({
csrf: window.WOODPECKER_CSRF || null, csrf: window.WOODPECKER_CSRF || null,
forge: window.WOODPECKER_FORGE || null, forge: window.WOODPECKER_FORGE || null,
rootURL: window.WOODPECKER_ROOT_URL || null, rootURL: window.WOODPECKER_ROOT_URL || null,
enableSwagger: window.WOODPECKER_ENABLE_SWAGGER || false,
}); });

View file

@ -22,6 +22,7 @@
<div class="flex items-center"> <div class="flex items-center">
<h2 class="text-lg text-wp-text-100">{{ $t('user.api_usage') }}</h2> <h2 class="text-lg text-wp-text-100">{{ $t('user.api_usage') }}</h2>
<a <a
v-if="enableSwagger"
:href="`${address}/swagger/index.html`" :href="`${address}/swagger/index.html`"
target="_blank" target="_blank"
class="ml-4 text-wp-link-100 hover:text-wp-link-200" class="ml-4 text-wp-link-100 hover:text-wp-link-200"
@ -56,9 +57,11 @@ import Button from '~/components/atomic/Button.vue';
import SelectField from '~/components/form/SelectField.vue'; import SelectField from '~/components/form/SelectField.vue';
import Scaffold from '~/components/layout/scaffold/Scaffold.vue'; import Scaffold from '~/components/layout/scaffold/Scaffold.vue';
import useApiClient from '~/compositions/useApiClient'; import useApiClient from '~/compositions/useApiClient';
import useConfig from '~/compositions/useConfig';
import { setI18nLanguage } from '~/compositions/useI18n'; import { setI18nLanguage } from '~/compositions/useI18n';
const { t, locale } = useI18n(); const { t, locale } = useI18n();
const { enableSwagger } = useConfig();
const apiClient = useApiClient(); const apiClient = useApiClient();
const token = ref<string | undefined>(); const token = ref<string | undefined>();