swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.66k stars 266 forks source link

Dark Mode Support? #300

Open adiatma85 opened 2 months ago

adiatma85 commented 2 months ago

Reference --> https://github.com/Amoenus/SwaggerDark/

My idea is we adding new configuration field something to be something like this:

type Config struct {
    // The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
    URL                      string
    DocExpansion             string
    InstanceName             string
    Title                    string
    DefaultModelsExpandDepth int
    DeepLinking              bool
    PersistAuthorization     bool
    Oauth2DefaultClientID    string
    IsDarkMode               bool
}

Then we can tweak the logic for css file creation to be something like this:

css := &textTemplate.Template{}
    if config.IsDarkMode {
        css, _ = textTemplate.New("swagger_index.css").Parse(swaggerDarkStyleTpl)
    } else {
        css, _ = textTemplate.New("swagger_index.css").Parse(swaggerStyleTpl)
    }

Add new function for modify the Dark Mode:

// Dark Mode for the Swagger
// Defaults to false
func SetDarkMode(isDarkMode bool) func(*Config) {
    return func(c *Config) {
        c.IsDarkMode = isDarkMode
    }
}

Finally, we can call it like this:

func main() {
    r := gin.New()

    r.GET("/v2/testapi/get-string-by-int/:some_id", api.GetStringByInt)
    r.GET("/v2/testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)

    url := ginSwagger.URL("http://petstore.swagger.io:8080/swagger/doc.json") // The url pointing to API definition
    isDarkMode := ginSwagger.SetDarkMode(true)
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url, isDarkMode))

    r.Run()
}

Wdyt?

adiatma85 commented 2 months ago

I am already trying this on forked repo, here is the result: image

rookie-luochao commented 1 month ago

Maybe you can try my new openapi-ui tool --> https://github.com/openapi-ui/go-openapi-ui