Last active
February 16, 2021 05:28
-
-
Save mr-pascal/0d529cf8aa613d977218614ea18a57be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
// Import Fiber Swagger | |
"github.com/arsmn/fiber-swagger/v2" | |
// Import Go Fiber | |
"github.com/gofiber/fiber/v2" | |
// Side Effect import for auto-generated swagger documentation | |
_ "medium_go_fiber_swagger/docs" | |
) | |
// @title Fiber Example API | |
// @version 1.0 | |
// @description This is a sample swagger for Fiber | |
// @contact.name API Support | |
// @contact.email [email protected] | |
// @host localhost:3000 | |
// @BasePath / | |
func main() { | |
// Create new Fiber application | |
app := fiber.New() | |
// Add endpoint to serve swagger documentation | |
app.Get("/swagger/*", swagger.New(swagger.Config{ // custom | |
URL: "/swagger/doc.json", | |
DeepLinking: false, | |
})) | |
// Add endpoint to get an item by it's ID | |
app.Get("/api/item/:id", GetItem) | |
// Listen on the port '3000' | |
app.Listen(":3000") | |
} | |
// GetItem godoc | |
// @Summary Get an item | |
// @Description Get an item by its ID | |
// @ID get-item-by-int | |
// @Accept json | |
// @Produce json | |
// @Tags Item | |
// @Param id path int true "Item ID" | |
// @Success 200 {object} Item | |
// @Failure 400 {object} HTTPError | |
// @Failure 404 {object} HTTPError | |
// @Failure 500 {object} HTTPError | |
// @Router /api/item/{id} [get] | |
func GetItem(c *fiber.Ctx) error { | |
// Create new Item and returns it | |
return c.JSON(Item{ | |
Id: c.Params("id"), | |
}) | |
} | |
type Item struct { | |
Id string | |
} | |
type HTTPError struct { | |
Status string | |
Message string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment