你可以使用golang的Gorilla mux来查找CSS文件。

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

How can I find CSS files using golang Gorilla mux

问题

我正在使用Go语言和Gorilla Mux。

这是我的webserver.go文件:

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func HomeHandler(rw http.ResponseWriter, r *http.Request) {
	http.ServeFile(rw, r, "index.html")
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", HomeHandler)

	http.Handle("/", r)

	log.Println("Server running on :8080")
	err := http.ListenAndServe(":8080", r)
	if err != nil {
		log.Printf("Error: %s\n", err.Error())
	}
}

在与webserver.go文件位于同一文件夹的位置上,有一个index.html文件。

/ - 这里是index.html

/css - 所有的CSS文件

/images - 所有的图片和资源文件

我成功使用上述代码加载了index.html文件,但似乎无法加载CSS文件和图片。

在index.html文件中,我有以下内容:

<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />

所以它应该能够找到CSS文件,或者我需要确保"Go"能够找到css和image文件夹吗?如何做到这一点?

英文:

I'm using Go with Gorilla Mux.

This is my webserver.go file

package main

import (
  &quot;log&quot;
  &quot;net/http&quot;

  &quot;github.com/gorilla/mux&quot;
)

func HomeHandler(rw http.ResponseWriter, r *http.Request) {
  http.ServeFile(rw, r, &quot;index.html&quot;)
}

func main() {
  r := mux.NewRouter()
  r.HandleFunc(&quot;/&quot;, HomeHandler)

  http.Handle(&quot;/&quot;, r)

  log.Println(&quot;Server running on :8080&quot;)
  err := http.ListenAndServe(&quot;:8080&quot;, r)
  if err != nil {
	 log.Printf(&quot;Error: %s\n&quot;, err.Error())
  }
}

In the same folder where the webserver.go file is located is the index.html file.

/ - here is the index.html

/css - All the CSS files

/images - All the images, resource files

I manage to load the index.html file using the above code but it doesn't seem to load the CSS files and images.

inside the index.html file I have.

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/demo.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/style.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/animate-custom.css&quot; /&gt;

So it should find the css files or do I have to make sure that "Go" can find the css and image folder?
How?

答案1

得分: 5

你可以使用http.FileServer来独立地提供所有静态文件,而不依赖于gorilla/mux

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func HomeHandler(rw http.ResponseWriter, r *http.Request) {
	http.ServeFile(rw, r, "index.html")
}

func main() {
	r := mux.NewRouter()

	cssHandler := http.FileServer(http.Dir("./css/"))
	imagesHandler := http.FileServer(http.Dir("./images/"))

	http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
	http.Handle("/images/", http.StripPrefix("/images/", imagesHandler))
	r.HandleFunc("/", HomeHandler)
	http.Handle("/", r)

	log.Println("Server running on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
英文:

You can use http.FileServer for serving all the static files independently of gorilla/mux.

package main

import (
	&quot;log&quot;
	&quot;net/http&quot;

	&quot;github.com/gorilla/mux&quot;
)

func HomeHandler(rw http.ResponseWriter, r *http.Request) {
	http.ServeFile(rw, r, &quot;index.html&quot;)
}

func main() {
	r := mux.NewRouter()

	cssHandler := http.FileServer(http.Dir(&quot;./css/&quot;))
	imagesHandler := http.FileServer(http.Dir(&quot;./images/&quot;))

	http.Handle(&quot;/css/&quot;, http.StripPrefix(&quot;/css/&quot;, cssHandler))
	http.Handle(&quot;/images/&quot;, http.StripPrefix(&quot;/images/&quot;, imagesHandler))
	r.HandleFunc(&quot;/&quot;, HomeHandler)
    http.Handle(&quot;/&quot;, r)

	log.Println(&quot;Server running on :8080&quot;)
    log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
}

huangapple
  • 本文由 发表于 2015年2月28日 23:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/28783541.html
匿名

发表评论

匿名网友

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

确定