github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/gin_app/swagger.go (about) 1 package gin_app 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "os" 7 8 "github.com/gin-gonic/gin" 9 10 "github.com/johnnyeven/libtools/env" 11 ) 12 13 func getSwaggerJSON() []byte { 14 file, err := os.Open("./swagger.json") 15 if err != nil { 16 return []byte{} 17 } 18 defer file.Close() 19 20 data, err := ioutil.ReadAll(file) 21 if err != nil { 22 return []byte{} 23 } 24 25 return data 26 } 27 28 func WithSwagger() gin.HandlerFunc { 29 swaggerJSON := []byte{} 30 31 if !env.IsOnline() { 32 swaggerJSON = getSwaggerJSON() 33 } 34 35 return func(c *gin.Context) { 36 c.Data(http.StatusOK, "application/json", swaggerJSON) 37 } 38 }