Render css js img files in gorilla mux

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

Render css js img files in gorilla mux

问题

学习Gorilla Toolkit和Golang,请在这里给我一些指导。想要在相应的文件夹中渲染.css、.js和.jpeg文件。

文件结构如下:

ROOT/
 |--main.go, message.go
 |
 |Templates/
 |   |--index.html,confirmation.html
 |
 |Static/
   |
   |css/
   |--ex1.css, ex2.css
   |   
   |js/
   |--ex1.js, ex2.js
   |
   |Images/ 
   |--ex1.jpeg, ex2.jpeg

主要包含Gorilla pat和mux的main包如下:

package main

import (
  "github.com/gorilla/mux"
  "github.com/gorilla/pat"
  "html/template"
  "log"
  "net/http"
)

func main() {

  r := pat.New()
  r.Get("/", http.HandlerFunc(index))
  r.Post("/", http.HandlerFunc(send))
  r.Get("/confirmation", http.HandlerFunc(confirmation))

  log.Println("Listening...")
  http.ListenAndServe(":8080",r)

  router := mux.NewRouter()
  router.HandleFunc("/", Home)
  router.PathPrefix("/static/").Handler(http.StripPrefix("/static/",http.FileServer(http.Dir(./static/))))
  http.Handle("/", router)
  err := http.ListenAndServe(":8443", router)
  if err != nil {
    log.Fatalln(err)
    }
}

出现错误:

.\main.go:23: syntax error: unexpected .

不确定如何通过func main运行多个HTTP服务器来启动应用程序并渲染静态文件夹中的所有文件。

英文:

Learning Gorilla Toolkit and golang so please go easy here. Would like to render .css, .js, and .jpeg files in their corresponding folders.

File Structure is:

ROOT/
 |--main.go, message.go
 |
 |Templates/
 |   |--index.html,confirmation.html
 |
 |Static/
   |
   |css/
   |--ex1.css, ex2.css
   |   
   |js/
   |--ex1.js, ex2.js
   |
   |Images/ 
   |--ex1.jpeg, ex2.jpeg

Package main with gorilla pat and mux as follows:

package main

import (
  "github.com/gorilla/mux"
  "github.com/gorilla/pat"
  "html/template"
  "log"
  "net/http"
)

func main() {

  r := pat.New()
  r.Get("/", http.HandlerFunc(index))
  r.Post("/", http.HandlerFunc(send))
  r.Get("/confirmation", http.HandlerFunc(confirmation))

  log.Println("Listening...")
  http.ListenAndServe(":8080",r)

  router := mux.NewRouter()
  router.HandleFunc("/", Home)
  router.PathPrefix("/static/").Handler(http.StripPrefix("/static/",http.FileServer(http.Dir(./static/))))
  http.Handle("/", router)
  err := http.ListenAndServe(":8443", router)
  if err != nil {
    log.Fatalln(err)
    }
}

Getting error:

.\main.go:23: syntax error: unexpected .

Not sure how to run multiple http servers through func main to start app and render all files nested in static folder.

答案1

得分: 2

你应该:

  • 将 http.Dir 的参数改为字符串形式:http.Dir("./static/")
  • 使用 go 命令在一个单独的 Goroutine 中运行第一个 http.ListenAndServe
  • 删除 http.Handle("/", router) 这一行。这会将 Gorilla Mux 路由器注册为 http.DefaultServeMux/ 的处理程序,而你根本没有使用它。因此可以安全地删除它。

修改后的代码如下:

package main

import (
	"github.com/gorilla/mux"
	"github.com/gorilla/pat"
	"log"
	"net/http"
)

func main() {

	r := pat.New()
	r.Get("/", http.HandlerFunc(index))
	// etc...

	log.Println("Listening on :8080...")
	go http.ListenAndServe(":8080", r)

	router := mux.NewRouter()
	router.HandleFunc("/", home)
	router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))

	log.Println("Listening on :8443...")
	http.ListenAndServe(":8443", router)
}

func index(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Index page"))
}

func home(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Home page"))
}

希望对你有帮助!

英文:

You should:

  • Make the parameter to http.Dir a string: http.Dir("./static/").
  • Run the first http.ListenAndServe in a separate Goroutine using the go command.
  • Remove the line http.Handle("/", router). This registers the Gorilla Mux router as the handler for / in the http.DefaultServeMux, which you then don't use at all. So it can be safely removed.

Like so:

package main

import (
	"github.com/gorilla/mux"
	"github.com/gorilla/pat"
	"log"
	"net/http"
)

func main() {

	r := pat.New()
	r.Get("/", http.HandlerFunc(index))
	// etc...

	log.Println("Listening on :8080...")
	go http.ListenAndServe(":8080", r)

	router := mux.NewRouter()
	router.HandleFunc("/", home)
	router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))

	log.Println("Listening on :8443...")
	http.ListenAndServe(":8443", router)
}

func index(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Index page"))
}

func home(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Home page"))
}

huangapple
  • 本文由 发表于 2014年11月11日 02:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/26850676.html
匿名

发表评论

匿名网友

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

确定