-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (58 loc) · 2.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/Checksum/gosh/handlers"
"github.com/toqueteos/webbrowser"
"github.com/Zillode/notify"
gorilla "github.com/gorilla/handlers"
"github.com/pkg/profile"
"github.com/zenazn/goji/web/middleware"
)
func main() {
var port, dir, ext string
var noCache, watch, openBrowser, spa, cpuprofile bool
flag.StringVar(&port, "port", "8000", "The port to bind to")
flag.StringVar(&dir, "dir", ".", "The directory to serve")
flag.BoolVar(&noCache, "no-cache", true, "Disable HTTP caching")
flag.BoolVar(&watch, "watch", false, "Reload browser on file change")
flag.BoolVar(&openBrowser, "open", true, "Open a browser to serve the page")
flag.BoolVar(&spa, "spa", true, "Serve a single page application. All unmatched routes are forwarded to index.html")
flag.StringVar(&ext, "ext", "js,css,html", "Comma separated file extensions to watch for change")
flag.BoolVar(&cpuprofile, "cpuprofile", false, "Record profile for debugging")
flag.Parse()
if cpuprofile {
defer profile.Start().Stop()
}
mux := http.NewServeMux()
handler := gorilla.LoggingHandler(os.Stdout, http.FileServer(http.Dir(dir)))
if noCache {
handler = middleware.NoCache(handler)
}
// If we have to watch the file for changes, pass the request through
// the appropriate handlers.
if watch {
// This is to intercept the original response to inject our javascript
handler = handlers.NewInjectingHandler(handler)
// Create a new watcher that will notify the page through Server-Sent Events
watchHandler := handlers.NewFileWatcher(dir, ext, handler)
defer notify.Stop(watchHandler.Watcher)
// This endpoint is serviced by the SSE broker, which receives a signal
// when a file system change occurs, and sends out a command to the browser
// to reload the page (see also: script.go)
mux.Handle("/__events", watchHandler.Broker)
log.Println("Watching for changes..")
}
if spa {
handler = handlers.NewFileServer(handler)
}
// Handle all other paths
mux.Handle("/", handler)
if openBrowser {
webbrowser.Open("http://localhost:" + port)
}
log.Printf("Serving %s on port %s", dir, port)
log.Fatal(http.ListenAndServe("localhost:"+port, mux))
}