Http 动态路由的文件服务器目录

Http 动态路由的文件服务器目录,http,go,handle,mux,Http,Go,Handle,Mux,我的场景 编译的角度项目保存为 . ├── branch1 │   ├── commitC │   │   ├── app1 │   │   │   ├── index.html │   │   │   └── stylesheet.css │   └── commitD │   ├── app1 │   │   ├── index.html │   │   └── stylesheet.css │   └── app2 │   ├── index

我的场景

编译的角度项目保存为

.
├── branch1
│   ├── commitC
│   │   ├── app1
│   │   │   ├── index.html
│   │   │   └── stylesheet.css
│   └── commitD
│       ├── app1
│       │   ├── index.html
│       │   └── stylesheet.css
│       └── app2
│           ├── index.html
│           └── stylesheet.css
├── branch2
│   ├── commitE
│      ├── app1
│      │   ├── index.html
│      │   └── stylesheet.css
│      └── app2
│          ├── index.html
│          └── stylesheet.css
└── master
    ├── commitA
    │   ├── app1
    │   │   ├── index.html
    │   │   └── stylesheet.css
    └── commitB
        ├── app1
            ├── index.html
            └── stylesheet.css

数据库

TABLE data(id , branch, commit)
参赛作品:例如

身份证件 分支 犯罪 abc branch1 委员会 def branch1 承诺 ghi 主人 委员会
http.FileServer
返回一个处理程序。可以手动调用此处理程序,而不是注册到修复路径

您将原始的
w http.ResponseWriter,r*http.Request
参数传递给它,因此http.FileServer能够访问请求并写入响应

您可能需要将
http.FileServer
包装到
http.StripPrefix
处理程序中,以便原始请求中的路径被剥离为相对于
path
变量的路径

func main(){
mux=mux.NewRouter()
HandleFunc(“/apps/{id}/{app}”,s.serveApp.Methods(“GET”)
}
func(s*Server)serveApp(w http.ResponseWriter,r*http.Request){
参数:=mux.Vars(r)
应用程序:=参数[“应用程序”]
id:=参数[“id”]
条目,错误:=getFromDB(id)
如果错误!=零{
w、 Header().Set(“内容类型”、“文本/html”)
respondWithError(w,http.StatusInternalServerError,err.Error())
返回
}
路径:=filepath.Join(DefaultFolder、entry.Branch、entry.Commit、app)
//必须剥离已由serveApp处理程序处理的部件。
baseURL:=fmt.Sprintf(“/apps/%s/%s/”,id,app)
pathHandler:=http.FileServer(http.Dir(path))
StripPrefix(baseURL,pathHandler).ServeHTTP(w,r)
//还是一行
//FileServer(http.StripPrefix(baseURL,http.Dir(path)).ServeHTTP(w,r)
}

http.FileServer
返回处理程序。可以手动调用此处理程序,而不是注册到修复路径

您将原始的
w http.ResponseWriter,r*http.Request
参数传递给它,因此http.FileServer能够访问请求并写入响应

您可能需要将
http.FileServer
包装到
http.StripPrefix
处理程序中,以便原始请求中的路径被剥离为相对于
path
变量的路径

func main(){
mux=mux.NewRouter()
HandleFunc(“/apps/{id}/{app}”,s.serveApp.Methods(“GET”)
}
func(s*Server)serveApp(w http.ResponseWriter,r*http.Request){
参数:=mux.Vars(r)
应用程序:=参数[“应用程序”]
id:=参数[“id”]
条目,错误:=getFromDB(id)
如果错误!=零{
w、 Header().Set(“内容类型”、“文本/html”)
respondWithError(w,http.StatusInternalServerError,err.Error())
返回
}
路径:=filepath.Join(DefaultFolder、entry.Branch、entry.Commit、app)
//必须剥离已由serveApp处理程序处理的部件。
baseURL:=fmt.Sprintf(“/apps/%s/%s/”,id,app)
pathHandler:=http.FileServer(http.Dir(path))
StripPrefix(baseURL,pathHandler).ServeHTTP(w,r)
//还是一行
//FileServer(http.StripPrefix(baseURL,http.Dir(path)).ServeHTTP(w,r)
}

剥离部分可能需要一些调整。感谢您的帮助,该部分现在可以工作了。下一个问题是css文件没有正确提供…拒绝应用“localhost:3000/main.css”的样式,因为其MIME类型(“text/plain”)不是受支持的样式表MIME类型,并且启用了严格的MIME检查…我应该在哪里设置mimetype?。在问题的底部添加了示例。也许您有一个想法:)MIME类型是通过头
内容类型
设置的。因此您可以使用
w.header().set(“内容类型”,“文本/html”)来设置它
。但是我想知道为什么http.FileServer没有正确设置它。您已经自己设置了标题吗?注意:提出额外的问题最好在新问题或评论中完成。编辑原始问题有点问题,因为答案不必再进行编辑以容纳额外的问题。删除部分:ht需要一些调整。感谢您的帮助,该部分现在可以工作了。下一个问题是css文件没有正确提供…拒绝应用“localhost:3000/main.css”的样式,因为它的MIME类型是('text/plain')不是受支持的样式表MIME类型,并且启用了严格的MIME检查…我应该在哪里设置mimetype?。在问题的底部添加了示例。也许您有一个想法:)MIME类型是通过标题
内容类型
设置的。因此,您可以使用
w.Header().set(“内容类型”、“文本/html”)
进行设置。但是我想知道为什么http.FileServer没有正确设置它。你已经自己设置标题了吗?注意:问额外的问题最好在新问题或评论中完成。编辑原始问题有点问题,因为答案不一定会再次编辑以适应附加问题。