-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
57 lines (52 loc) · 1.02 KB
/
server.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
package main
import (
"log"
"net/http"
"os"
"os/exec"
)
type Server struct {
config *Config
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rpath := r.URL.Path
if rpath == "/tasks" {
s.showTasks(w)
return
}
task := s.getTask(rpath)
if task == nil {
w.WriteHeader(404)
return
}
r.ParseForm()
if r.Form.Get("secret") != task.Secret {
log.Println("User provided bad secret", r.Form.Get("secret"))
w.WriteHeader(401)
return
}
cmdstrs := task.render(map[string]string{})
for _, cmdstr := range cmdstrs {
cmd := exec.Command("/bin/bash", "-c", cmdstr)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
}
}
func (s *Server) showTasks(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Write(s.config.toCleanJSON())
}
func (s *Server) getTask(path string) *Task {
for _, t := range s.config.Tasks {
if t.Name == path[1:] {
return &t
}
}
return nil
}