在Go的网站地址路径中,将文件系统中的两个文件夹作为一个虚拟文件夹。

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

Two folders in the file system as one virtual folder in the path of the site address (web server in Go)

问题

我在文件系统中有两个文件夹,分别是"files1"和"files2"。

我可以将文件系统中的一个文件夹作为站点地址路径下的一个虚拟文件夹进行托管,代码如下:

  1. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./files1"))))

如何将"files1"和"files2"文件夹的内容都托管到站点地址路径"/public/"下呢?

英文:

I have two folders in the file system "files1" and "files2".

I can host one folder in the file system as one virtual folder in the path of the site address like this:

  1. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./files1"))))

How can I host the contents of the "files1" and "files2" folders on the same path of the site address "/public/" ?

答案1

得分: 4

一个简单的解决方案是实现http.FileSystem接口。

以下是示例代码:

  1. package main
  2. import (
  3. "errors"
  4. "io/fs"
  5. "net/http"
  6. )
  7. func main() {
  8. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(mergedDir{
  9. Dir1: http.Dir("./files1"),
  10. Dir2: http.Dir("./files2"),
  11. })))
  12. http.ListenAndServe(":8080", nil)
  13. }
  14. type mergedDir struct {
  15. Dir1 http.Dir // 首先尝试访问Dir1,因此它具有更高的优先级。
  16. Dir2 http.Dir
  17. }
  18. func (d mergedDir) Open(name string) (http.File, error) {
  19. f, err := d.Dir1.Open(name)
  20. if err != nil {
  21. if errors.Is(err, fs.ErrNotExist) {
  22. return d.Dir2.Open(name)
  23. }
  24. }
  25. return f, err
  26. }

我已经使用以下目录结构进行了测试:

  1. ├── files1
  2. ├── f1-1.txt
  3. └── f1-sub
  4. └── f1-s.txt
  5. └── files2
  6. ├── f1-1.txt
  7. ├── f2-1.txt
  8. └── f2-sub
  9. └── f2-s.txt

有两个f1-1.txt,因为首先尝试访问files1,所以会提供files1中的版本。


更新

根据作者的要求,这是支持多个目录的mergedDir的另一个版本:

  1. type mergedDir struct {
  2. Dirs []http.Dir
  3. }
  4. func (d mergedDir) Open(name string) (http.File, error) {
  5. for _, dir := range d.Dirs {
  6. f, err := dir.Open(name)
  7. if err == nil {
  8. return f, nil
  9. }
  10. if !errors.Is(err, fs.ErrNotExist) {
  11. return f, err
  12. }
  13. }
  14. return nil, fs.ErrNotExist
  15. }
英文:

A simple solution is to implement the http.FileSystem interface.

Here is the demo:

  1. package main
  2. import (
  3. "errors"
  4. "io/fs"
  5. "net/http"
  6. )
  7. func main() {
  8. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(mergedDir{
  9. Dir1: "./files1",
  10. Dir2: "./files2",
  11. })))
  12. http.ListenAndServe(":8080", nil)
  13. }
  14. type mergedDir struct {
  15. Dir1 http.Dir // Dir1 will be tried first so it has higher priority.
  16. Dir2 http.Dir
  17. }
  18. func (d mergedDir) Open(name string) (http.File, error) {
  19. f, err := d.Dir1.Open(name)
  20. if err != nil {
  21. if errors.Is(err, fs.ErrNotExist) {
  22. return d.Dir2.Open(name)
  23. }
  24. }
  25. return f, err
  26. }

I have tested with this directory structure:

  1. ├── files1
  2. │   ├── f1-1.txt
  3. │   └── f1-sub
  4. │   └── f1-s.txt
  5. └── files2
  6. ├── f1-1.txt
  7. ├── f2-1.txt
  8. └── f2-sub
  9. └── f2-s.txt

There are two f1-1.txt, the one in files1 is served because files1 is tried first.


Update:

Another version of mergedDir to support multiple directories as requested by the author:

  1. type mergedDir struct {
  2. Dirs []http.Dir
  3. }
  4. func (d mergedDir) Open(name string) (http.File, error) {
  5. for _, dir := range d.Dirs {
  6. f, err := dir.Open(name)
  7. if err == nil {
  8. return f, nil
  9. }
  10. if !errors.Is(err, fs.ErrNotExist) {
  11. return f, err
  12. }
  13. }
  14. return nil, fs.ErrNotExist
  15. }

huangapple
  • 本文由 发表于 2023年4月5日 21:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75940008.html
匿名

发表评论

匿名网友

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

确定