-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-v1.go
75 lines (63 loc) · 1.53 KB
/
api-v1.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
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
)
func slowResponse(c *gin.Context) {
wait, _ := strconv.Atoi(c.Query("wait"))
// make response randomly slower
delay := (time.Duration(WAIT+rand.Intn(500)) * time.Millisecond) + time.Duration(wait)*time.Millisecond
time.Sleep(delay)
fmt.Println(delay)
data, _ := ioutil.ReadAll(c.Request.Body)
fmt.Println(string(data))
c.JSON(http.StatusOK, gin.H{
"item": c.Param("item"),
"id": c.Param("id"),
"reqId": requestid.Get(c),
"delay": delay,
//"receivedData": string(data),
})
}
func v1Status(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"msg": "pong",
"reqId": requestid.Get(c),
})
}
func v1Info(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"version": "0.1.1",
"app_name": "troll",
"client_ip": c.ClientIP(),
"referer": c.Request.Referer(),
"user-agent": c.Request.UserAgent(),
"reqId": requestid.Get(c),
})
}
func v1AllHeaders(c *gin.Context) {
reqDump, _ := httputil.DumpRequest(c.Request, true)
fmt.Println(string(reqDump))
c.JSON(http.StatusOK, gin.H{
"rec_headers": strings.Split(string(reqDump), "\r\n"),
})
}
func v1RoutesAdd(rtG *gin.RouterGroup) {
r := rtG.Group("/")
log.Println("Loading V1 routes...")
r.Use(requestid.New())
r.GET("/status", v1Status)
r.GET("/info", v1Info)
r.GET("/headers", v1AllHeaders)
r.GET("/:item/*id", slowResponse)
r.POST("/:item/*id", slowResponse)
}