swaggo / gin-swagger

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

Getting 404 only for the swagger endpoint #186

Closed pedromassango closed 2 years ago

pedromassango commented 2 years ago

Hi there. In my code everything seems to be in place (I can invoke all other endpoints but the swagger does not work.

Here is my main.go file:

package main

import (
    docs "app/public/docs"
    v1 "app/router/v1"
    "github.com/gin-gonic/gin"
    swaggerfiles "github.com/swaggo/files"
    ginSwagger "github.com/swaggo/gin-swagger"
)

func main() {
    router := gin.New()
    router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

    docs.SwaggerInfo.BasePath = "/api/v1"
    docs.SwaggerInfo.Version = "1.0"
    docs.SwaggerInfo.Host = "app.swagger.io"

    version1 := router.Group("/api/v1")
    {
        v1.InitRoutes(version1)
    }

    router.Run(":8080") // listen and serve on 0.0.0.0:8080
}

Please note that the docs has been generated successfully:

My swagger.json

{
    "swagger": "2.0",
    "info": {
        "contact": {}
    },
    "basePath": "/api/v1",
    "paths": {
        "/auth/login": {
            "post": {
                "description": "Authenticate the user in the system.",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "example"
                ],
                "summary": "Login",
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "/auth/signup": {
            "post": {
                "description": "Sign up the user in the system.",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "example"
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

My swagger.yaml

basePath: /api/v1
info:
  contact: {}
paths:
  /auth/login:
    post:
      consumes:
      - application/json
      description: Authenticate the user in the system.
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            type: string
      summary: Login
      tags:
      - example
  /auth/signup:
    post:
      consumes:
      - application/json
      description: Sign up the user in the system.
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            type: string
      tags:
      - example
swagger: "2.0"

Am I missing something?

pedromassango commented 2 years ago

Here is a preview of one of my functions:

// @BasePath /api/v1
// @description Authenticate the user in the system.
// @tags example
// @accept json
// @produce json
// @success 200 {string} Helloworld
// @Router /auth/login [post]
func Login(c *gin.Context) {
    c.JSON(http.StatusOK, "Success")
}