-
Notifications
You must be signed in to change notification settings - Fork 0
/
gospeak.go
65 lines (55 loc) · 1.37 KB
/
gospeak.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
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/GeertJohan/go.rice"
"log"
"net/http"
"os"
"os/exec"
)
type SpeechHandler struct {
sChan chan string
}
func (h *SpeechHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.sChan <- fmt.Sprintf("%s", r.FormValue("s"))
}
func SpeechServer(c string, sChan chan string) {
for {
s := <-sChan
log.Printf("Command: %s -- Saying: %s\n", c, s)
cmd := exec.Command(c, s)
_, err := cmd.Output()
if err != nil {
log.Println(err.Error())
}
}
}
func testCommand(c string) {
if c == "" {
log.Fatalln("Command flag can not be empty")
}
if _, err := os.Stat(c); os.IsNotExist(err) {
log.Fatalln("Command does not exist")
}
}
func main() {
app := cli.NewApp()
app.Name = "gospeak"
app.Usage = "provides a web interface to the systems text-to-speech system"
app.Flags = []cli.Flag{
cli.IntFlag{"port, p", 8080, "port gospeak listens on"},
cli.StringFlag{"command, c", "", "command to use for speech"},
}
app.Action = func(c *cli.Context) {
command := c.String("command")
testCommand(command)
port := fmt.Sprintf(":%d", c.Int("port"))
var sChan = make(chan string)
go SpeechServer(command, sChan)
http.Handle("/speak", &SpeechHandler{sChan})
http.Handle("/", http.FileServer(rice.MustFindBox("static").HTTPBox()))
log.Fatal(http.ListenAndServe(port, nil))
}
app.Run(os.Args)
}