-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.39 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
package main
import (
"fmt"
"github.com/gin-contrib/logger"
"ibunny/tracer"
"ibunny/tracker"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func init() {
gin.SetMode(gin.DebugMode)
// Logging to a file
//f, err := os.Create("./logs/runtime.log")
//if err != nil {
// panic("Cannot access log file.")
//}
//
//gin.DefaultWriter = io.MultiWriter(f)
}
func main() {
r := gin.Default()
// Middlewares
r.Use(logger.SetLogger())
// Trusted proxies
r.ForwardedByClientIP = true
errSetProxies := r.SetTrustedProxies([]string{"127.0.0.1"})
if errSetProxies != nil {
return
}
// Homepage
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"page": "home",
})
})
r.GET("/ping", func(c *gin.Context) {
dt := time.Now()
c.JSON(http.StatusOK, gin.H{
"message": "pong",
"now": dt.String(),
})
})
// Debug
r.GET("/debug", func(c *gin.Context) {
dt := time.Now()
c.JSON(http.StatusOK, gin.H{
"code": "SUCCESS",
"now": dt.String(),
"ip": c.ClientIP(),
"userAgent": c.Request.UserAgent(),
"referer": c.Request.Referer(),
})
})
// Redirect by code
r.GET("/:code", tracker.ShortCodeHandler)
// Redirect by structural
r.GET("/go/:id", tracker.GoHandler)
// Trace URL
r.POST("/trace", tracer.TraceHandler)
errRun := r.Run("127.0.0.1:8000")
if errRun != nil {
fmt.Println("Error when start server.")
return
}
}