-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (45 loc) · 1.04 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
)
func increaseCounter(reptype string) {
req, err := http.NewRequest("POST", "https://kv.valar.dev/valar/"+reptype+"?op=inc", nil)
if err != nil {
log.Println("request invalid:", err)
return
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("VALAR_TOKEN"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("report failed:", err)
return
}
resp.Body.Close()
}
func main() {
// Load script
script, err := ioutil.ReadFile("install.sh")
if err != nil {
panic(err)
}
http.HandleFunc("/report", func(w http.ResponseWriter, r *http.Request) {
repType := r.URL.Query().Get("type")
switch repType {
case "install_finish":
increaseCounter("total_installs")
case "install_start":
increaseCounter("started_installs")
}
w.Write([]byte(""))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(script)
increaseCounter("total_downloads")
})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}