Skip to content

Commit

Permalink
issues/20 If dev Live Reload port is already in use, fallback on an a…
Browse files Browse the repository at this point in the history
…lternative port.
  • Loading branch information
Deleplace committed Mar 27, 2019
1 parent bceae96 commit adb7af1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
35 changes: 33 additions & 2 deletions files/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ package files
import (
"fmt"
"log"
"net"
"path/filepath"
"strconv"
"time"

"github.com/jaschaephraim/lrserver"
"github.com/radovskyb/watcher"
)

func Watch(root string) error {
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
func Watch(root string, port uint16) error {
lr := lrserver.New(lrserver.DefaultName, port)
go func() {
log.Fatal(lr.ListenAndServe())
}()
Expand Down Expand Up @@ -61,3 +63,32 @@ func Watch(root string) error {

return nil
}

func LiveReloadPort() uint16 {
port := lrserver.DefaultPort
for {
if isPortAvailable(port) {
break
}
// If port is already in use (e.g. another Demoit instance), then
// choose an alternative port
log.Println("\tCan't use live reload port", port, "(already in use)")
port++
if port == lrserver.DefaultPort+10 {
log.Fatalln("Couldn't find a live reload port after 10 attempts")
}
}
return port
}

func isPortAvailable(port uint16) bool {
portstr := strconv.Itoa(int(port))
conn, err := net.DialTimeout("tcp", net.JoinHostPort("", portstr), time.Second)
_ = err
if conn != nil {
// Connection established means "Port was already in use!"
conn.Close()
return false
}
return true
}
12 changes: 11 additions & 1 deletion handlers/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strconv"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/dgageot/demoit/flags"
"github.com/dgageot/demoit/templates"
"github.com/gorilla/mux"
"github.com/jaschaephraim/lrserver"
)

// Page describes a page of the demo.
Expand All @@ -43,6 +45,11 @@ type Page struct {
DevMode bool
}

// LiveReloadPort is the port used for live-reload of page contents, on change.
// Default is 35729.
// Can be set to an alternative value, before rendering.
var LiveReloadPort uint16 = lrserver.DefaultPort

// Step renders a given page.
func Step(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand All @@ -63,7 +70,9 @@ func Step(w http.ResponseWriter, r *http.Request) {
}

step := steps[id]
pageTemplate, err := template.New("page").Parse(templates.Index(step.HTML))
pageTemplate, err := template.New("page").
Funcs(template.FuncMap{"lrport": func() uint16 { return LiveReloadPort }}).
Parse(templates.Index(step.HTML))
if err != nil {
http.Error(w, "Unable to parse page", http.StatusInternalServerError)
return
Expand All @@ -72,6 +81,7 @@ func Step(w http.ResponseWriter, r *http.Request) {
var html bytes.Buffer
err = pageTemplate.Execute(&html, step)
if err != nil {
log.Println(err)
http.Error(w, "Unable to render page", http.StatusInternalServerError)
return
}
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func main() {
}

func startFileWatch(root string) {
log.Fatal(files.Watch(root))
lrport := files.LiveReloadPort()
handlers.LiveReloadPort = lrport
log.Fatal(files.Watch(root, lrport))
}

func startWebServer(r http.Handler) {
Expand Down
2 changes: 1 addition & 1 deletion templates/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func Index(content []byte) string {
<div id="progression" style="width: calc(100vw * {{ .CurrentStep }} / {{ .StepCount }})"></div>
</body>
<script src="/js/demoit.js"></script>
{{ if .DevMode }}<script src="http://localhost:35729/livereload.js"></script>{{ end }}
{{ if .DevMode }}<script src="http://localhost:{{ lrport }}/livereload.js"></script>{{ end }}
</html>`
}

0 comments on commit adb7af1

Please sign in to comment.