-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (41 loc) · 1.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"time"
)
type Response struct {
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
UnixTimestamp int64 `json:"unix_timestamp"`
SourceIP string `json:"src_ip"`
Path string `json:"path"`
Host string `json:"host"`
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
hostname, err := os.Hostname()
if err != nil {
hostname = "N/A"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
res, _ := json.Marshal(Response{
Message: "Hi",
Timestamp: time.Now(),
UnixTimestamp: time.Now().Unix(),
SourceIP: r.RemoteAddr,
Path: r.RequestURI,
Host: hostname,
})
log.Printf("Request from %s to %s\n", r.RemoteAddr, r.RequestURI)
w.Header().Add("Content-Type", "application/json")
w.Write(res)
})
log.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}