DEV Community

Discussion on: WSL vs plain old VirtualBox

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

It's an issue that is obvious to me. It usually hurts nothing, except for gin-swagger.

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "runtime"
    "strconv"
    "strings"

    "github.com/gin-gonic/gin"
    swaggerFiles "github.com/swaggo/files"
    ginSwagger "github.com/swaggo/gin-swagger"

    _ "${MY_GO_MOD}/docs"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2
func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    r := gin.New()

    swaggerURL := fmt.Sprintf("http://localhost:%s/swagger/doc.json", port)
    if runtime.GOOS == "linux" {
        if data, err := ioutil.ReadFile("/proc/version"); err == nil && strings.Contains(string(data), "microsoft") {
            if p, err := strconv.Atoi(port); err == nil {
                swaggerURL = fmt.Sprintf("http://localhost:%d/swagger/doc.json", p+1)
            }
        }
    }

    url := ginSwagger.URL(swaggerURL)
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

    log.Printf("Server running at http://localhost:%s", port)
    r.Run(":" + port)
}
Enter fullscreen mode Exit fullscreen mode