-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
38 lines (33 loc) · 871 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/gin-gonic/gin"
"k8sapi-lowcode/pkg/utils"
)
type InputPod struct {
ApiVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Name string `json:"name"`
}
func main() {
r := gin.New()
r.POST("/", func(c *gin.Context) {
pod := &InputPod{}
if err := c.ShouldBindJSON(pod); err != nil {
c.AbortWithStatusJSON(400, gin.H{"err": err.Error()})
return
}
cc := cuecontext.New()
cv := cc.CompileBytes(utils.MustLoadFile("./test.cue"))
cv = cv.FillPath(cue.ParsePath("param"), cc.Encode(pod))
b, err := cv.LookupPath(cue.ParsePath("pod")).MarshalJSON()
if err != nil {
c.AbortWithStatusJSON(400, gin.H{"err": err.Error()})
return
}
c.Writer.Header().Add("content-type", "application/json")
c.Writer.Write(b)
})
r.Run(":8080")
}