-
Notifications
You must be signed in to change notification settings - Fork 3
/
echoenv.go
56 lines (46 loc) · 1.04 KB
/
echoenv.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"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
// https://github.com/gin-gonic/gin
func main() {
router := gin.Default()
// Normal case
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, getEnvironment(c))
})
// Handle any route
router.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusFound, getEnvironment(c))
})
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run()
}
func getEnvironment(c *gin.Context) gin.H {
hostname, err := os.Hostname()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get hostname: %s", err.Error())
}
return gin.H{
"hostname": hostname,
"env": os.Environ(),
"process": gin.H{
"pid": os.Getpid(),
"uid": os.Getuid(),
"gid": os.Getgid(),
},
"request": gin.H{
"method": c.Request.Method,
"requestURI": c.Request.RequestURI,
"protocol": c.Request.Proto,
"header": c.Request.Header,
"host": c.Request.Host,
"url": c.Request.URL,
},
"clientIP": c.ClientIP(),
}
}