/
gui.go
95 lines (84 loc) · 2.34 KB
/
gui.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
package lib
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"sync"
)
// ServeMux for concurrency
type ServeMux struct {
mutex sync.RWMutex
}
//ServeHTTP hundle results route
func (mutex *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/":
mutex.mutex.RLock()
defer mutex.mutex.RUnlock()
ShowResultsFile(w, r, "data/results")
return
case r.URL.Path == "/Logo.png":
mutex.mutex.RLock()
defer mutex.mutex.RUnlock()
http.ServeFile(w, r, "data/web/Logo.png")
return
case r.URL.Query().Get("p") != "":
mutex.mutex.RLock()
defer mutex.mutex.RUnlock()
ShowResultsFile(w, r, r.URL.Query().Get("p"))
return
default:
http.Redirect(w, r, "/", http.StatusFound)
return
}
}
//ShowResultsFile see if the directory is in the data/results and show all json files in it
func ShowResultsFile(w http.ResponseWriter, r *http.Request, path string) {
if Existe(path) {
if !strings.HasSuffix(path, ".json") {
htmlTemplate := template.New("index.html")
htmlTemplate, err := htmlTemplate.ParseFiles("data/web/index.html")
Printerr(err, "gui:ShowResult:htmlTemplate.ParseFiles")
list := GetListFile(path)
var listfile []string
for k, v := range list {
if list[k] != "" {
listfile = append(listfile, path+"/"+v)
}
}
htmlTemplate.Execute(w, listfile)
} else {
htmlTemplate := template.New("result.html")
htmlTemplate, err := htmlTemplate.ParseFiles("data/web/result.html")
Printerr(err, "gui:ShowResult:htmlTemplate.ParseFiles")
data := DecodeJSONFile(path)
htmlTemplate.Execute(w, data.WebServers)
}
} else {
Bad(fmt.Sprintf("%s File don't existe.", path))
}
}
//StartListning start listning to the given port
func StartListning() {
Info("Stating server on http://localhost:9011/ | http://[::1]:9011/")
mux := &ServeMux{}
err := http.ListenAndServe(":9011", mux)
if err != nil {
fmt.Printf("StartListning:error: %s\n", err)
}
}
//DecodeJSONFile decode json file and return WebServerslice
func DecodeJSONFile(path string) WebServerslice {
data := WebServerslice{}
jsonfile, err := os.Open(path)
defer jsonfile.Close()
Printerr(err, "gui:ShowResult:os.Open")
jsonParser := json.NewDecoder(jsonfile)
if err = jsonParser.Decode(&data); err != nil {
Printerr(err, "gui:ShowResult:Parsing config file error")
}
return data
}