Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
go-tour: honor -http argument in websocket code
Browse files Browse the repository at this point in the history
R=golang-dev, dsymonds
CC=golang-dev
https://codereview.appspot.com/7305094
  • Loading branch information
adg committed Feb 15, 2013
1 parent 49493e8 commit ff1825b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions gotour/appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ func prepContent(in io.Reader) io.Reader {
}()
return out
}

// socketAddr returns the WebSocket handler address.
// The App Engine version does not provide a WebSocket handler.
func socketAddr() string { return "" }
14 changes: 11 additions & 3 deletions gotour/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
_ "code.google.com/p/go-tour/wc"
)

const basePkg = "code.google.com/p/go-tour/"
const (
basePkg = "code.google.com/p/go-tour/"
socketPath = "/socket"
)

var (
httpListen = flag.String("http", "127.0.0.1:3999", "host:port to listen on")
Expand All @@ -43,6 +46,8 @@ var (

// GOPATH containing the tour packages
gopath = os.Getenv("GOPATH")

httpAddr string
)

func isRoot(path string) bool {
Expand Down Expand Up @@ -99,7 +104,7 @@ func main() {
http.Error(w, "not found", 404)
})

http.Handle("/socket", socket.Handler)
http.Handle(socketPath, socket.Handler)

err = serveScripts(filepath.Join(root, "js"), "socket.js")
if err != nil {
Expand All @@ -117,7 +122,7 @@ func main() {
log.Print(localhostWarning)
}

httpAddr := host + ":" + port
httpAddr = host + ":" + port
go func() {
url := "http://" + httpAddr
if waitServer(url) && *openBrowser && startBrowser(url) {
Expand Down Expand Up @@ -196,3 +201,6 @@ func startBrowser(url string) bool {

// prepContent for the local tour simply returns the content as-is.
func prepContent(r io.Reader) io.Reader { return r }

// socketAddr returns the WebSocket handler address.
func socketAddr() string { return "ws://" + httpAddr + socketPath }
4 changes: 1 addition & 3 deletions gotour/tour.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func renderTour(w io.Writer, root string) error {
// Set up templates.
action := filepath.Join(root, "template", "action.tmpl")
tour := filepath.Join(root, "template", "tour.tmpl")
t := present.Template().Funcs(template.FuncMap{"nocode": nocode})
t := present.Template().Funcs(template.FuncMap{"nocode": nocode, "socketAddr": socketAddr})
_, err = t.ParseFiles(action, tour)
if err != nil {
return err
Expand All @@ -61,8 +61,6 @@ func nocode(s present.Section) bool {
return true
}

// TODO(adg): move these scripts out of static and into a dedicated js folder.

var commonScripts = []string{
"jquery.js",
"codemirror/lib/codemirror.js",
Expand Down
2 changes: 1 addition & 1 deletion js/tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ function setcookie(name, value, expire) {
}

if (window.connectPlayground) {
runFunc = window.connectPlayground("ws://localhost:3999/socket");
runFunc = window.connectPlayground(window.socketAddr);
} else {
// If this message is logged,
// we have neglected to include socket.js or playground.js.
Expand Down
3 changes: 3 additions & 0 deletions template/tour.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<link rel="stylesheet" href="/static/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="static/tour.css">
<script>
window.socketAddr = "{{socketAddr}}";
</script>
<script src="/script.js"></script>

</head>
Expand Down

0 comments on commit ff1825b

Please sign in to comment.