forked from Treblle/treblle-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (42 loc) · 1.16 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
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/joho/godotenv"
treblle_fiber "github.com/RafaelPiloto10/treblle-go-fiber/trebble_fiber"
)
func main() {
// Define a new Fiber app with config.
app := fiber.New(fiber.Config{})
godotenv.Load()
treblle_fiber.Configure(treblle_fiber.Configuration{
APIKey: os.Getenv("API_KEY"),
ProjectID: os.Getenv("PROJECT_ID"),
AdditionalFieldsToMask: []string{"count"},
IgnorePrefix: []string{"/pi"},
})
app.Use(logger.New(), treblle_fiber.Middleware())
app.Add("POST", "/ping/:id", Ping)
// Start server (with or without graceful shutdown).
app.Listen("localhost:3000")
}
type PingRequest struct {
Count int32 `json:"count"`
}
func Ping(c *fiber.Ctx) error {
body := &PingRequest{}
// Checking received data from JSON body.
if err := c.BodyParser(body); err != nil {
// Return status 400 and error message.
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": true,
"msg": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map {
"error": false,
"msg": fmt.Sprintf("Pinged %v", body.Count),
})
}