-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (55 loc) · 1.35 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
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/AleksMa/GraphStealChecker/check"
"github.com/AleksMa/GraphStealChecker/net"
)
func ParseArgs() (port int) {
portArg := flag.Int("p", 8080, "service port")
flag.Parse()
port = *portArg
return
}
func main() {
port := ParseArgs()
path, err := os.Getwd()
http.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, fmt.Sprintf("http://127.0.0.1:%v", port), http.StatusPermanentRedirect)
return
}
programs, limit, subgraphSize, likelihood := net.ParseArgs(r, path)
now := time.Now()
result := check.Check(path, programs, subgraphSize, limit, likelihood)
fmt.Printf("Working %v seconds\n", int(time.Since(now).Seconds()))
tmpl, err := template.New("tmpl").Parse(net.CheckTemplate)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(w, result)
if err != nil {
log.Fatal(err)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("start").Parse(net.StartTemplate)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(w, nil)
if err != nil {
log.Fatal(err)
}
})
fmt.Printf("http://127.0.0.1:%v\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
if err != nil {
log.Fatal(err)
}
}