fiber-swagger

command
v0.0.0-...-83be562 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 4 Imported by: 0

README

How to use Swagger

Check the go version:

$ go version
go version go1.19.3 linux/amd64

Install swag (github: swaggo/swag):

$ go install github.com/swaggo/swag/cmd/swag@latest
$ swag -v
swag version v1.8.9

Create the project directory, and install go libraries: Ref: gofiber/swagger

$ mkdir fiber-swagger
$ cd fiber-swagger

$ go mod init github.com/mukappalambda/fiber-swagger
$ go get -u github.com/gofiber/fiber/v2
$ go get -u github.com/gofiber/swagger

Edit the file main.go:

package main

import (
        "log"

        "github.com/gofiber/fiber/v2"
        "github.com/gofiber/swagger"
)

// @title                       Fiber Example API
// @version             1.0
// @description This is a sample swagger for Fiber
// @termsOfService      http://swagger.io/terms/
// @contact.name        API Support
// @contact.email       fiber@swagger.io
// @license.name        Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host                        localhost:8080
// @BasePath            /
func main() {
        app := fiber.New()
        app.Get("/docs/*", swagger.HandlerDefault)

        app.Get("/users/", GetAllUsers)
        log.Fatal(app.Listen(":8080"))

}

// GetAllUsers is a function to get all users data from database
//
//      @Summary                Get all users
//      @Description    Get all users
//      @Tags                   users
//      @Router                 /users [get]
func GetAllUsers(c *fiber.Ctx) error {
        return c.Send([]byte("All books"))
}

Current project directory layout:

.
├── go.mod
├── go.sum
└── main.go

0 directories, 3 files

Create the docs:

$ swag init
2022/12/18 13:10:18 Generate swagger docs....
2022/12/18 13:10:18 Generate general API Info, search dir:./
2022/12/18 13:10:18 create docs.go at  docs/docs.go
2022/12/18 13:10:18 create swagger.json at  docs/swagger.json
2022/12/18 13:10:18 create swagger.yaml at  docs/swagger.yaml

Current project directory layout:

.
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
└── main.go

1 directory, 6 files

Import the docs in main.go

import (
        "log"

        "github.com/gofiber/fiber/v2"
        "github.com/gofiber/swagger"
        _ "github.com/mukappalambda/fiber-swagger/docs"
)

Run main.go:

$ go run main.go

 ┌───────────────────────────────────────────────────┐
 │                   Fiber v2.40.1                   │
 │               http://127.0.0.1:8080               │
 │       (bound on host 0.0.0.0 and port 8080)       │
 │                                                   │
 │ Handlers ............. 4  Processes ........... 1 │
 │ Prefork ....... Disabled  PID ............. 78249 │
 └───────────────────────────────────────────────────┘

Visit http://localhost:8080/docs/, and you will see the docs generated by swagger.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL