135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
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 (
|
|
db *gorm.DB
|
|
giteaConfig giteaClient.GiteaConfig
|
|
proxyConfig ProxyConfig
|
|
)
|
|
|
|
type ProxyConfig struct {
|
|
ProjectOrigin string `env:"PROJECT_ORIGIN,required"`
|
|
}
|
|
|
|
func init() {
|
|
//open a db connection
|
|
var err error
|
|
db, err := gorm.Open("sqlite3", "./database.db")
|
|
if err != nil {
|
|
panic("failed to connect database")
|
|
}
|
|
if err := env.Parse(&giteaConfig); err != nil {
|
|
panic(fmt.Sprintf("ENV error: %+v", err.Error()))
|
|
}
|
|
if err := env.Parse(&proxyConfig); err != nil {
|
|
panic(fmt.Sprintf("ENV error: %+v", err.Error()))
|
|
}
|
|
|
|
giteaClient.SetUp(giteaConfig)
|
|
db.AutoMigrate(&userModel{})
|
|
}
|
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
|
config := cors.DefaultConfig()
|
|
config.AllowOrigins = []string{proxyConfig.ProjectOrigin}
|
|
router.Use(gin.Logger())
|
|
router.Use(cors.New(config))
|
|
|
|
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.PostIssueSwagger(),
|
|
controller.GetIssuesSwagger(),
|
|
controller.GetIssueSwagger(),
|
|
controller.GetIssueCommentsSwagger(),
|
|
controller.GetLabelsSwagger()),
|
|
)
|
|
|
|
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 (
|
|
userModel struct {
|
|
gorm.Model
|
|
Email string `json:"email"`
|
|
Token int `json:"token"`
|
|
}
|
|
)
|
|
|
|
func getIssues(c *gin.Context) {
|
|
issues, err := giteaClient.GetIssues();
|
|
if(err != nil){
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
c.AsciiJSON(http.StatusOK, issues)
|
|
}
|
|
|
|
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 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 getLabels(c *gin.Context) {
|
|
labels, err := giteaClient.GetLabels()
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
c.AsciiJSON(http.StatusOK, labels)
|
|
}
|
|
|