动态路由的文件服务器目录 [英] Fileserver directory for dynamic route
问题描述
我的情况
已编译的角度项目的保存方式类似于
Compiled angular projects are saved like
.
├── 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)
条目:例如
id | 分支 | 提交 |
---|---|---|
abc | branch1 | commitC |
def | branch1 | commitD |
ghi | 母版 | commitA |
现在我要访问
:8080/apps/{id}
:8080/apps/{id}
例如:localhost:8080/apps/ abc
e.g.: localhost:8080/apps/abc
路径应在请求数据库条目后生成
the path should will be generated after requesting the DB entry
,文件服务器为目录提供服务./files/branch1/ commitC /
and a fileserver serves the directory ./files/branch1/commitC/
现在我希望看到文件夹app1和app2
now i would expect to see the folders app1 and app2
我有什么
func main() {
mux = mux.NewRouter()
mux.HandleFunc("/apps/{id}/{app}", s.serveApp).Methods("GET")
}
func (s *Server) serveApp(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
app := params["app"]
id := params["id"]
entry, err := getFromDB(id)
if err != nil {
w.Header().Set("Content-Type", "text/html")
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
file := filepath.Join(DefaultFolder, entry.Branch, entry.Commit, app, "index.html")
fmt.Printf("redirecting to %s", file)
http.ServeFile(w, r, file)
}
我该如何为整个目录提供服务,以便可以正确访问所有css和js文件?
how can I serve the whole directory like that, so all css and js files can be accessed correctly?
我想我需要像这样的东西
I think I need something like this
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
但是如何访问mux.Vars(request)建立目录路径?
but how can a access mux.Vars(request) to build up the directory path?
############ 关于CSS服务问题
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
mux := mux.NewRouter()
fs := http.FileServer(http.Dir("static"))
mux.Handle("/", fs)
log.Println("Listening...")
http.ListenAndServe(":3000", mux)
}
CSS文件用作文本/纯文本"
CSS files are served as 'text/plain'
文件:
- main.go
- 静态/
- index.html
- main.css
index.html
index.html
<!doctype html> <html> <head> <meta charset="utf-8"> <title>A static page</title> <link rel="stylesheet" href="main.css"> </head> <body> <h1>Hello from a static page</h1> </body> </html>
main.css
body {color: #c0392b}
推荐答案
http.FileServer
返回处理程序.可以手动调用此处理程序,而不是将其注册到修订路径.http.FileServer
returns a handler. This handler can be called manually instead of registered to a fix path.您将原始的
w http.ResponseWriter和r * http.Request
参数传递给它,因此http.FileServer能够访问请求并写入响应.You pass the original
w http.ResponseWriter, r *http.Request
parameters to it, so the http.FileServer is able to access the request and write the response.您可能需要将
http.FileServer
包装到http.StripPrefix
处理程序中,以便将原始请求中的路径简化为与path
变量.You will probably need to wrap the
http.FileServer
into ahttp.StripPrefix
handler so the path in the original request gets stripped down to a path relativ to thepath
variable.func main() { mux = mux.NewRouter() mux.HandleFunc("/apps/{id}/{app}", s.serveApp).Methods("GET") } func (s *Server) serveApp(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) app := params["app"] id := params["id"] entry, err := getFromDB(id) if err != nil { w.Header().Set("Content-Type", "text/html") respondWithError(w, http.StatusInternalServerError, err.Error()) return } path := filepath.Join(DefaultFolder, entry.Branch, entry.Commit, app) // the part already handled by serveApp handler must be stripped. baseURL := fmt.Sprintf("/apps/%s/%s/", id, app) pathHandler := http.FileServer(http.Dir(path)) http.StripPrefix(baseURL, pathHandler).ServeHTTP(w, r) // or in one line // http.FileServer(http.StripPrefix(baseURL, http.Dir(path)).ServeHTTP(w, r) }
这篇关于动态路由的文件服务器目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!