在Golang中加载图像和CSS文件。

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

Load image and css in Golang

问题

我在项目的根目录下的server.js文件中设置了一个路由。

http.HandleFunc("/",route.IndexHandler)

IndexHandlerroute包中实现,代码如下:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    data := struct {
        Name string
    }{
        "My name",
    }
    util.RenderTemplate(w, "index", data)
}

RenderTemplate函数在util包中实现,代码如下:

func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    cwd, _ := os.Getwd()
    t, err := template.ParseFiles(filepath.Join(cwd, "./view/"+tmpl+".html"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

项目的目录结构如下:

/
/public/css
/public/images
/public/js
/route
/view

index.html视图文件位于view文件夹中,路由器位于route文件夹中。

index.html中,我使用以下方式包含资源:

<link rel="stylesheet" type="text/css" href="../public/css/style.css">
<img src="../public/images/img_landing_page_mac.png">

当请求适当的路径时,index.html仍然被渲染,但是图片和样式表没有加载。我应该如何在Golang的HTML模板引擎中包含它们?

英文:

I setup a route in server.js in package main in root directory of project

http.HandleFunc(&quot;/&quot;,route.IndexHandler)

The IndexHandler is implemented in package route like this:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	data:=struct{
		Name string
	}{
		&quot;My name&quot;,
	}
	util.RenderTemplate(w, &quot;index&quot;, data)
}

The RenderTemplate function is implemented in package util like this:

func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    cwd, _ := os.Getwd()
    t, err := template.ParseFiles(filepath.Join(cwd, &quot;./view/&quot; + tmpl + &quot;.html&quot;))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

Directory structure in project like this:

/
/public/css
/public/images
/public/js
/route
/view

index.html view is located in folder view, router is in folder route

In index.html I include resources like these:

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;../public/css/style.css&quot;&gt;

&lt;img src=&quot;../public/images/img_landing_page_mac.png&quot;&gt;

When request the appropriate path, index.html is still rendered, but images and stylesheet are not loaded. How can I do to include them in Golang html template engine?

答案1

得分: 16

你需要明确要求服务器提供静态文件的服务。

请参考http.FileServer

在你的情况下,注册另一个处理程序。

http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
英文:

You need to explicitly ask your server to serve static files.

See http.FileServer

In your case register another handler.

http.Handle(&quot;/public/&quot;, http.StripPrefix(&quot;/public/&quot;, http.FileServer(http.Dir(&quot;public&quot;))))

答案2

得分: 3

像Aruna说的那样,注册一个静态文件服务器处理程序

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

要在HTML中使用这些文件,只需

<img src="/public/images/img_landing_page_mac.png">
英文:

Like Aruna said, register a static file server handle

http.Handle(&quot;/public/&quot;, http.StripPrefix(&quot;/public/&quot;, http.FileServer(http.Dir(&quot;public&quot;))))

And to use the files in your HTML, simply

&lt;img src=&quot;/public/images/img_landing_page_mac.png&quot;&gt;

huangapple
  • 本文由 发表于 2016年3月9日 18:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/35888817.html
匿名

发表评论

匿名网友

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

确定