From 04130fcb1579f2ed68baf909f8e243d0cee2f1fe Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 22 May 2014 16:12:19 +0200 Subject: [PATCH] Allow GUI development with standard binary --- build.sh | 3 ++- cmd/syncthing/gui.go | 43 +++++++++++++++++++++++++++++--- cmd/syncthing/gui_development.go | 9 ------- cmd/syncthing/gui_embedded.go | 40 ----------------------------- cmd/syncthing/main.go | 6 +++-- 5 files changed, 45 insertions(+), 56 deletions(-) delete mode 100644 cmd/syncthing/gui_development.go delete mode 100644 cmd/syncthing/gui_embedded.go diff --git a/build.sh b/build.sh index dd3704306..3ce6ae1bf 100755 --- a/build.sh +++ b/build.sh @@ -88,7 +88,8 @@ case "$1" in ;; guidev) - build -tags guidev + echo "Syncthing is already built for GUI developments. Try:" + echo " STGUIASSETS=~/someDir/gui syncthing" ;; test) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 49e5b8ca0..cf997588c 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 6c4952471..000000000 --- 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 4d1562d52..000000000 --- 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 483402ee2..f0591d0f6 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) }