chore: linting (#206)

This commit is contained in:
Bogdan U 2022-04-22 14:41:42 +03:00 committed by GitHub
parent f844160fb7
commit bd7f2153bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 44 deletions

View File

@ -16,38 +16,35 @@ import (
type swaggerConfig struct { type swaggerConfig struct {
URL string URL string
DeepLinking bool
DocExpansion string DocExpansion string
DefaultModelsExpandDepth int
Oauth2RedirectURL template.JS
Title string Title string
Oauth2RedirectURL template.JS
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool PersistAuthorization bool
} }
// Config stores ginSwagger configuration variables. // Config stores ginSwagger configuration variables.
type Config struct { type Config struct {
//The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. // The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string URL string
DeepLinking bool
DocExpansion string DocExpansion string
DefaultModelsExpandDepth int
InstanceName string InstanceName string
Title string Title string
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool PersistAuthorization bool
} }
// Convert the config to a swagger one in order to fill unexposed template values. func (c Config) toSwaggerConfig() swaggerConfig {
func (c Config) ToSwaggerConfig() swaggerConfig {
return swaggerConfig{ return swaggerConfig{
URL: c.URL, URL: c.URL,
DeepLinking: c.DeepLinking, DeepLinking: c.DeepLinking,
DocExpansion: c.DocExpansion, DocExpansion: c.DocExpansion,
DefaultModelsExpandDepth: c.DefaultModelsExpandDepth, DefaultModelsExpandDepth: c.DefaultModelsExpandDepth,
Oauth2RedirectURL: template.JS( Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
"`${window.location.protocol}//${window.location.host}$" + "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" + "/oauth2-redirect.html`",
"/oauth2-redirect.html`",
),
Title: c.Title, Title: c.Title,
PersistAuthorization: c.PersistAuthorization, PersistAuthorization: c.PersistAuthorization,
} }
@ -67,7 +64,7 @@ func DocExpansion(docExpansion string) func(c *Config) {
} }
} }
// DeepLinking set the swagger deeplinking configuration // DeepLinking set the swagger deep linking configuration.
func DeepLinking(deepLinking bool) func(c *Config) { func DeepLinking(deepLinking bool) func(c *Config) {
return func(c *Config) { return func(c *Config) {
c.DeepLinking = deepLinking c.DeepLinking = deepLinking
@ -82,7 +79,7 @@ func DefaultModelsExpandDepth(depth int) func(c *Config) {
} }
} }
// InstanceName set the instance name that was used to generate the swagger documents. // InstanceName set the instance name that was used to generate the swagger documents
// Defaults to swag.Name ("swagger"). // Defaults to swag.Name ("swagger").
func InstanceName(name string) func(c *Config) { func InstanceName(name string) func(c *Config) {
return func(c *Config) { return func(c *Config) {
@ -90,8 +87,8 @@ func InstanceName(name string) func(c *Config) {
} }
} }
// If set to true, it persists authorization data and it would not be lost on browser close/refresh // PersistAuthorization If set to true, it persists authorization data and it would not be lost on browser close/refresh
// Defaults to false // Defaults to false.
func PersistAuthorization(persistAuthorization bool) func(c *Config) { func PersistAuthorization(persistAuthorization bool) func(c *Config) {
return func(c *Config) { return func(c *Config) {
c.PersistAuthorization = persistAuthorization c.PersistAuthorization = persistAuthorization
@ -99,8 +96,8 @@ func PersistAuthorization(persistAuthorization bool) func(c *Config) {
} }
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc { func WrapHandler(handler *webdav.Handler, options ...func(c *Config)) gin.HandlerFunc {
defaultConfig := &Config{ defaultConfig := Config{
URL: "doc.json", URL: "doc.json",
DeepLinking: true, DeepLinking: true,
DocExpansion: "list", DocExpansion: "list",
@ -109,11 +106,11 @@ func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc {
Title: "Swagger UI", Title: "Swagger UI",
} }
for _, c := range confs { for _, c := range options {
c(defaultConfig) c(&defaultConfig)
} }
return CustomWrapHandler(defaultConfig, h) return CustomWrapHandler(&defaultConfig, handler)
} }
// CustomWrapHandler wraps `http.Handler` into `gin.HandlerFunc` // CustomWrapHandler wraps `http.Handler` into `gin.HandlerFunc`
@ -123,6 +120,7 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
if config.InstanceName == "" { if config.InstanceName == "" {
config.InstanceName = swag.Name config.InstanceName = swag.Name
} }
if config.Title == "" { if config.Title == "" {
config.Title = "Swagger UI" config.Title = "Swagger UI"
} }
@ -131,19 +129,20 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
t := template.New("swagger_index.html") t := template.New("swagger_index.html")
index, _ := t.Parse(swagger_index_templ) index, _ := t.Parse(swagger_index_templ)
var rexp = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`) var rexp = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[?|.]*`)
return func(ctx *gin.Context) {
if ctx.Request.Method != http.MethodGet {
ctx.AbortWithStatus(http.StatusMethodNotAllowed)
return func(c *gin.Context) {
if c.Request.Method != http.MethodGet {
c.AbortWithStatus(http.StatusMethodNotAllowed)
return return
} }
matches := rexp.FindStringSubmatch(c.Request.RequestURI) matches := rexp.FindStringSubmatch(ctx.Request.RequestURI)
if len(matches) != 3 { if len(matches) != 3 {
c.Status(http.StatusNotFound) ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
_, _ = c.Writer.Write([]byte("404 page not found"))
return return
} }
@ -154,39 +153,39 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
switch filepath.Ext(path) { switch filepath.Ext(path) {
case ".html": case ".html":
c.Header("Content-Type", "text/html; charset=utf-8") ctx.Header("Content-Type", "text/html; charset=utf-8")
case ".css": case ".css":
c.Header("Content-Type", "text/css; charset=utf-8") ctx.Header("Content-Type", "text/css; charset=utf-8")
case ".js": case ".js":
c.Header("Content-Type", "application/javascript") ctx.Header("Content-Type", "application/javascript")
case ".png": case ".png":
c.Header("Content-Type", "image/png") ctx.Header("Content-Type", "image/png")
case ".json": case ".json":
c.Header("Content-Type", "application/json; charset=utf-8") ctx.Header("Content-Type", "application/json; charset=utf-8")
} }
switch path { switch path {
case "index.html": case "index.html":
_ = index.Execute(c.Writer, config.ToSwaggerConfig()) _ = index.Execute(ctx.Writer, config.toSwaggerConfig())
case "doc.json": case "doc.json":
doc, err := swag.ReadDoc(config.InstanceName) doc, err := swag.ReadDoc(config.InstanceName)
if err != nil { if err != nil {
c.AbortWithStatus(http.StatusInternalServerError) ctx.AbortWithStatus(http.StatusInternalServerError)
return return
} }
_, _ = c.Writer.Write([]byte(doc))
ctx.JSON(http.StatusOK, doc)
default: default:
handler.ServeHTTP(c.Writer, c.Request) handler.ServeHTTP(ctx.Writer, ctx.Request)
} }
} }
} }
// DisablingWrapHandler turn handler off // DisablingWrapHandler turn handler off
// if specified environment variable passed // if specified environment variable passed
func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc { func DisablingWrapHandler(handler *webdav.Handler, envName string) gin.HandlerFunc {
eFlag := os.Getenv(envName) if os.Getenv(envName) != "" {
if eFlag != "" {
return func(c *gin.Context) { return func(c *gin.Context) {
// Simulate behavior when route unspecified and // Simulate behavior when route unspecified and
// return 404 HTTP code // return 404 HTTP code
@ -194,14 +193,13 @@ func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc {
} }
} }
return WrapHandler(h) return WrapHandler(handler)
} }
// DisablingCustomWrapHandler turn handler off // DisablingCustomWrapHandler turn handler off
// if specified environment variable passed // if specified environment variable passed
func DisablingCustomWrapHandler(config *Config, h *webdav.Handler, envName string) gin.HandlerFunc { func DisablingCustomWrapHandler(config *Config, h *webdav.Handler, envName string) gin.HandlerFunc {
eFlag := os.Getenv(envName) if os.Getenv(envName) != "" {
if eFlag != "" {
return func(c *gin.Context) { return func(c *gin.Context) {
// Simulate behavior when route unspecified and // Simulate behavior when route unspecified and
// return 404 HTTP code // return 404 HTTP code