2019-12-03 20:14:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gitea-issue/giteaClient"
|
|
|
|
"github.com/caarlos0/env"
|
2019-12-03 20:53:52 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
2019-12-03 20:14:14 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
db *gorm.DB
|
|
|
|
giteaConfig giteaClient.GiteaConfig
|
2019-12-03 20:53:52 +00:00
|
|
|
proxyConfig ProxyConfig
|
2019-12-03 20:14:14 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 20:53:52 +00:00
|
|
|
type ProxyConfig struct {
|
|
|
|
ProjectOrigin string `env:"PROJECT_ORIGIN,required"`
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:14:14 +00:00
|
|
|
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 {
|
2019-12-04 17:04:06 +00:00
|
|
|
panic(fmt.Sprintf("ENV error: %+v", err.Error()))
|
2019-12-03 20:14:14 +00:00
|
|
|
}
|
2019-12-03 20:53:52 +00:00
|
|
|
if err := env.Parse(&proxyConfig); err != nil {
|
2019-12-04 17:04:06 +00:00
|
|
|
panic(fmt.Sprintf("ENV error: %+v", err.Error()))
|
2019-12-03 20:53:52 +00:00
|
|
|
}
|
2019-12-03 20:14:14 +00:00
|
|
|
|
|
|
|
giteaClient.SetUp(giteaConfig)
|
|
|
|
db.AutoMigrate(&userModel{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
router := gin.Default()
|
2019-12-03 20:53:52 +00:00
|
|
|
config := cors.DefaultConfig()
|
|
|
|
config.AllowOrigins = []string{proxyConfig.ProjectOrigin}
|
|
|
|
router.Use(cors.New(config))
|
2019-12-03 20:14:14 +00:00
|
|
|
|
|
|
|
v1 := router.Group("/api/v1/issues")
|
|
|
|
{
|
2019-12-04 17:04:06 +00:00
|
|
|
v1.GET("", getIssues)
|
|
|
|
v1.GET("/:id", getIssue)
|
|
|
|
v1.GET("/:id/comments", getIssueComments)
|
|
|
|
}
|
2019-12-03 20:14:14 +00:00
|
|
|
|
2019-12-04 17:04:06 +00:00
|
|
|
labels := router.Group("/api/v1/labels")
|
|
|
|
{
|
|
|
|
labels.GET("", getLabels)
|
2019-12-03 20:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = router.Run()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// todoModel describes a todoModel 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)
|
2019-12-04 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLabels(c *gin.Context) {
|
|
|
|
labels, err := giteaClient.GetLabels()
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
c.AsciiJSON(http.StatusOK, labels)
|
|
|
|
}
|
|
|
|
|