forked from compat-table/compat-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.go
More file actions
106 lines (89 loc) · 2.52 KB
/
scraper.go
File metadata and controls
106 lines (89 loc) · 2.52 KB
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
var uploadPath string
func readRequest(r *http.Request) (map[string]interface{}, error) {
var err error
buf := bytes.NewBuffer([]byte{})
if _, err = io.Copy(buf, r.Body); err != nil {
return nil, err
}
var data map[string]interface{}
if err = json.Unmarshal(buf.Bytes(), &data); err != nil {
return nil, err
}
if data, ok := data["esVersion"].(string); !ok || data == "" {
return nil, fmt.Errorf("missing 'esVersion' key in uploaded data")
}
if data, ok := data["osVersion"]; !ok || data == "" {
return nil, fmt.Errorf("missing 'osVersion' key in uploaded data")
}
if data, ok := data["playerVersion"]; !ok || data == "" {
return nil, fmt.Errorf("missing 'playerVersion' key in uploaded data")
}
return data, nil
}
func writeFile(data map[string]interface{}) error {
var filename string
var err error
for !os.IsNotExist(err) {
filename = filepath.Join(uploadPath, fmt.Sprintf("%d.json", time.Now().UnixMilli()))
_, err = os.Stat(filename)
}
fileBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
fileBytesReader := bytes.NewReader(fileBytes)
if _, err = io.Copy(file, fileBytesReader); err != nil {
return err
}
return nil
}
func handle(w http.ResponseWriter, r *http.Request) {
var err error
data, err := readRequest(r)
if err != nil {
log.Println("error receiving upload:", err)
w.WriteHeader(400)
return
}
log.Println("received data from:", data["osVersion"])
if err = writeFile(data); err != nil {
log.Println("error writing to file:", err)
w.WriteHeader(500)
return
}
log.Println("saved data from:", data["osVersion"])
w.WriteHeader(200)
}
func main() {
var err error
host := flag.String("host", "127.0.0.1", "host the server will run on")
port := flag.Int("port", 8080, "port the server will run on")
staticPath := flag.String("staticpath", ".", "dir containing static files to serve")
flag.StringVar(&uploadPath, "uploadpath", "./upload", "dir to store uploaded files")
flag.Parse()
if _, err = os.Stat(*staticPath); err != nil {
log.Fatalln(err)
}
http.HandleFunc("/upload", handle)
http.Handle("/", http.FileServer(http.Dir(*staticPath)))
log.Println("starting scraper server on:", fmt.Sprintf("http://%s:%d", *host, *port))
http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
}