Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 404为非根目录路径中的文件提供服务时出错_Go_Google App Engine Golang - Fatal编程技术网

Go 404为非根目录路径中的文件提供服务时出错

Go 404为非根目录路径中的文件提供服务时出错,go,google-app-engine-golang,Go,Google App Engine Golang,我正在运行这个命令“go run webapp/main.go”。原因是应用程序引擎将从根目录调用我的应用程序,因此我将路径更改为从根目录调用文件。我也不介意你是否有Go最佳实践技巧 └── webapp ├── app.yaml ├── assets │ ├── css │ │ └── index.css │ └── img ├── main.go ├── main_test.go └── templates

我正在运行这个命令“go run webapp/main.go”。原因是应用程序引擎将从根目录调用我的应用程序,因此我将路径更改为从根目录调用文件。我也不介意你是否有Go最佳实践技巧

└── webapp
    ├── app.yaml
    ├── assets
    │   ├── css
    │   │   └── index.css
    │   └── img
    ├── main.go
    ├── main_test.go
    └── templates
        └── index.html
对如此琐碎的事情怎么会出错感到困惑。localhost:8080/css/index.css工作正常。我还有另一个处理函数来为localhost服务:8080/static/css/index.css,但是我得到了一个404错误。当我使用命令“go run main.go”并从代码中删除“webapp”时,一切都很顺利。但是,它如何使用/而不是/static/呢。从这个答案中可以看出,它应该充当/webapp/assets/static目录。我也尝试过http.StripPrefix,但也没有成功

package main

import (
    "flag"
    "log"
    "net/http"
    "os"
    "path/filepath"
    "sync"
    "text/template"
)

type templateHandler struct {
    once     sync.Once
    filename string
    templ    *template.Template
}

// ServeHTTP handles the HTTP request.
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    t.once.Do(func() {
        t.templ = template.Must(template.ParseFiles(filepath.Join("webapp", "templates", t.filename)))
    })
    if err := t.templ.Execute(w, r); err != nil {
        log.Printf("Error executing template: %v", err)
        http.Error(w, "Internal server error", http.StatusInternalServerError)
    }
}

func main() {
    dir, err := os.Getwd()
    if err != nil {
        log.Printf(err.Error())
    }
    log.Printf("dir: %s", dir)

    // command flags
    var addr = flag.String("addr", ":8080", "The addr of the application.")
    flag.Parse()

    // env variables
    envPort := os.Getenv("PORT")
    if envPort != "" {
        envPort = ":" + envPort
        addr = &envPort
    }

    fs := http.FileServer(http.Dir("./webapp/assets"))
    http.Handle("/static/", fs)

    log.Printf("Listening on port %s", *addr)

    // http.Handle("/", &templateHandler{filename: "index.html"})

    if err := http.ListenAndServe(*addr, fs); err != nil {
        log.Fatal(err)
    }
}
“它如何使用/而不是/static/”

因为您将
fs
直接传递到
ListenAndServe
,这意味着
http.Handle(“/static/”,fs)
所使用的正在被忽略/覆盖

:

Handle在中注册给定模式的处理程序 DefaultServeMux。ServeMux的文档解释了如何使用模式 是匹配的

ListendServe侦听TCP网络地址addr,然后调用 使用处理程序处理传入连接上的请求。 已接受的连接配置为启用TCP保持有效

处理程序通常为nil,在这种情况下,DefaultServeMux为 已使用。

ListendServe始终返回非零错误

所以一般来说,你应该这样做:

fs := http.FileServer(http.Dir("./webapp/assets"))
// register fs for "/static/" in DefaultServeMux
http.Handle("/static/", fs)
// start listening at addr and serve incoming requests
// using DefaultServeMux as the router (because nil).
if err := http.ListenAndServe(*addr, nil); err != nil {
    log.Fatal(err)
}
如果您的设置中存在其他问题,则您的应用程序可能无法立即按预期工作,但这一更改肯定是实现该目标的必要条件