Go 多目录服务不工作

Go 多目录服务不工作,go,Go,下面的代码有错误吗?从以下代码看,多目录服务不起作用。当我访问localhost:9090/ide时,服务器将返回404错误 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 serveCon

下面的代码有错误吗?从以下代码看,多目录服务不起作用。当我访问localhost:9090/ide时,服务器将返回404错误

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)
    }
}
当我像这样更改代码时

http.HandleFunc("/", serveIDE)

它会像我预期的那样工作。

使用
http.FileServer
的问题之一是,请求路径用于构建文件名,因此,如果您从根目录以外的任何位置提供服务,则需要将路由前缀剥离到该处理程序

标准库包括一个有用的工具,用于
http.StripPrefix
,但它只在
http.HandleFunc
s上工作,而不在
http.HandleFunc
s上工作,因此要使用它,您需要将
HandleFunc
调整为
处理程序

这里有一个工作版本,它可以做你想做的事情。请注意,wHandler只是从
HttpFunc
方法到
Hander
接口的适配器:

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)
        }
}

使用
http.FileServer
的问题之一是,请求路径用于构建文件名,因此,如果您从根目录以外的任何位置提供服务,则需要将路由前缀剥离到该处理程序

标准库包括一个有用的工具,用于
http.StripPrefix
,但它只在
http.HandleFunc
s上工作,而不在
http.HandleFunc
s上工作,因此要使用它,您需要将
HandleFunc
调整为
处理程序

这里有一个工作版本,它可以做你想做的事情。请注意,wHandler只是从
HttpFunc
方法到
Hander
接口的适配器:

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)
        }
}