Skip to content

Commit

Permalink
More main reduction, reducing code for testability
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Oct 19, 2014
1 parent 95580fe commit 2b9318c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
88 changes: 48 additions & 40 deletions gosh.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,6 @@ func run(cmdPath string) error {
return err
}

func runner(chs map[string]chan string, runFunc func(string) error) {
cases := []reflect.SelectCase{}
for _, ch := range chs {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch)})
}

for {
// Grab the next request (arbitrarily if there's more
// than one waiting)
_, val, ok := reflect.Select(cases)
if !ok {
return
}
cmdPath := val.String()

log.Printf("Got request, executing %v", cmdPath)
runFunc(cmdPath)
}
}

func findScripts(dn string) ([]string, error) {
d, err := os.Open(dn)
if err != nil {
Expand Down Expand Up @@ -118,6 +96,49 @@ func mkScriptChans(dir string) (map[string]chan string, map[string]string, error
return chs, cmdMap, nil
}

type httpHandler struct {
chs map[string]chan string
cmdMap map[string]string
}

func (h httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path[1:]
select {
case h.chs[urlPath] <- h.cmdMap[urlPath]:
// successfully queued a request to run a script
default:
// One of two things happened:
// 1. The path doesn't actually match to a script,
// so there's nothing to queue.
// 2. The buffer is full, meaning there's already a
// run queued that will begin after this request,
// so we don't need to do anything.
}
w.WriteHeader(202)
}

func (h httpHandler) run(runFunc func(string) error) {
cases := []reflect.SelectCase{}
for _, ch := range h.chs {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch)})
}

for {
// Grab the next request (arbitrarily if there's more
// than one waiting)
_, val, ok := reflect.Select(cases)
if !ok {
return
}
cmdPath := val.String()

log.Printf("Got request, executing %v", cmdPath)
runFunc(cmdPath)
}
}

func main() {
flag.Parse()

Expand All @@ -126,22 +147,9 @@ func main() {
log.Fatalf("Error finding scripts: %v", err)
}

go runner(chs, run)

http.HandleFunc(*prefixPath, func(w http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path[1:]
select {
case chs[urlPath] <- cmdMap[urlPath]:
// successfully queued a request to run a script
default:
// One of two things happened:
// 1. The path doesn't actually match to a script,
// so there's nothing to queue.
// 2. The buffer is full, meaning there's already a
// run queued that will begin after this request,
// so we don't need to do anything.
}
w.WriteHeader(202)
})
log.Fatal(http.ListenAndServe(*bindAddr, nil))
h := &httpHandler{chs, cmdMap}
go h.run(run)

http.Handle(*prefixPath, h)
log.Fatal(http.ListenAndServe(*bindAddr, h))
}
3 changes: 2 additions & 1 deletion gosh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ func TestRunner(t *testing.T) {
chans := map[string]chan string{"test1": chan1, "test2": chan2, "test3": chan3}

outch := make(chan string, 10)
go runner(chans, func(s string) error {
h := &httpHandler{chans, nil}
go h.run(func(s string) error {
outch <- s
return nil
})
Expand Down

0 comments on commit 2b9318c

Please sign in to comment.