-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (80 loc) · 2.01 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
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
103
package main
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
func redeemCode(c *fiber.Ctx, code string, phoneNumber string) {
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: false,
}
client := &http.Client{
Transport: transport,
}
req, err := http.NewRequest(
"POST",
fmt.Sprintf("https://gift.truemoney.com/campaign/vouchers/%s/redeem", code),
strings.NewReader(fmt.Sprintf(`{"mobile": "%s"}`, phoneNumber)),
)
userAgent := "MyApp/" + uuid.NewString()
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/json")
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
return
}
res, err := client.Do(req)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
return
}
c.Status(res.StatusCode).Send(body)
}
func main() {
port := "1500"
err := godotenv.Load()
if err == nil {
port = os.Getenv("PORT")
}
router := fiber.New()
router.Use(logger.New(logger.Config{
Format: "${TagGreen} ${time} [${ip}:${port}] ${latency} ${status} - ${method} ${path} ${body} \n",
TimeFormat: "02/01/2006 15:04:05",
TimeZone: "Local",
}))
router.Post("/redeem/:code", func(c *fiber.Ctx) error {
code := c.Params("code")
var requestBody struct {
MobilePhone string `json:"mobile"`
}
if err := c.BodyParser(&requestBody); err != nil {
c.Status(http.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
redeemCode(c, code, requestBody.MobilePhone)
return nil
})
router.Listen(fmt.Sprintf(":%s", port))
}