Use embed instead of nrsc, enables 'go get'

This commit is contained in:
Jakob Borg 2014-01-20 23:01:38 +01:00
parent 9b449cb527
commit 84d7452f9e
3 changed files with 30 additions and 24 deletions

View File

@ -14,11 +14,11 @@ if [[ $fast != yes ]] ; then
fi fi
if [[ -z $1 ]] ; then if [[ -z $1 ]] ; then
go build -ldflags "-X main.Version $version" \ go build -ldflags "-X main.Version $version"
&& nrsc syncthing gui elif [[ $1 == "embed" ]] ; then
embedder main gui > gui.files.go
elif [[ $1 == "tar" ]] ; then elif [[ $1 == "tar" ]] ; then
go build -ldflags "-X main.Version $version" \ go build -ldflags "-X main.Version $version" \
&& nrsc syncthing gui \
&& mkdir syncthing-dist \ && mkdir syncthing-dist \
&& cp syncthing README.md LICENSE syncthing-dist \ && cp syncthing README.md LICENSE syncthing-dist \
&& tar zcvf syncthing-dist.tar.gz syncthing-dist \ && tar zcvf syncthing-dist.tar.gz syncthing-dist \
@ -34,7 +34,6 @@ elif [[ $1 == "all" ]] ; then
export GOARCH="$goarch" export GOARCH="$goarch"
export name="syncthing-$goos-$goarch" export name="syncthing-$goos-$goarch"
go build -ldflags "-X main.Version $version" \ go build -ldflags "-X main.Version $version" \
&& nrsc syncthing gui \
&& mkdir -p "$name" \ && mkdir -p "$name" \
&& cp syncthing "$buildDir/$name" \ && cp syncthing "$buildDir/$name" \
&& cp README.md LICENSE "$name" \ && cp README.md LICENSE "$name" \
@ -53,7 +52,6 @@ elif [[ $1 == "all" ]] ; then
export GOARCH="$goarch" export GOARCH="$goarch"
export name="syncthing-$goos-${goarch}v$goarm" export name="syncthing-$goos-${goarch}v$goarm"
go build -ldflags "-X main.Version $version" \ go build -ldflags "-X main.Version $version" \
&& nrsc syncthing gui \
&& mkdir -p "$name" \ && mkdir -p "$name" \
&& cp syncthing "$buildDir/$name" \ && cp syncthing "$buildDir/$name" \
&& cp README.md LICENSE "$name" \ && cp README.md LICENSE "$name" \

6
gui.files.go Normal file

File diff suppressed because one or more lines are too long

40
gui.go
View File

@ -3,17 +3,18 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"mime" "mime"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"time"
"bitbucket.org/tebeka/nrsc"
"github.com/calmh/syncthing/model" "github.com/calmh/syncthing/model"
"github.com/codegangsta/martini" "github.com/codegangsta/martini"
"github.com/cratonica/embed"
) )
func startGUI(addr string, m *model.Model) { func startGUI(addr string, m *model.Model) {
@ -26,9 +27,20 @@ func startGUI(addr string, m *model.Model) {
router.Get("/rest/need", restGetNeed) router.Get("/rest/need", restGetNeed)
router.Get("/rest/system", restGetSystem) router.Get("/rest/system", restGetSystem)
fs, err := embed.Unpack(Resources)
if err != nil {
panic(err)
}
var modt time.Time
fi, err := os.Stat(os.Args[0])
if err != nil {
modt = fi.ModTime()
}
go func() { go func() {
mr := martini.New() mr := martini.New()
mr.Use(nrscStatic("gui")) mr.Use(embeddedStatic(fs, modt.UTC().Format(http.TimeFormat)))
mr.Use(martini.Recovery()) mr.Use(martini.Recovery())
mr.Action(router.Handle) mr.Action(router.Handle)
mr.Map(m) mr.Map(m)
@ -124,36 +136,26 @@ func restGetSystem(w http.ResponseWriter) {
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }
func nrscStatic(path string) interface{} { func embeddedStatic(fs map[string][]byte, modt string) interface{} {
if err := nrsc.Initialize(); err != nil {
panic("Unable to initialize nrsc: " + err.Error())
}
return func(res http.ResponseWriter, req *http.Request, log *log.Logger) { return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
file := req.URL.Path file := req.URL.Path
// nrsc expects there not to be a leading slash
if file[0] == '/' { if file[0] == '/' {
file = file[1:] file = file[1:]
} }
f := nrsc.Get(file) bs, ok := fs[file]
if f == nil { if !ok {
return return
} }
rdr, err := f.Open()
if err != nil {
http.Error(res, "Internal Server Error", http.StatusInternalServerError)
}
defer rdr.Close()
mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path)) mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
if len(mtype) != 0 { if len(mtype) != 0 {
res.Header().Set("Content-Type", mtype) res.Header().Set("Content-Type", mtype)
} }
res.Header().Set("Content-Size", fmt.Sprintf("%d", f.Size())) res.Header().Set("Content-Size", fmt.Sprintf("%d", len(bs)))
res.Header().Set("Last-Modified", f.ModTime().UTC().Format(http.TimeFormat)) res.Header().Set("Last-Modified", modt)
io.Copy(res, rdr) res.Write(bs)
} }
} }