多目录服务不起作用

huangapple go评论152阅读模式
英文:

Multiple Dir serving is not working

问题

以下是翻译好的代码:

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)

它将按照我预期的工作。

英文:

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.

答案1

得分: 4

使用http.FileServer的一个问题是请求路径被用来构建文件名,所以如果你不是从根目录提供服务,你需要去掉路由前缀到处理程序。

标准库中包含一个有用的工具http.StripPrefix,但它只适用于http.Handler,而不是http.HandleFunc,所以要使用它,你需要将你的HandleFunc适配为Handler

下面是一个可以实现你想要的功能的工作版本。注意,wHandler只是从你的HttpFunc方法适配到Handler接口的适配器:

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

希望对你有帮助!

英文:

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.Handlers, not http.HandleFuncs, 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)
        }
}

huangapple
  • 本文由 发表于 2017年4月25日 10:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/43600768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定