Gin Swag 初始化路径为空

问题描述 投票:0回答:0

我正在使用 swagger 来创建 API 文档。但是我在用swag init做程序的时候遇到了一个问题,就是空路径。这是我制作的程序。

package main

import (
    "log"
    "net/http"

    _ "simpleswag/docs"

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

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

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

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

// @host     localhost:3000
// @BasePath /
// @schemes  http
func main() {
    // Gin instance
    r := gin.New()

    // Routes
    r.GET("/", HealthCheck)

    url := ginSwagger.URL("http://localhost:3000/swagger/doc.json") // The url pointing to API definition
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

    // Start server
    if err := r.Run(":3000"); err != nil {
        log.Fatal(err)
    }
}

// HealthCheck godoc
// @Summary     Show the status of server.
// @Description get the status of server.
// @Tags        root
// @Accept      */*
// @Produce     json
// @Success     200 {object} map[string]interface{}
// @Router      / [get]
func HealthCheck(c *gin.Context) {
    res := map[string]interface{}{
        "data": "Server is up and running",
    }

    c.JSON(http.StatusOK, res)
}

这是swag init的结果。

{
    "schemes": [
        "http"
    ],
    "swagger": "2.0",
    "info": {
        "description": "This is a sample server server.",
        "title": "Gin Swagger Example API",
        "termsOfService": "http://swagger.io/terms/",
        "contact": {
            "name": "API Support",
            "url": "http://www.swagger.io/support",
            "email": "[email protected]"
        },
        "license": {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
        },
        "version": "2.0"
    },
    "host": "localhost:3000",
    "basePath": "/",
    "paths": {}
}

我的程序有没有问题,如何解决。我正在使用带有 golang 版本 1.18.3 和最新版本 swag 的 Windows 11。 谢谢你。

go swagger go-gin
© www.soinside.com 2019 - 2024. All rights reserved.