-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-tracker.go
144 lines (110 loc) · 3.04 KB
/
index-tracker.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var PATCH_DIR string
func getPatch(timestamp uint64) (string, error) {
query := fmt.Sprintf("cat $(ls -1 | awk -F '-' '$1 > %d') < /dev/null", timestamp)
cmd := exec.Command("sh", "-c", query)
cmd.Dir = PATCH_DIR
output, err := cmd.Output()
if err != nil {
return "", err
}
return string(output), nil
}
func extractTimestampArg(path string) (uint64, error, int) {
parts := strings.Split(path, "/")
arg := parts[1]
if len(parts) != 2 || len(arg) == 0 {
return 0, errors.New("Not Found"), http.StatusNotFound
}
timestamp, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return 0, errors.New("Invalid argument. Expected a UNIX timestamp."), http.StatusBadRequest
}
return timestamp, nil, 0
}
func endpointHandler(w http.ResponseWriter, r *http.Request) {
responseCh := make(chan string)
go func() {
path := r.URL.Path
w.Header().Set("Content-Type", "text/plain")
timestamp, err, httpErrCode := extractTimestampArg(path)
if err != nil {
w.WriteHeader(httpErrCode)
responseCh <- err.Error()
return
}
patch, err := getPatch(timestamp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseCh <- "Query was failed."
return
}
contentLength := strconv.Itoa(len(patch))
w.Header().Set("Content-Length", contentLength)
w.WriteHeader(http.StatusOK)
responseCh <- patch
}()
select {
case response := <-responseCh:
w.Write([]byte(response))
case <-time.After(5 * time.Second): // timeout handling
w.WriteHeader(http.StatusRequestTimeout)
w.Write([]byte("Timeout exceeded"))
}
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (grw gzipResponseWriter) Write(b []byte) (int, error) {
return grw.Writer.Write(b)
}
func gzipMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// only compress if client supports gzip encoding
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
// replace the response writer
w = gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
}
// move to next handler
next.ServeHTTP(w, r)
})
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("API is healthy"))
}
func main() {
PATCH_DIR = os.Getenv("PATCH_DIR")
apiPort := os.Getenv("API_PORT")
if PATCH_DIR == "" {
log.Fatal("PATCH_DIR environment is not present.")
}
if apiPort == "" {
apiPort = "6150"
}
mux := http.NewServeMux()
// Register the handlers
mux.HandleFunc("/health", healthCheckHandler)
mux.HandleFunc("/", endpointHandler)
// Apply the gzip middleware to the entire mux
handler := gzipMiddleware(mux)
fmt.Printf("index-tracker server is listening on port %s for %s\n", apiPort, PATCH_DIR)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", apiPort), handler))
}