From acf2d2b46799a9b6a539c16b0bd5136199a9811a Mon Sep 17 00:00:00 2001 From: codeskyblue <codeskyblue@gmail.com> Date: Tue, 26 Jul 2016 14:06:01 +0800 Subject: [PATCH] support binary standalone now --- .gitignore | 1 + README.md | 18 +++++++++++++++--- httpstaticserver.go | 10 +++++----- main.go | 3 +++ res_bindata.go | 17 +++++++++++++++++ res_nobindata.go | 18 ++++++++++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 res_bindata.go create mode 100644 res_nobindata.go diff --git a/.gitignore b/.gitignore index d8b3162..a93c218 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ _testmain.go *.prof gohttpserver +bindata_assetfs.go diff --git a/README.md b/README.md index 05670a6..b1abae6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ If using go1.5, ensure you set GO15VENDOREXPERIMENT=1 ## Features 1. [x] Support QRCode code generate 1. [x] Breadcrumb path quick change -1. [ ] All assets package to Standalone binary +1. [x] All assets package to Standalone binary 1. [x] Different file type different icon 1. [ ] Support show or hide hidden files 1. [ ] Upload support @@ -47,13 +47,25 @@ Listen port 8000 on all interface ## Developer Guide Depdencies are managed by godep -``` +```sh go get -v github.com/tools/godep +go get github.com/jteeuwen/go-bindata/... +go get github.com/elazarl/go-bindata-assetfs/... ``` -Reference Web sites +## How to build single binary release +```sh +go-bindata-assetfs -tags bindata res/... +go build -tags bindata +``` + +That's all. ^_^ + +## Reference Web sites * <https://vuejs.org.cn/> * Icon from <http://www.easyicon.net/558394-file_explorer_icon.html> +* <https://github.com/elazarl/go-bindata-assetfs> + ## LICENSE This project is under license [MIT](LICENSE) diff --git a/httpstaticserver.go b/httpstaticserver.go index 3025883..a99a266 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "html/template" "log" "net/http" "os" @@ -41,10 +40,11 @@ func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { relPath := filepath.Join(s.Root, path) finfo, err := os.Stat(relPath) if err == nil && finfo.IsDir() { - indexPath := filepath.Join("./res", "index.tmpl.html") - t := template.New("index").Delims("[[", "]]") - tmpl := template.Must(t.ParseFiles(indexPath)) - tmpl.ExecuteTemplate(w, "index.tmpl.html", nil) + // indexPath := filepath.Join("./res", "index.tmpl.html") + // t := template.New("index").Delims("[[", "]]") + // tmpl := template.Must(t.ParseFiles(indexPath)) + // tmpl.ExecuteTemplate(w, "index.tmpl.html", nil) + tmpl.Execute(w, nil) } else { http.ServeFile(w, r, relPath) } diff --git a/main.go b/main.go index 6d44978..47478e6 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,9 @@ func main() { } http.Handle("/", hdlr) + + // indexContent, _ := Asset("res/index.tmpl.html") + // log.Println(string(indexContent)) log.Printf("Listening on addr: %s\n", strconv.Quote(gcfg.Addr)) var err error diff --git a/res_bindata.go b/res_bindata.go new file mode 100644 index 0000000..e3d3c3f --- /dev/null +++ b/res_bindata.go @@ -0,0 +1,17 @@ +// +build bindata + +package main + +import ( + "html/template" + "net/http" +) + +var tmpl *template.Template + +func init() { + http.Handle("/-/res/", http.StripPrefix("/-/res/", http.FileServer(assetFS()))) + + indexContent, _ := Asset("res/index.tmpl.html") + tmpl = template.Must(template.New("t").Delims("[[", "]]").Parse(string(indexContent))) +} diff --git a/res_nobindata.go b/res_nobindata.go new file mode 100644 index 0000000..66bceb2 --- /dev/null +++ b/res_nobindata.go @@ -0,0 +1,18 @@ +// +build !bindata + +package main + +import ( + "html/template" + "io/ioutil" + "net/http" +) + +var tmpl *template.Template + +func init() { + http.Handle("/-/res/", http.StripPrefix("/-/res/", http.FileServer(http.Dir("./res")))) + + indexContent, _ := ioutil.ReadFile("./res/index.tmpl.html") + tmpl = template.Must(template.New("t").Delims("[[", "]]").Parse(string(indexContent))) +}