-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (44 loc) · 1.04 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
53
54
55
56
package main
import (
"fmt"
"os"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/google/uuid"
)
var printBody = os.Getenv("PRINT_BODY") == "true"
var printHeaders = os.Getenv("PRINT_HEADERS") == "true"
func serverAddr() string {
port := os.Getenv("PORT")
if port == "" {
return ":5050"
}
return ":" + port
}
func handler(c *fiber.Ctx) error {
rid := strings.Split(uuid.New().String(), "-")[0]
fmt.Printf("[%s] %s %s\n", rid, c.Request().Header.Method(), c.Request().URI().String())
if printBody {
fmt.Printf("[%s] Start Body:\n", rid)
fmt.Printf("%s", c.Body())
fmt.Printf("\n[%s] End Body\n", rid)
}
if printHeaders {
fmt.Printf("[%s] Start Headers:\n", rid)
fmt.Printf("%s", c.Request().Header.Header())
fmt.Printf("\n[%s] End Headers\n", rid)
}
return c.SendStatus(200)
}
func main() {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Use(cors.New())
app.Use(handler)
err := app.Listen(serverAddr())
if err != nil {
panic(err.Error())
}
}