栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

多个目录服务不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多个目录服务不起作用

使用中的问题之一

http.FileServer
是请求路径用于构建文件名,因此,如果要从根目录以外的任何地方提供服务,则需要剥离到该处理程序的路由前缀。

标准库为此提供了一个有用的工具

http.StripPrefix
,但该工具只能在
http.Handler
s上使用,而不能在s
http.HandleFunc
上使用,因此要使用它,您需要使您适应
HandleFunc
a
Handler

这是一个应该执行您想要的工作版本。请注意,wHandler只是从

HttpFunc
方法到
Hander
接口的适配器:

package mainimport (        "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)        }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号