forked from shimberger/gohls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_command.go
110 lines (102 loc) · 2.5 KB
/
http_command.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"sync"
//"net/http"
"bufio"
"crypto/sha1"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"syscall"
)
type Empty struct{}
type HttpCommandHandler struct {
tokenChannel chan Empty
cacheDir string
inProgress map[string]string
inProgressMutex *sync.RWMutex
// path string
}
func NewHttpCommandHandler(workerCount int, cacheDir string) *HttpCommandHandler {
ch := &HttpCommandHandler{make(chan Empty, workerCount), cacheDir, make(map[string]string), new(sync.RWMutex)}
for i := workerCount; i > 0; i-- {
ch.tokenChannel <- Empty{}
}
go ch.start()
return ch
}
func (s *HttpCommandHandler) start() {
}
func (s *HttpCommandHandler) calculateKey(cmd string, args []string) string {
h := sha1.New()
h.Write([]byte(cmd))
for _, v := range args {
h.Write([]byte(v))
}
sum := h.Sum(nil)
return fmt.Sprintf("%x", sum)
}
func (s *HttpCommandHandler) ServeCommand(cmdPath string, args []string, w io.Writer) error {
key := s.calculateKey(cmdPath, args)
token := <-s.tokenChannel
//log.Printf("Token: %v",key)
defer func() {
s.tokenChannel <- token
//log.Printf("Released token")
}()
cachePath := filepath.Join("cache", s.cacheDir, key)
mkerr := os.MkdirAll(filepath.Join("cache", s.cacheDir), 0777)
if mkerr != nil {
log.Printf("Could not create cache dir %v: %v", filepath.Join("cache", s.cacheDir), mkerr)
return mkerr
}
if file, err := os.Open(cachePath); err == nil {
defer file.Close()
_, err = io.Copy(w, file)
if err != nil {
log.Printf("Error copying file to client: %v", err)
return err
}
return nil
}
cacheFile, ferr := os.Create(cachePath)
if ferr != nil {
log.Printf("Could not create cache file %v: %v", cacheFile, ferr)
return ferr
}
defer cacheFile.Close()
log.Printf("Executing %v %v", cmdPath, args)
cmd := exec.Command(cmdPath, args...)
stdout, err := cmd.StdoutPipe()
defer stdout.Close()
if err != nil {
log.Printf("Error opening stdout of command: %v", err)
return err
}
err = cmd.Start()
if err != nil {
log.Printf("Error starting command: %v", err)
return err
}
filew := bufio.NewWriter(cacheFile)
multiw := io.MultiWriter(filew, w)
_, err = io.Copy(multiw, stdout)
if err != nil {
log.Printf("Error copying data to client: %v", err)
cacheFile.Close()
os.Remove(cachePath)
// Ask the process to exit
cmd.Process.Signal(syscall.SIGKILL)
cmd.Process.Wait()
return err
}
cmd.Wait()
filew.Flush()
log.Printf("Streaming done\n");
return nil
//s.inProgressMutex.Lock()
//s.inProgressMutex.Unlock()
}