exposed listen addr parameter

This commit is contained in:
Darko Luketic 2019-11-04 02:30:57 +01:00
parent f1aa34fc56
commit 3a9999ce6d

View File

@ -16,7 +16,11 @@ limitations under the License.
package cmd package cmd
import ( import (
"fmt"
"net"
"net/http" "net/http"
"os"
"strings"
"time" "time"
"github.com/dalu/chromedom/server" "github.com/dalu/chromedom/server"
@ -27,6 +31,7 @@ var (
binary string binary string
duration time.Duration duration time.Duration
maxmem int maxmem int
addr string
) )
// serveCmd represents the serve command // serveCmd represents the serve command
@ -38,13 +43,15 @@ var serveCmd = &cobra.Command{
s := http.DefaultServeMux s := http.DefaultServeMux
h := server.NewHandler(binary, duration, maxmem) h := server.NewHandler(binary, duration, maxmem)
s.Handle("/", h) s.Handle("/", h)
return http.ListenAndServe(":9292", s) if strings.HasPrefix(addr, "unix:") {
// return runUnix("/tmp/chromedom.sock", s) return runUnix(strings.TrimPrefix(addr, "unix:"), s)
} else {
fmt.Printf("Listening and serving HTTP on %s", addr)
return http.ListenAndServe(addr, s)
}
}, },
} }
/*
func runUnix(file string, engine *http.ServeMux) (err error) { func runUnix(file string, engine *http.ServeMux) (err error) {
fmt.Printf("Listening and serving HTTP on unix:/%s", file) fmt.Printf("Listening and serving HTTP on unix:/%s", file)
os.Remove(file) os.Remove(file)
@ -53,11 +60,12 @@ func runUnix(file string, engine *http.ServeMux) (err error) {
return return
} }
defer listener.Close() defer listener.Close()
os.Chmod(file, 0777) if err := os.Chmod(file, 0777); err != nil {
return err
}
err = http.Serve(listener, engine) err = http.Serve(listener, engine)
return return
} }
*/
func init() { func init() {
rootCmd.AddCommand(serveCmd) rootCmd.AddCommand(serveCmd)
@ -65,4 +73,5 @@ func init() {
serveCmd.Flags().StringVarP(&binary, "binary", "b", "/usr/bin/google-chrome", "chrome, chromium or headless binary path") serveCmd.Flags().StringVarP(&binary, "binary", "b", "/usr/bin/google-chrome", "chrome, chromium or headless binary path")
serveCmd.Flags().DurationVarP(&duration, "duration", "d", 60*time.Minute, "how long the values live in the cache") serveCmd.Flags().DurationVarP(&duration, "duration", "d", 60*time.Minute, "how long the values live in the cache")
serveCmd.Flags().IntVarP(&maxmem, "maxmem", "m", 512, "maximum memory in MB the cache may use") serveCmd.Flags().IntVarP(&maxmem, "maxmem", "m", 512, "maximum memory in MB the cache may use")
serveCmd.Flags().StringVarP(&addr, "addr", "a", "localhost:9292", "ip:port pair to listen on, or prefixed with unix: the unix socket path to listen on")
} }