Open ndbroadbent opened 9 years ago
+1
+1
+1, swagger is very useful for API development
:+1:
@phalt @redstrike @matejkramny @rogeriomarques @ndbroadbent I'll add support for it with v0.6 in mind. Thank you for submitting this request.
+1
I'm gonna raise the biddings on this issue, Ok for Swagger, but why not RAML? In my personal experience using both swagger and raml barebone, the latter has a better explained specification and has a more pragmatic approach.
I'll explore what can be done with my limited golang skills, and keep you posted
One of the main concerns in thinking this feature out, is design this swagger/raml feature producing a base json/raml document that can be extended to let the user exploit the full-feature set of their API specifications.
I hardly seen that in other frameworks, but I'm hoping someone could propose some input regarding this
+1 for swagger support
I'm surprised there isn't someone who has created a parser that looks at an AST + attached routes for net/http to generate Swagger/RAML style documentation.
If it isn't a project, perhaps that would be the better method of implementation for this? Rather than having it part of Gin core, if it was a separate binary that scanned a package?
I think to externalize API documentation, gin.router should be exported (so Router). Moreso, I think model validation should be expanded before implementing API documentation, since there is a big difference between the vastity of API validation options and gin ones.
Noticed Swagger support was on the README checklist for V1. That seems to be gone now. Should we not have our hopes up for this being released?
@dustinwtf We do not need swagger support in 1.0. 1.0 is about having a stable API, production-ready and performance.
Once 1.0 final is reached, we can focus in features. Also, I do not think the swagger support should be part of Gin core. Probably a different package.
Agreed! That's definitely a good focus. Just wondered considering it was there and disappeared.
Is there an easy way to get all the registered routes now? Like a map of url -> handlers or something.
@nazwa no, we need to implement it. Two solutions:
While the second way is the easiest to implement it adds a unneeded overhead and complexity all over the place. So I would try to iterate the trees, it can not be that hard.
*Engine
should have a method called Routes() []RouteInfo or something like that. We do not want to expose the trees directly.
Also, since the router is not longer dependent of the HttpRouter package, we have access to all the info.
something like this, I do not have time to do it just now. If anybody is interested, please create a PR!!
func (engine *Engine) Routes() (routes []RouteInfo) {
for _, root := range engine.trees {
routes = iterate(routes, root)
}
return routes
}
func iterate(routes []RouteInfo, root *node) []RouteInfo {
for _, node := range root.children {
routes = iterate(routes, node)
}
if root.handlers != nil {
routes = append(routes,root.path)
}
return routes
}
Yea, something like this would be perfect. I'll try to have a proper play with it in the evening, as it definitely would be useful in the long run.
@nazwa @dustinwtf @ndbroadbent https://github.com/gin-gonic/gin/commit/45dd7776938fddc914e8a2fe60367a3eb99e1e53
router := gin.New()
router.GET("/favicon.ico", handler)
router.GET("/", handler)
group := router.Group("/users")
group.GET("/", handler)
group.GET("/:id", handler)
group.POST("/:id", handler)
router.Static("/static", ".")
fmt.Println(router.Routes())
[{GET /} {GET /users/} {GET /users/:id} {GET /favicon.ico} {GET /static/*filepath} {POST /users/:id} {HEAD /static/*filepath}]
Here's a proposal for auto API documentation:
{"GET", "/users", "main.usersHandler"}
beego special comments:
// @Description get all users
// @Success 200 {object}
func usersHandler(c *gin.Context) {
// ..
}
The swagger generator will use the information from the comments + engine.Routes() to generate the documentation.
This is perfect, @manucorporat! When do you see this being merged into master?
@manucorporat Looks great, thanks!
Is there any reason to return the handler function name as a string, not as a Func
pointer?
If I understand it correctly - the final goal is to find the comments before the functions and parse them to generate Swagger/RAML docs? Probably the file name and line number would be more helpful (like returned by the Func.FileLine
). Then the parser could just read the lines above it.
@zserge A pointer to a function is too low level, in my opinion, a high level API (like Gin) should hide low level details, it makes everybody's code safer. For example: if the middleware API would allow to jump between handlers and do crazy things, the whole community of middleware would be more error-prone and unsecure.
I like the idea of providing the filename and line, that's why I created the RouteInfo
struct, we can add new fields in a completely backcompatible way! what about FileName string
and FileLine uint
?
https://github.com/gin-gonic/gin/blob/master/gin.go#L37-L41
Is there a working example of how to use this with one of the swagger generators?
@MattDavisRV +1
+1
+1 on a example of how to use this
Ok, here is an example, but it requires modifying the Gin codes slightly
swagger.go
file at the root of your project I may have missed something, let me know and I will edit this.
You may only have to add the HandleHTTP field to Engine. I wanted the same JSON responses on OPTIONS requests.
Cheers!
~ Tony
go get github.com/yvasiyarov/swagger
git clone https://github.com/swagger-api/swagger-ui
cd swagger-ui
npm install
gulp
If you make any modifications to swagger-ui, be sure to rerun gulp
github.com/gin-gonic/gin/gin.go
Add the following fields to the Engine definition [~ line 32]
HandleOPTIONS bool
ApiDescriptionsJson map[string]string
Add fields to the engine.New() call [~ line 107 now]
HandleOPTIONS: true,`
The remainder of step 3 may be unnecessary, however you will be a better OPTIONS netizen if you do.
Change handleHTTPRequest to the following [~ line 274 now]
func (engine *Engine) handleHTTPRequest(context *Context) {
httpMethod := context.Request.Method
path := context.Request.URL.Path
// Find root of the tree for the given HTTP method
t := engine.trees
for i, tl := 0, len(t); i < tl; i++ {
if t[i].method == httpMethod {
root := t[i].root
// Find route in tree
handlers, params, tsr := root.getValue(path, context.Params)
if handlers != nil {
context.handlers = handlers
context.Params = params
context.Next()
context.writermem.WriteHeaderNow()
return
} else if httpMethod != "CONNECT" && path != "/" {
if tsr && engine.RedirectTrailingSlash {
redirectTrailingSlash(context)
return
}
if engine.RedirectFixedPath && redirectFixedPath(context, root, engine.RedirectFixedPath) {
return
}
}
break
}
}
if httpMethod == "OPTIONS" {
// Handle OPTIONS requests
if engine.HandleOPTIONS {
if allow := engine.allowed(path, httpMethod, context.Params); len(allow) > 0 {
apiKey := strings.Trim(context.Request.RequestURI, "/")
if json, ok := engine.ApiDescriptionsJson[apiKey]; ok {
t, e := template.New("desc").Parse(json)
if e != nil {
serveError(context, http.StatusInternalServerError, []byte("Internal Server Error 500\n"))
return
}
t.Execute(context.Writer, context.Request.Host)
context.writermem.Header()["Allow"] = []string{allow}
}
context.writermem.WriteHeaderNow()
return
}
}
} else {
// TODO: unit test
if engine.HandleMethodNotAllowed {
for _, tree := range engine.trees {
if tree.method != httpMethod {
if handlers, _, _ := tree.root.getValue(path, nil); handlers != nil {
context.handlers = engine.allNoMethod
serveError(context, 405, default405Body)
return
}
}
}
}
}
context.handlers = engine.allNoRoute
serveError(context, 404, default404Body)
}
Add the following just above the handleHTTPRequest
func (engine *Engine) allowed(path, reqMethod string, po Params) (allow string) {
if path == "*" { // server-wide
for _, T := range engine.trees {
if T.method == "OPTIONS" {
continue
}
// add request method to list of allowed methods
if len(allow) == 0 {
allow = T.method
} else {
allow += ", " + T.method
}
}
} else { // specific path
for _, T := range engine.trees {
// Skip the requested method - we already tried this one
if T.method == reqMethod || T.method == "OPTIONS" {
continue
}
handle, _, _ := engine.trees.get(T.method).getValue(path, po)
if handle != nil {
// add request method to list of allowed methods
if len(allow) == 0 {
allow = T.method
} else {
allow += ", " + T.method
}
}
}
}
if len(allow) > 0 {
allow += ", OPTIONS"
}
return
}
swagger.go
file at the root of your projectBe sure to update SWAGDIR
to the location of the swagger-ui
repository
package main
import (
"flag"
"net/http"
"strings"
"text/template"
"github.com/gin-gonic/gin"
)
var SWAGDIR = "../swagger-ui/dist"
var staticContent = flag.String("staticPath", SWAGDIR, "Path to folder with Swagger UI")
var apiurl = flag.String("api", "http://localhost:8080", "The base path URI of the API service")
func swaggify(router *gin.Engine) {
// Swagger Routes
router.GET("/", IndexHandler)
router.Static("/swagger-ui", *staticContent)
for apiKey := range apiDescriptionsJson {
router.GET("/"+apiKey+"/", ApiDescriptionHandler)
}
// API json data
router.ApiDescriptionsJson = apiDescriptionsJson
}
func IndexHandler(c *gin.Context) {
w := c.Writer
r := c.Request
isJsonRequest := false
if acceptHeaders, ok := r.Header["Accept"]; ok {
for _, acceptHeader := range acceptHeaders {
if strings.Contains(acceptHeader, "json") {
isJsonRequest = true
break
}
}
}
if isJsonRequest {
t, e := template.New("desc").Parse(resourceListingJson)
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
t.Execute(w, *apiurl)
} else {
http.Redirect(w, r, "/swagger-ui/", http.StatusFound)
}
}
func ApiDescriptionHandler(c *gin.Context) {
w := c.Writer
r := c.Request
apiKey := strings.Trim(r.RequestURI, "/")
if json, ok := apiDescriptionsJson[apiKey]; ok {
t, e := template.New("desc").Parse(json)
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
t.Execute(w, *apiurl)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
by calling
swaggify(router)
add the following to your main.go
// @APIVersion 0.0.0
// @APITitle title
// @APIDescription description
// @Contact user@domain.com
// @TermsOfServiceUrl http://...
// @License MIT
// @LicenseUrl http://osensource.org/licenses/MIT
swagger -apiPackage="path/to/your/api" -ignore "$vendor"
note path/to/your/api
is a `go getable path
Any progress with this feature?
I'm beginner in go; I began to rewrite a comment parser to generate swagger.json. The project is still on-going.
I decided to write my own parser as I could not manage to use https://github.com/yvasiyarov/swagger (missing deps with context
). But I use the same wording.
Some data are not in comments, such host
, scheme
and basepath
, as it depends on the environment where the API is deployed.
My project is only based on comments and does not parse the code. So it can be used with other frameworks https://github.com/landru29/swaggo
Some features are still missing. For today, only basics work.
@landru29 you need to run yvasiyarov/swagger with -ignore
+1
++1
+1
Please use the reactions or the subscribe button.
👍
+1
I've been using swagger with go using code to generate the swagger file. The project is
and I've included an example using gin, https://github.com/savaki/swag/blob/master/examples/gin/main.go
Feedback would be greatly appreciated.
@savaki There's no license in github.com/savaki/swag so I can't use it. Any chance of it getting an open source license?
+1
+1
+65535
Just wondering will there be a PR to this repo about it? I would love to use it a official way :(
@javierprovecho when will gin official support swagger?Thanks!
+1
+1 👍
One thing I really like about go-restful is their built-in Swagger integration. Check out the example project: https://github.com/emicklei/mora#swagger
Some things I really love about Gin is the focus on performance, and overall, it just seems like a much better designed framework. However, I think that auto-generated documentation with Swagger is pretty awesome. It makes it easy for new engineers to get up-to-speed quickly by reading through the docs, and I especially love the way you can auto-generate API clients for lots of different languages.
I'm not sure how involved this change would be, I'm not sure if this could be done as an extension. But we could probably take lots of inspiration from (or just copy) the swagger code from go-restful (MIT licensed).
Anyway, this is not a huge priority for me, just a nice to have.