go - 文件服务器返回 404
问题描述
我真正想做的是提供两个目录作为文件服务器,/assets
和/web/public
. 作为参考,这是我的项目结构:
/root
/assets
/web
/public
/src
/templates
main.go
但是,当我运行服务器时,/public
路由返回 404,而资产很好。我梳理了参考资料和一堆教程,我所做的一切都应该在董事会之上。
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
// here is where i set up the file servers
publicHandler := http.FileServer(http.Dir(filepath.Join("web", "public")))
r.PathPrefix("/pubic/").Handler(http.StripPrefix("/pubic/", publicHandler))
assetHandler := http.FileServer(http.Dir("assets"))
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", assetHandler))
r.HandleFunc("/", s.HandleHome())
port := fmt.Sprintf(":%d", 5123)
http.ListenAndServe(port, r)
}
func (s *Server) HandleHome() http.HandlerFunc {
tpl, err := template.New("").ParseFiles("web/templates/index.tpl")
if err != nil {
panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
err = tpl.ExecuteTemplate(w, "index", nil)
if err != nil {
http.Error(w, "oops", http.StatusInternalServerError)
}
}
}
解决方案
推荐阅读
- javascript - 有助于 React 模块的简单问题
- css - 带有css的多个后代子选择器
- php - 重新排列多维php数组
- azure - SSIS 包执行失败(数据工厂) - 脚本组件未加载代码
- filter - 过滤数据中的 Dax 总和和加权平均值
- templates - Typo3 / 空白模板
- azure - 为什么在创建 QueueTrigger WebJob 函数时会看到 FunctionIndexingException?
- javascript - 当从快速 ui 事件触发时,添加补间以按顺序完成
- go - 为什么我的 Golang Workerpool 忽略作业?
- javascript - 如何在本机反应中调整图像大小?