From f2702297badceeef5b8bc0634c16df8dcca9c3b0 Mon Sep 17 00:00:00 2001 From: Aleksander Cynarski Date: Wed, 4 Dec 2019 19:06:35 +0100 Subject: [PATCH] add swagger documentation --- controller/issue.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ controller/label.go | 27 +++++++++++++++++ main.go | 39 +++++++++++++++++------- 3 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 controller/issue.go create mode 100644 controller/label.go diff --git a/controller/issue.go b/controller/issue.go new file mode 100644 index 0000000..122709d --- /dev/null +++ b/controller/issue.go @@ -0,0 +1,73 @@ +package controller + +import ( + "code.gitea.io/sdk/gitea" + "gitea-issue/giteaClient" + "github.com/gin-gonic/gin" + "github.com/savaki/swag/endpoint" + "github.com/savaki/swag/swagger" + "net/http" + "strconv" + "fmt" +) + +func GetIssues(c *gin.Context) { + issues, err := giteaClient.GetIssues(); + if(err != nil){ + c.AbortWithStatus(http.StatusNotFound) + } + c.AsciiJSON(http.StatusOK, issues) +} + +func GetIssuesSwagger() (*swagger.Endpoint){ + return endpoint.New("get", "/issues", "List project issues", + endpoint.Handler(GetIssues), + endpoint.Description("Get all issues"), + endpoint.Tags("issues"), + endpoint.Response(http.StatusOK, []gitea.Issue{}, "Gitea Issue list"), + ) +} + +func GetIssue(c *gin.Context) { + issueId, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + fmt.Println(fmt.Sprintf("ParseInt err: %+v", err)) + c.AsciiJSON(http.StatusNotFound, err.Error()) + } + issue, err := giteaClient.GetIssue(issueId) + if err != nil { + c.AbortWithStatus(http.StatusNotFound) + } + c.AsciiJSON(http.StatusOK, issue) +} + +func GetIssueSwagger() (*swagger.Endpoint){ + return endpoint.New("get", "/issue/:id", "Get one issue", + endpoint.Handler(GetIssue), + endpoint.Description("Get one issue"), + endpoint.Tags("issues"), + endpoint.Response(http.StatusOK, gitea.Issue{}, "Gitea Issue"), + ) +} + +func GetIssueComments(c *gin.Context) { + issueId, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + fmt.Println(fmt.Sprintf("ParseInt err: %+v", err)) + c.AsciiJSON(http.StatusNotFound, err.Error()) + } + issueComments, err := giteaClient.GetIssueComments(issueId) + if err != nil { + c.AbortWithStatus(http.StatusNotFound) + } + c.AsciiJSON(http.StatusOK, issueComments) +} + +func GetIssueCommentsSwagger() (*swagger.Endpoint){ + return endpoint.New("get", "/issue/:id/comments", "Get issue comments", + endpoint.Handler(GetIssue), + endpoint.Description("Get issue comments"), + endpoint.Tags("issues"), + endpoint.Response(http.StatusOK, []gitea.Comment{}, "Gitea issue comments"), + ) +} diff --git a/controller/label.go b/controller/label.go new file mode 100644 index 0000000..7548b87 --- /dev/null +++ b/controller/label.go @@ -0,0 +1,27 @@ +package controller + +import ( + "code.gitea.io/sdk/gitea" + "gitea-issue/giteaClient" + "github.com/gin-gonic/gin" + "github.com/savaki/swag/endpoint" + "github.com/savaki/swag/swagger" + "net/http" +) + +func GetLabels(c *gin.Context) { + labels, err := giteaClient.GetLabels() + if err != nil { + c.AbortWithStatus(http.StatusNotFound) + } + c.AsciiJSON(http.StatusOK, labels) +} + +func GetLabelsSwagger() (*swagger.Endpoint){ + return endpoint.New("get", "/labels", "List project labels", + endpoint.Handler(GetLabels), + endpoint.Description("Get all labels"), + endpoint.Tags("labels"), + endpoint.Response(http.StatusOK, []gitea.Label{}, "Gitea labels list"), + ) +} \ No newline at end of file diff --git a/main.go b/main.go index b2048f3..9b8b8d3 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,20 @@ package main import ( "fmt" + "gitea-issue/controller" "gitea-issue/giteaClient" "github.com/caarlos0/env" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" + ginSwagger "github.com/swaggo/gin-swagger" + "github.com/swaggo/gin-swagger/swaggerFiles" "net/http" "strconv" + "github.com/savaki/swag" + "github.com/savaki/swag/swagger" + ) var ( @@ -45,26 +51,37 @@ func main() { router := gin.Default() config := cors.DefaultConfig() config.AllowOrigins = []string{proxyConfig.ProjectOrigin} + router.Use(gin.Logger()) router.Use(cors.New(config)) - v1 := router.Group("/api/v1/issues") - { - v1.GET("", getIssues) - v1.GET("/:id", getIssue) - v1.GET("/:id/comments", getIssueComments) - } + proxyApi := swag.New( + swag.Title("Gitea issues proxy"), + swag.Description("Gitea issues proxy for separate projects"), + swag.ContactEmail("aleksander@cynarski.pl"), + swag.License("MIT", "https://opensource.org/licenses/MIT"), + swag.Tag("issues", "Gitea issues proxy endpoints"), + swag.Tag("labels", "Gites labels for issue"), + swag.Endpoints( + controller.GetIssuesSwagger(), + controller.GetIssueSwagger(), + controller.GetIssueCommentsSwagger(), + controller.GetLabelsSwagger()), + ) - labels := router.Group("/api/v1/labels") - { - labels.GET("", getLabels) - } + proxyApi.Walk(func(path string, endpoint *swagger.Endpoint) { + h := endpoint.Handler.(func(c *gin.Context)) + path = swag.ColonPath(path) + router.Handle(endpoint.Method, path, h) + }) + router.GET("/swagger.json", gin.WrapH(proxyApi.Handler(true))) + url := ginSwagger.URL("/swagger.json") + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) _ = router.Run() } type ( - // todoModel describes a todoModel type userModel struct { gorm.Model Email string `json:"email"`