Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically open browser on start and add the js reload hook to html contents #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ http-watcher args # acceptable args list below, -h to show them
-private=false: Only listen on lookback interface, otherwise listen on all interface
-proxy=0: Local dynamic site's port number, like 8080, HTTP watcher proxy it, automatically reload browsers when watched directory's file changed
-root=".": Watched root directory for filesystem events, also the HTTP File Server's root directory
-open=true: Open your default browser to the serving url
```

### HTML + JS + CSS (static web project)
Expand Down
32 changes: 26 additions & 6 deletions http-watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"text/template"
"time"

fqdn "github.com/ShowMax/go-fqdn"
"github.com/howeyc/fsnotify"
"github.com/skratchdot/open-golang/open"
)

type Client struct {
Expand All @@ -34,6 +36,7 @@ type ReloadMux struct {
port int
ignores string
ignorePattens []*regexp.Regexp
openBrowser bool
command string
root string
reloadJs *template.Template
Expand Down Expand Up @@ -86,6 +89,7 @@ func showDoc(w http.ResponseWriter, req *http.Request, err error) {

func publicHosts() []string {
ips := make([]string, 0)
ips = append(ips, baseURL)
if reloadCfg.private {
ips = append(ips, "127.0.0.1:"+strconv.Itoa(reloadCfg.port))
} else {
Expand Down Expand Up @@ -234,9 +238,6 @@ func fileHandler(w http.ResponseWriter, path string, req *http.Request) {
}
w.Header().Set("Content-Type", ctype)
}
if fi, err := os.Stat(path); err == nil {
w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
}
w.WriteHeader(200)
io.Copy(w, f)
appendReloadHook(w, ctype, req)
Expand Down Expand Up @@ -424,6 +425,8 @@ func processFsEvents() {
}
}

var baseURL string

func main() {
flag.IntVar(&(reloadCfg.port), "port", 8000, "Which port to listen")
flag.StringVar(&(reloadCfg.root), "root", ".", "Watched root directory for filesystem events, also the HTTP File Server's root directory")
Expand All @@ -433,6 +436,7 @@ func main() {
flag.IntVar(&(reloadCfg.proxy), "proxy", 0, "Local dynamic site's port number, like 8080, HTTP watcher proxy it, automatically reload browsers when watched directory's file changed")
flag.BoolVar(&(reloadCfg.monitor), "monitor", true, "Enable monitor filesystem event")
flag.Float64Var(&(reloadCfg.delay), "delay", 0, "Delay in seconds before reload browser.")
flag.BoolVar(&(reloadCfg.openBrowser), "open", true, "open browser on start")
flag.Parse()

if _, e := os.Open(reloadCfg.command); e == nil {
Expand Down Expand Up @@ -462,18 +466,34 @@ func main() {

int := ":" + strconv.Itoa(reloadCfg.port)
p := strconv.Itoa(reloadCfg.port)

baseURL = fqdn.Get() + ":" + p

mesg := ""
if reloadCfg.proxy != 0 {
mesg += "; proxy site http://127.0.0.1:" + strconv.Itoa(reloadCfg.proxy)
}
mesg += "; please visit http://127.0.0.1:" + p
mesg += "; please visit http://" + baseURL
if reloadCfg.private {
int = "localhost" + int
log.Printf("listens on 127.0.0.1@" + p + mesg)
} else {
log.Printf("listens on 0.0.0.0@" + p + mesg)
}
if err := http.ListenAndServe(int, nil); err != nil {
log.Fatal(err)

q := make(chan bool)
go func(q chan bool) {
if err := http.ListenAndServe(int, nil); err != nil {
log.Fatal(err)
}
q <- true
}(q)

if reloadCfg.openBrowser == true {
time.Sleep(time.Millisecond * 500) // Give a chance to http Server to start
log.Printf("opening http://%s with system browser", baseURL)
open.Start("http://" + baseURL)
}

<-q
}