如何维护golang webapp的html和其他资源的文件夹结构?

时间:2015-12-26 00:51:12

标签: html go directory

在我的Web应用程序中,我维护了以下Web资源的文件夹结构,

/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/

在app.folder下的server.go中,我正在启动Web服务器并呈现html等。在main.go [在顶级文件夹下]我试图调用server.go函数。我这样做是因为我可以维护go依赖关系并轻松构建项目。

这是我的main.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/"))))

我是新手语言。有谁可以帮我这个?

0 个答案:

没有答案