-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
108 lines (90 loc) · 2.57 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
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
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"time"
)
var (
httpListenAddr = flag.String("listen", ":8080", "Address to listen on")
convertCommand = flag.String("command", "./convert.sh", "Command to execute to convert CSV")
)
func main() {
flag.Parse()
http.Handle("/convert/v4", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
convertRequest("4", w, r)
}))
http.Handle("/convert/v6", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
convertRequest("6", w, r)
}))
log.Println("serving on " + *httpListenAddr)
if err := http.ListenAndServe(*httpListenAddr, nil); err != nil {
log.Fatal(err)
}
}
type responseRecorder struct {
Code int
Size int
http.ResponseWriter
}
func (rr *responseRecorder) WriteHeader(code int) {
rr.Code = code
rr.ResponseWriter.WriteHeader(code)
}
func (rr *responseRecorder) Write(blob []byte) (int, error) {
size, err := rr.ResponseWriter.Write(blob)
rr.Size += size
return size, err
}
func convertRequest(version string, w http.ResponseWriter, r *http.Request) {
rr := &responseRecorder{ResponseWriter: w}
start := time.Now()
defer func() {
log.Printf("%s %s with %d in %s (%d bytes)", r.Method, r.URL.Path, rr.Code, time.Since(start), rr.Size)
}()
if r.Method != "POST" {
sendError(rr, http.StatusMethodNotAllowed, "Only POST requests are allowed")
return
}
if r.Header.Get("Content-Type") != "text/csv" {
sendError(rr, http.StatusNotAcceptable, "Unexpected content type, only text/csv is supported.")
return
}
tmpFile, err := ioutil.TempFile("", "")
if err != nil {
sendError(rr, http.StatusInternalServerError, err.Error())
return
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
n, err := io.Copy(tmpFile, r.Body)
if err != nil {
sendError(rr, http.StatusInternalServerError, err.Error())
return
}
tmpFile.Close()
target := tmpFile.Name() + ".mmdb"
log.Printf("received request with %d bytes, converting from %s to %s", n, tmpFile.Name(), target)
cmd := exec.CommandContext(r.Context(), *convertCommand, tmpFile.Name(), target, version)
output, err := cmd.CombinedOutput()
if err != nil {
if len(output) > 0 {
sendError(rr, http.StatusInternalServerError, string(output))
} else {
sendError(rr, http.StatusInternalServerError, err.Error())
}
return
}
defer os.Remove(target)
rr.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(rr, r, target)
}
func sendError(w http.ResponseWriter, code int, text string) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(code)
w.Write([]byte(text))
}