-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
89 lines (80 loc) · 1.91 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os/exec"
)
type Configure struct {
Listen string `json:"listen"`
Modes []string `json:"modes"`
}
var configure = loadConfigure()
func main() {
if configure == nil {
fmt.Println("please configure the './configure.json' file.")
return
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./pages")))
mux.HandleFunc("/api/switch", handleSwitch)
mux.HandleFunc("/api/status", handleQueryStatus)
fmt.Printf("listen address: http://%s\n", configure.Listen)
if err := http.ListenAndServe(configure.Listen, mux); err != nil {
fmt.Println("error:", err)
}
}
func handleSwitch(rw http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
host, _, _ := net.SplitHostPort(r.RemoteAddr)
modeName := r.FormValue("mode_name")
if !configure.InMode(modeName) {
rw.WriteHeader(http.StatusBadRequest)
return
}
cmd := exec.Command("./hooks/switch.sh", host, modeName)
handleOutput(rw, cmd)
}
func handleQueryStatus(rw http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
host, _, _ := net.SplitHostPort(r.RemoteAddr)
cmd := exec.Command("./hooks/status.sh", host)
handleOutput(rw, cmd)
}
func loadConfigure() *Configure {
data, err := ioutil.ReadFile("./configure.json")
if err != nil {
return nil
}
conf := new(Configure)
if err := json.Unmarshal(data, conf); err != nil {
return nil
}
return conf
}
func handleOutput(rw http.ResponseWriter, cmd *exec.Cmd) {
output, err := cmd.Output()
if err != nil {
rw.WriteHeader(http.StatusServiceUnavailable)
rw.Write([]byte(err.Error()))
return
}
rw.WriteHeader(http.StatusOK)
rw.Write(output)
}
func (conf *Configure) InMode(name string) bool {
for _, mode := range conf.Modes {
if mode == name {
return true
}
}
return false
}