-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (36 loc) · 1.03 KB
/
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
39
40
41
42
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/ushieru/serendipia/src/gateway"
"github.com/ushieru/serendipia/src/service_registry"
)
func main() {
app := fiber.New()
proxy := gateway.NewGateway()
go proxy.ServiceRegistry.InitTick()
app.Get("/services", func(ctx *fiber.Ctx) error {
services := make(map[string]serviceregistry.Service)
proxy.ServiceRegistry.Services.Range(func(key, value any) bool {
services[key.(string)] = value.(serviceregistry.Service)
return true
})
return ctx.JSON(services)
})
app.Post("/services", func(ctx *fiber.Ctx) error {
params := &struct {
ServiceName string `json:"service_name"`
ServicePort string `json:"service_port"`
}{}
if err := ctx.BodyParser(¶ms); err != nil {
return ctx.SendStatus(400)
}
serviceKey := proxy.
ServiceRegistry.
Register(params.ServiceName, ctx.IP(), ctx.Protocol(), params.ServicePort)
return ctx.SendString(serviceKey)
})
app.Use("/*", func(c *fiber.Ctx) error {
return proxy.CallService(c)
})
app.Listen(":3000")
}