add http router
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 := tx.Bucket([]byte("led"))
|
||||
if b := bucket.Get([]byte("project")); b == nil {
|
||||
return nil
|
||||
} else {
|
||||
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})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user