多个Dir服务不起作用 [英] Multiple Dir serving is not working
问题描述
package main
log
net / http
)
func serveide(w http.ResponseWriter,r * http.Request){
http.FileServer(http.Dir(/ home / user / ide)).ServeHTTP(w,r)
}
func serveConsole(w http.ResponseWriter,r * http .Request){
http.FileServer(http.Dir(/ home / user / console)).ServeHTTP(w,r)
}
func main {
http.HandleFunc(/ ide,serveIDE)
http.HandleFunc(/ console,serveConsole)
err:= http.ListenAndServe(:9090,nil)
if err!= nil {
log.Fatal(ListenAndServe:,err)
}
}
当我改变这样的代码时,
http.HandleFunc( ),
它会按我的预期工作。
其中一个问题是wi使用 http.FileServer
是因为请求路径用于构建文件名,所以如果您从除root之外的任何地方提供服务,则需要将路由前缀去除标准库包含一个对 http.StripPrefix
有用的工具,但只适用于 code> http.Handler
s,而不是 http.HandleFunc
s,所以要使用它,您需要调整 HandleFunc
添加到 Handler
。
这是一个工作版本,你要。请注意,wHandler只是一个从 HttpFunc
方法到 Hander
界面的适配器:
包主
导入(
记录
净/ http
)
func serveIDE(w http.ResponseWriter,r * http.Request){
http.FileServer(http.Dir(/ home / user / ide))。ServeHTTP(w,r)
}
func serveConsole(w http.ResponseWriter,r * http.Request){
http.FileServer(http.Dir(/ home / user / console)) .ServeHTTP(w,r)
}
type wHandler struct {
fn http.HandlerFunc
}
func(h * wHandler )ServeHTTP(w http.ResponseWriter,r * http.Request){
log.Printf(Handle request:%s%s,r.Method,r.RequestURI)
defer log.Printf( 完成请求:%s%s,r.Method,r.RequestURI)
h.fn(w,r)
}
func main(){
http.Handle(/ ide,http.StripPrefix(/ ide,& wHandler {fn:serveIDE}))
http.Han dle(/ console,http.StripPrefix(/ console,& wHandler {fn:serveConsole}))
err:= http.ListenAndServe(:9090,nil)
if err != nil {
log.Fatal(ListenAndServe:,err)
}
}
Any mistakes in below code? Multiple directory serving is not working from the below code. When I access the localhost:9090/ide, the server will return 404 error.
package main
import (
"log"
"net/http"
)
func serveIDE(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("/home/user/ide")).ServeHTTP(w, r)
}
func serveConsole(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("/home/user/console")).ServeHTTP(w, r)
}
func main() {
http.HandleFunc("/ide", serveIDE)
http.HandleFunc("/console", serveConsole)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
When I change the code like this,
http.HandleFunc("/", serveIDE)
It will work as I expected.
One of the issues with using http.FileServer
is that the request path is used to build the file name, so if you're serving from anywhere but the root you need to strip the route prefix to that handler.
The standard library includes a helpful tool for that http.StripPrefix
, but that only works on http.Handler
s, not http.HandleFunc
s, so to use it you need to adapt your HandleFunc
to a Handler
.
Here is a working version that should do what you want. Note that wHandler is just an adapter from your HttpFunc
methods to Hander
interface:
package main
import (
"log"
"net/http"
)
func serveIDE(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("/home/user/ide")).ServeHTTP(w, r)
}
func serveConsole(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("/home/user/console")).ServeHTTP(w, r)
}
type wHandler struct {
fn http.HandlerFunc
}
func (h *wHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handle request: %s %s", r.Method, r.RequestURI)
defer log.Printf("Done with request: %s %s", r.Method, r.RequestURI)
h.fn(w, r)
}
func main() {
http.Handle("/ide", http.StripPrefix("/ide", &wHandler{fn: serveIDE}))
http.Handle("/console", http.StripPrefix("/console", &wHandler{fn: serveConsole}))
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
这篇关于多个Dir服务不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!