-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
53 lines (45 loc) · 1.04 KB
/
client.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
_ "golang.org/x/net/context"
"net/http"
)
import dapr "github.com/dapr/go-sdk/client"
var Client dapr.Client
func InitDapr() {
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
Client = client
}
func main() {
InitDapr()
r := gin.Default()
v1 := r.Group("/api")
Request(v1.Group("/"))
Health(v1.Group("/"))
_ = r.Run(":40002")
}
func Request(router *gin.RouterGroup) {
ctx:=context.Background()
router.POST("/request", func(context *gin.Context) {
content := &dapr.DataContent{
ContentType: "text/plain",
Data: []byte("hello"),
}
res, err := Client.InvokeMethodWithContent(ctx, "dapr-sample-java", "/sampleJavaApp/name", "post", content)
if err != nil {
log.Error().Err(err).Msg("")
return
}
context.JSON(http.StatusOK, gin.H{"response": string(res)})
})
}
func Health(router *gin.RouterGroup) {
router.GET("/health", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"response": "healthy"})
})
}