github.com/kcmerrill/alfred@v0.0.0-20180727171036-06445dcb5e3d/pkg/alfred/serve.go (about) 1 package alfred 2 3 import ( 4 "net/http" 5 "strings" 6 "time" 7 8 "github.com/gorilla/mux" 9 ) 10 11 // serve is alfred's built in web server, useful for sharing private repos 12 func serve(task Task, context *Context, tasks map[string]Task) { 13 if task.Serve == "" { 14 return 15 } 16 17 dir, _ := task.dir(context) 18 19 addr := []string{"0.0.0.0", task.Serve} 20 if strings.Contains(task.Serve, ":") { 21 addr = strings.Split(task.Serve, ":") 22 } 23 24 // TODO taskdir task.dir() 25 r := mux.NewRouter() 26 r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir(dir)))) 27 srv := &http.Server{ 28 Handler: r, 29 Addr: strings.Join(addr, ":"), 30 WriteTimeout: 15 * time.Second, 31 ReadTimeout: 15 * time.Second, 32 } 33 34 go func() { 35 outOK("serving "+dir, strings.Join(addr, ":"), context) 36 if err := srv.ListenAndServe(); err != nil { 37 outFail("serve", err.Error(), context) 38 task.Exit(context, tasks) 39 } 40 }() 41 }