52 lines
942 B
Go
52 lines
942 B
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"rpi-ci-led/pkg/database"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
func New() *gin.Engine {
|
|
e := gin.New()
|
|
|
|
e.Use(gin.Logger())
|
|
e.Use(gin.Recovery())
|
|
|
|
return e
|
|
}
|
|
|
|
func SetRoutes(e *gin.Engine) {
|
|
e.GET("/", func(c *gin.Context) {
|
|
c.String(200, "ok")
|
|
})
|
|
|
|
e.GET("/projects", func(c *gin.Context) {
|
|
var list []string = make([]string, 0)
|
|
db := database.Get()
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
bucket, err := tx.CreateBucketIfNotExists([]byte("led"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if b := bucket.Get([]byte("project")); b == nil {
|
|
fmt.Println("b ::: ", b)
|
|
return nil
|
|
} else {
|
|
fmt.Println("b ::: ", b)
|
|
if err := json.Unmarshal(b, &list); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(500, gin.H{"message": "database read fail"})
|
|
}
|
|
|
|
c.JSON(200, gin.H{"list": list})
|
|
})
|
|
}
|