diff options
author | Jakob Borg <jakob@nym.se> | 2014-05-22 16:12:19 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-05-22 16:12:19 +0200 |
commit | 04130fcb1579f2ed68baf909f8e243d0cee2f1fe (patch) | |
tree | 54a3e5037f8bf70b0d976e23d9e633e57443ba97 /cmd | |
parent | 52d8e4c691beb2273bdb39f4ca9ad5acc88bb8c0 (diff) |
Allow GUI development with standard binary
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/syncthing/gui.go | 43 | ||||
-rw-r--r-- | cmd/syncthing/gui_development.go | 9 | ||||
-rw-r--r-- | cmd/syncthing/gui_embedded.go | 40 | ||||
-rw-r--r-- | cmd/syncthing/main.go | 6 |
4 files changed, 43 insertions, 55 deletions
diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 49e5b8ca0b..cf997588c5 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -4,17 +4,21 @@ import ( "bytes" "encoding/base64" "encoding/json" + "fmt" "io/ioutil" "log" "math/rand" + "mime" "net" "net/http" + "path/filepath" "runtime" "sync" "time" "crypto/tls" "code.google.com/p/go.crypto/bcrypt" + "github.com/calmh/syncthing/auto" "github.com/calmh/syncthing/config" "github.com/calmh/syncthing/logger" "github.com/calmh/syncthing/model" @@ -31,8 +35,7 @@ var ( configInSync = true guiErrors = []guiError{} guiErrorsMut sync.Mutex - static = embeddedStatic() - staticFunc = static.(func(http.ResponseWriter, *http.Request, *log.Logger)) + static func(http.ResponseWriter, *http.Request, *log.Logger) ) const ( @@ -43,7 +46,7 @@ func init() { l.AddHandler(logger.LevelWarn, showGuiError) } -func startGUI(cfg config.GUIConfiguration, m *model.Model) error { +func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) error { var listener net.Listener var err error if cfg.UseTLS { @@ -70,6 +73,12 @@ func startGUI(cfg config.GUIConfiguration, m *model.Model) error { } } + if len(assetDir) > 0 { + static = martini.Static(assetDir).(func(http.ResponseWriter, *http.Request, *log.Logger)) + } else { + static = embeddedStatic() + } + router := martini.NewRouter() router.Get("/", getRoot) router.Get("/rest/version", restGetVersion) @@ -108,7 +117,7 @@ func startGUI(cfg config.GUIConfiguration, m *model.Model) error { func getRoot(w http.ResponseWriter, r *http.Request) { r.URL.Path = "/index.html" - staticFunc(w, r, nil) + static(w, r, nil) } func restMiddleware(w http.ResponseWriter, r *http.Request) { @@ -340,3 +349,29 @@ func basic(username string, passhash string) http.HandlerFunc { } } } + +func embeddedStatic() func(http.ResponseWriter, *http.Request, *log.Logger) { + var modt = time.Now().UTC().Format(http.TimeFormat) + + return func(res http.ResponseWriter, req *http.Request, log *log.Logger) { + file := req.URL.Path + + if file[0] == '/' { + file = file[1:] + } + + bs, ok := auto.Assets[file] + if !ok { + return + } + + mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path)) + if len(mtype) != 0 { + res.Header().Set("Content-Type", mtype) + } + res.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs))) + res.Header().Set("Last-Modified", modt) + + res.Write(bs) + } +} diff --git a/cmd/syncthing/gui_development.go b/cmd/syncthing/gui_development.go deleted file mode 100644 index 6c49524718..0000000000 --- a/cmd/syncthing/gui_development.go +++ /dev/null @@ -1,9 +0,0 @@ -//+build guidev - -package main - -import "github.com/codegangsta/martini" - -func embeddedStatic() interface{} { - return martini.Static("gui") -} diff --git a/cmd/syncthing/gui_embedded.go b/cmd/syncthing/gui_embedded.go deleted file mode 100644 index 4d1562d52b..0000000000 --- a/cmd/syncthing/gui_embedded.go +++ /dev/null @@ -1,40 +0,0 @@ -//+build !guidev - -package main - -import ( - "fmt" - "log" - "mime" - "net/http" - "path/filepath" - "time" - - "github.com/calmh/syncthing/auto" -) - -func embeddedStatic() interface{} { - var modt = time.Now().UTC().Format(http.TimeFormat) - - return func(res http.ResponseWriter, req *http.Request, log *log.Logger) { - file := req.URL.Path - - if file[0] == '/' { - file = file[1:] - } - - bs, ok := auto.Assets[file] - if !ok { - return - } - - mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path)) - if len(mtype) != 0 { - res.Header().Set("Content-Type", mtype) - } - res.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs))) - res.Header().Set("Last-Modified", modt) - - res.Write(bs) - } -} diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 483402ee2f..f0591d0f69 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -84,7 +84,9 @@ const ( - "xdr" (the xdr package) - "all" (all of the above) - STCPUPROFILE Write CPU profile to the specified file.` + STCPUPROFILE Write CPU profile to the specified file. + + STGUIASSETS Directory to load GUI assets from. Overrides compiled in assets.` ) func main() { @@ -279,7 +281,7 @@ func main() { } l.Infof("Starting web GUI on %s://%s:%d/", proto, hostShow, addr.Port) - err := startGUI(cfg.GUI, m) + err := startGUI(cfg.GUI, os.Getenv("STGUIASSETS"), m) if err != nil { l.Fatalln("Cannot start GUI:", err) } |