-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
102 lines (80 loc) · 1.9 KB
/
server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gofiber/fiber/v2"
//"github.com/gofiber/fiber/v2/middleware/cors"
)
var sidecarAddress string = os.Getenv("SIDECAR_ADDRESS")
func sendResponse(c *fiber.Ctx, content string) error {
return c.JSON(&fiber.Map{
"response": content,
})
}
type requestParam struct {
Name string `json:"name"`
}
type requestBody struct {
Params requestParam `json:"params"`
}
func registerServiceSchema() {
// var localAddress string = "http://localhost:5002"
fmt.Printf("Registering service schema (%s)...\n", sidecarAddress)
postBody := `{
"name": "go-demo",
"settings": {
"baseUrl": "http://go-demo:5002"
},
"actions": {
"hello": "/actions/hello",
"welcome": {
"params": {
"name": "string|no-empty|trim"
},
"handler": "/actions/welcome"
}
},
"events": {
"sample.event": "/events/sample.event"
}
}`
resp, err := http.Post(sidecarAddress+"/v1/registry/services", "application/json", bytes.NewBufferString(postBody))
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println("Response: " + string(body))
}
func main() {
if sidecarAddress == "" {
sidecarAddress = "http://localhost:5103"
}
app := fiber.New()
//app.Use(cors.New())
app.Use(func(c *fiber.Ctx) error {
c.Accepts("application/json")
return c.Next()
})
app.Post("/actions/hello", func(c *fiber.Ctx) error {
return sendResponse(c, "Hello from Go!")
})
app.Post("/actions/welcome", func(c *fiber.Ctx) error {
body := new(requestBody)
c.BodyParser(body)
return sendResponse(c, "Hello "+body.Params.Name+" from Go!")
})
app.Post("events/sample.event", func(c *fiber.Ctx) error {
log.Println("Sample event happened.")
return c.SendStatus(200)
})
registerServiceSchema()
log.Fatal(app.Listen("0.0.0.0:5002"))
}