英文:
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 thego
command. - Remove the line
http.Handle("/", router)
. This registers the Gorilla Mux router as the handler for/
in thehttp.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"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论