This repository was archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (58 loc) · 1.43 KB
/
main.go
File metadata and controls
75 lines (58 loc) · 1.43 KB
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
package main
import (
"os"
"fmt"
"log"
"context"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/otelfiber"
telemetry "github.com/fireacademy/telemetry"
)
func getPort() string {
port := os.Getenv("LEAFLET_LISTEN_PORT")
if port == "" {
panic("LEAFLET_LISTEN_PORT not set")
}
return port
}
func Index(c *fiber.Ctx) error {
return c.SendString("Leaflet server is running.")
}
func ReadinessCheck(c *fiber.Ctx) error {
ready := IsReady()
if ready {
return c.SendString("OK")
}
return c.Status(400).SendString("NOT READY BRO")
}
func ProxyToRPCEndpoint(c *fiber.Ctx) error {
endpoint := c.Params("endpoint")
body := string(c.Body())
if len(body) < 2 {
body = "{}"
}
ctx := c.UserContext()
resp, err := DoRPCRequest(ctx, "POST", endpoint, body)
if err != nil {
telemetry.LogError(ctx, err, "error while calling RPC")
return c.Status(500).JSON(fiber.Map{
"success": false,
"message": "error while calling RPC",
})
}
c.Set("Content-Type", "application/json")
return c.Status(resp.StatusCode).SendStream(resp.Body)
}
func main() {
cleanup := telemetry.Initialize()
defer cleanup(context.Background())
SetupRPCClient()
app := fiber.New()
app.Use(otelfiber.Middleware())
app.Get("/", Index)
app.Get("/ready", ReadinessCheck)
app.Get("/rpc/:endpoint", ProxyToRPCEndpoint)
app.Post("/rpc/:endpoint", ProxyToRPCEndpoint)
port := getPort()
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
}