embed static resources via statik

This commit is contained in:
Chris Lu
2018-10-07 10:54:05 -07:00
parent a5b5887efc
commit fffbc2d490
11 changed files with 33 additions and 255 deletions

View File

@@ -12,21 +12,24 @@ import (
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/images"
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/stats"
"github.com/chrislusf/seaweedfs/weed/storage"
"github.com/chrislusf/seaweedfs/weed/util"
_ "github.com/chrislusf/seaweedfs/weed/statik"
statik "github.com/rakyll/statik/fs"
)
var serverStats *stats.ServerStats
var startTime = time.Now()
var statikFS http.FileSystem
func init() {
serverStats = stats.NewServerStats()
go serverStats.Start()
statikFS, _ = statik.New()
}
func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
@@ -191,14 +194,7 @@ func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
writeJsonQuiet(w, r, http.StatusOK, m)
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
data, err := images.Asset("favicon/favicon.ico")
if err != nil {
glog.V(2).Infoln("favicon read error:", err)
return
}
if e := writeResponseContent("favicon.ico", "image/x-icon", bytes.NewReader(data), w, r); e != nil {
glog.V(2).Infoln("response write error:", e)
}
func handleStaticResources(defaultMux *http.ServeMux) {
defaultMux.Handle("/favicon.ico", http.FileServer(statikFS))
defaultMux.Handle("/seaweedfsstatic/", http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
}

View File

@@ -37,6 +37,7 @@ type FilerServer struct {
}
func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
fs = &FilerServer{
option: option,
}
@@ -56,7 +57,7 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption)
notification.LoadConfiguration(v.Sub("notification"))
defaultMux.HandleFunc("/favicon.ico", faviconHandler)
handleStaticResources(defaultMux)
defaultMux.HandleFunc("/", fs.filerHandler)
if defaultMux != readonlyMux {
readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
@@ -85,8 +86,8 @@ func LoadConfiguration(configFileName string, required bool) {
glog.Errorf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
"\n\nPlease follow this example and add a filer.toml file to "+
"current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n"+
" https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n" +
"\n\nOr use this command to generate the default toml file\n" +
" https://github.com/chrislusf/seaweedfs/blob/master/weed/%s.toml\n"+
"\n\nOr use this command to generate the default toml file\n"+
" weed scaffold -config=%s -output=.\n",
configFileName, configFileName, configFileName)
}

View File

@@ -44,7 +44,7 @@ var StatusTpl = template.Must(template.New("status").Funcs(funcMap).Parse(`<!DOC
<div class="container">
<div class="page-header">
<h1>
<img src="http://7viirv.com1.z0.glb.clouddn.com/seaweed50x50.png"></img>
<img src="/seaweedfsstatic/seaweed50x50.png"></img>
SeaweedFS Filer
</h1>
</div>

View File

@@ -15,7 +15,7 @@ var StatusTpl = template.Must(template.New("status").Parse(`<!DOCTYPE html>
<div class="container">
<div class="page-header">
<h1>
<img src="http://7viirv.com1.z0.glb.clouddn.com/seaweed50x50.png"></img>
<img src="/seaweedfsstatic/seaweed50x50.png"></img>
SeaweedFS <small>{{ .Version }}</small>
</h1>
</div>

View File

@@ -43,6 +43,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
vs.guard = security.NewGuard(whiteList, "")
handleStaticResources(adminMux)
adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
adminMux.HandleFunc("/admin/assign_volume", vs.guard.WhiteList(vs.assignVolumeHandler))
@@ -64,7 +65,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
adminMux.HandleFunc("/", vs.privateStoreHandler)
if publicMux != adminMux {
// separated admin and public port
publicMux.HandleFunc("/favicon.ico", faviconHandler)
handleStaticResources(publicMux)
publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
}

View File

@@ -50,7 +50,7 @@ var StatusTpl = template.Must(template.New("status").Funcs(funcMap).Parse(`<!DOC
<div class="container">
<div class="page-header">
<h1>
<img src="http://7viirv.com1.z0.glb.clouddn.com/seaweed50x50.png"></img>
<img src="/seaweedfsstatic/seaweed50x50.png"></img>
SeaweedFS <small>{{ .Version }}</small>
</h1>
</div>