Skip to content

Instantly share code, notes, and snippets.

@sofianinho
Last active April 11, 2018 11:28
Show Gist options
  • Save sofianinho/ab9261c01e0c0d6038bb17dd7587e991 to your computer and use it in GitHub Desktop.
Save sofianinho/ab9261c01e0c0d6038bb17dd7587e991 to your computer and use it in GitHub Desktop.
create your swagger v2 (openAPIv2) straight from your gin code
//download swagger-ui as a zip from https://github.com/swagger-api/swagger-ui/tree/v2.2.10
//make sure to change line 40 of file: swagger-ui/dist/idex.html to: "/swagger.json" in the url var
//this is how the swagger-ui knows to ask for the specification at the right place
//You can enrich your swagger api by using the swag.XXXX functions that return "Option" type. example: Description, Host, License...
// These functions are here: https://godoc.org/github.com/savaki/swag#Option
//the original code of the author from swag has been modified
//original lionk: https://raw.githubusercontent.com/savaki/swag/master/examples/gin/main.go
// Copyright 2017 Matt Ho
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"io"
"net/http"
"time"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin"
"github.com/savaki/swag"
"github.com/savaki/swag/endpoint"
"github.com/savaki/swag/swagger"
"github.com/sirupsen/logrus"
)
func handle(c *gin.Context) {
io.WriteString(c.Writer, "This is not the handler you are looking for")
}
// Category example from the swagger pet store
type Category struct {
ID int64 `json:"category"`
Name string `json:"name"`
}
// Pet example from the swagger pet store
type Pet struct {
ID int64 `json:"id"`
Category Category `json:"category"`
Name string `json:"name"`
PhotoUrls []string `json:"photoUrls"`
Tags []string `json:"tags"`
}
func main() {
post := endpoint.New("post", "/pet", "Add a new pet to the store",
endpoint.Handler(handle),
endpoint.Description("Additional information on adding a pet to the store"),
endpoint.Body(Pet{}, "Pet object that needs to be added to the store", true),
endpoint.Response(http.StatusOK, Pet{}, "Successfully added pet"),
)
get := endpoint.New("get", "/pet/{petId}", "Find pet by ID",
endpoint.Handler(handle),
endpoint.Path("petId", "integer", "ID of pet to return", true),
endpoint.Response(http.StatusOK, Pet{}, "successful operation"),
)
api := swag.New(
swag.Endpoints(post, get),
swag.Description("My timey wimey API thingy"),
)
router := gin.New()
logrus.SetFormatter(&logrus.JSONFormatter{})
router.Use(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true))
api.Walk(func(path string, endpoint *swagger.Endpoint) {
h := endpoint.Handler.(func(c *gin.Context))
path = swag.ColonPath(path)
router.Handle(endpoint.Method, path, h)
})
enableCors := true
router.GET("/swagger.json", gin.WrapH(api.Handler(enableCors)))
router.Static("/swagger", "./swagger-ui/dist")
http.ListenAndServe(":8080", router)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment