add swagger documentation
This commit is contained in:
parent
93bb34ad76
commit
f2702297ba
73
controller/issue.go
Normal file
73
controller/issue.go
Normal 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
27
controller/label.go
Normal 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
39
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"`
|
||||
|
Loading…
Reference in New Issue
Block a user