add swagger documentation

This commit is contained in:
Aleksander Cynarski 2019-12-04 19:06:35 +01:00
parent 93bb34ad76
commit f2702297ba
3 changed files with 128 additions and 11 deletions

73
controller/issue.go Normal file
View File

@ -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"),
)
}

27
controller/label.go Normal file
View File

@ -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"),
)
}

39
main.go
View File

@ -2,14 +2,20 @@ package main
import ( import (
"fmt" "fmt"
"gitea-issue/controller"
"gitea-issue/giteaClient" "gitea-issue/giteaClient"
"github.com/caarlos0/env" "github.com/caarlos0/env"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" _ "github.com/jinzhu/gorm/dialects/sqlite"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"net/http" "net/http"
"strconv" "strconv"
"github.com/savaki/swag"
"github.com/savaki/swag/swagger"
) )
var ( var (
@ -45,26 +51,37 @@ func main() {
router := gin.Default() router := gin.Default()
config := cors.DefaultConfig() config := cors.DefaultConfig()
config.AllowOrigins = []string{proxyConfig.ProjectOrigin} config.AllowOrigins = []string{proxyConfig.ProjectOrigin}
router.Use(gin.Logger())
router.Use(cors.New(config)) router.Use(cors.New(config))
v1 := router.Group("/api/v1/issues") proxyApi := swag.New(
{ swag.Title("Gitea issues proxy"),
v1.GET("", getIssues) swag.Description("Gitea issues proxy for separate projects"),
v1.GET("/:id", getIssue) swag.ContactEmail("aleksander@cynarski.pl"),
v1.GET("/:id/comments", getIssueComments) 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") proxyApi.Walk(func(path string, endpoint *swagger.Endpoint) {
{ h := endpoint.Handler.(func(c *gin.Context))
labels.GET("", getLabels) 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() _ = router.Run()
} }
type ( type (
// todoModel describes a todoModel type
userModel struct { userModel struct {
gorm.Model gorm.Model
Email string `json:"email"` Email string `json:"email"`