-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_utils.go
47 lines (40 loc) · 999 Bytes
/
api_utils.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
/*
* Copyright 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package tapir
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
var pongs int = 0
func APIping(appName string, boottime time.Time) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
tls := ""
if r.TLS != nil {
tls = "TLS "
}
log.Printf("APIping: received %s/ping request from %s.\n", tls, r.RemoteAddr)
decoder := json.NewDecoder(r.Body)
var pp PingPost
err := decoder.Decode(&pp)
if err != nil {
log.Println("APIping: error decoding ping post:", err)
}
pongs += 1
hostname, _ := os.Hostname()
response := PingResponse{
Time: time.Now(),
BootTime: boottime,
Client: r.RemoteAddr,
Msg: fmt.Sprintf("%spong from %s @ %s", tls, appName, hostname),
Pings: pp.Pings + 1,
Pongs: pongs,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
}