-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (66 loc) · 1.23 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
package main
import (
"github.com/hoisie/web"
"gopkg.in/yaml.v2"
"os"
"io/ioutil"
"os/exec"
"bytes"
"time"
)
type Hook struct {
Directory string
Execute string
Password string
}
type Config struct {
Listen string
Hooks map[string]Hook
}
var config Config
func main() {
config = Config{
Hooks: make(map[string]Hook),
}
f, err := os.Open("config.yaml")
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
f.Close()
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
panic(err)
}
web.Match("POST|GET", "/([0-9a-zA-Z]+)", handle)
web.Run(config.Listen)
}
func handle(ctx *web.Context, name string) {
hook, ok := config.Hooks[name]
if !ok {
ctx.NotFound("Page not found")
return
}
if hook.Password != "" && ctx.Params["password"] != hook.Password {
ctx.NotFound("Page not found")
return
}
var output bytes.Buffer
cmd := exec.Command("sh", "-c", hook.Execute)
cmd.Stdout = &output
cmd.Stderr = &output
cmd.Dir = hook.Directory
err := cmd.Start()
if err != nil {
ctx.Abort(500, err.Error() + "\n\n")
}
timer := time.AfterFunc(30 * time.Second, func() {
cmd.Process.Kill()
})
cmd.Wait()
timer.Stop()
ctx.Write(output.Bytes())
}