如何为golang webapp维护html和其他资源的文件夹结构?
在我的web应用程序中,我为web资源维护以下文件夹结构如何为golang webapp维护html和其他资源的文件夹结构?,html,go,directory,Html,Go,Directory,在我的web应用程序中,我为web资源维护以下文件夹结构 /Users/Dinesh/go/src/github.com/dineshappavoo/test-proj/ main.go README.md
/Users/Dinesh/go/src/github.com/dineshappavoo/test-proj/
main.go
README.md
app/
about.html
home.html
server.go
resources/
css/
js/
fonts/
mypack/
abc/
在应用文件夹下的server.go中,我正在启动web服务器并呈现html等。在main.go[顶级文件夹下]中,我正在尝试调用server.go函数。我这样做是因为我可以维护go依赖项并轻松构建项目
这是我的主要内容
package main
import (
"github.com/dineshappavoo/test-proj/app"
)
func main() {
app.Main()
}
这里有server.go代码段
package app
import (
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"regexp"
"strconv"
....
....
)
//Home page handler
func homeHandler(
w http.ResponseWriter,
r *http.Request,
) {
renderTemplate(w, "home", nil)
}
//About page handler
func aboutHandler(
w http.ResponseWriter,
r *http.Request,
) {
renderTemplate(w, "about", nil)
}
var templates = template.Must(template.ParseFiles("about.html", "home.html"))
func renderTemplate(
w http.ResponseWriter,
tmpl string,
p interface{},
) {
//If you use variables other than the struct u r passing as p, then "multiple response.WriteHeader calls" error may occur. Make sure you pass
//all variables in the struct even they are in the header.html embedded
if err := templates.ExecuteTemplate(w, tmpl+".html", p); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//URL validation for basic web services
var validPath = regexp.MustCompile("^/$|/(home|about)/(|[a-zA-Z0-9]+)$")
func validate(
fn func(
http.ResponseWriter,
*http.Request,
)) http.HandlerFunc {
return func(
w http.ResponseWriter,
r *http.Request,
) {
//log.Printf("Request URL Path %s ", r.URL.Path)
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r)
}
}
//Main method to install
func Main() {
log.Printf("TEST APP SERVER STARTED")
http.HandleFunc("/", validate(homeHandler))
http.HandleFunc("/home/", validate(homeHandler))
http.HandleFunc("/about/", validate(aboutHandler))
http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("resources"))))
//http.Handle("/templ/", http.StripPrefix("/templ/", http.FileServer(http.Dir("templ"))))
if *addr {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("final-port.txt", []byte(l.Addr().String()), 0644)
if err != nil {
log.Fatal(err)
}
s := &http.Server{}
s.Serve(l)
return
}
http.ListenAndServe(":8080", nil)
}
当我直接启动server.go时,它工作[通过将server.go中的Main()设为Main()并运行server.go]。但是当我使用上面的结构时,它失败了
错误:
panic: open about.html: no such file or directory
goroutine 1 [running]:
html/template.Must(0x0, 0x81a568, 0x11980f20, 0x0)
/usr/local/go/src/html/template/template.go:304 +0x40
github.com/dineshappavoo/test-proj/app.init()
/Users/Dinesh/go/src/github.com/dineshappavoo/test-proj/app/server.go:625 +0x1ec
main.init()
/Users/Dinesh/go/src/github.com/dineshappavoo/test-proj/main.go:9 +0x39
编辑-I
我给出了html模板的绝对路径。成功了。但是css没有加载。我改了下面一行
http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("/app/"))))
我是新来的语言。有人能帮我一下吗?您正在调用
template.Must
——这会导致出现错误,而应用程序正在生成错误,因为它找不到about.html
。如果您正在调用go run
,那么您的应用程序可能是从临时目录运行的。您应该调用go build
,或者提供模板文件的绝对路径(更好)。尽量避免在小型单文件程序之外使用go run
。@elithrar-我正在使用go build构建应用程序。/。。。然后去安装。。。在测试项目文件夹下。并通过./testproj运行项目。我应该改变这种方法吗?您的问题是您正在使用go-run-server.go
?不是这样吗?我过去只是想确保它正常工作。当我使用go run server.go时,它会工作。但是,当我使用顶级二进制文件时,出现了错误。/test projYou是对的。我使用了html文件的绝对路径。成功了。但我不知道为什么它在过去的几年里失败了。谢谢elithrar。我将补充这一答案。