所以我试图为 html 模板提供静态 css 和 java 脚本,但参数阻碍了我这样做的能力。
这是我的代码
package main
import (
"net/http"
"html/template"
"github.com/julienschmidt/httprouter"
"fmt"
)
type PageData struct {
Chapter int
Page int
Source string
}
func main(){
//I'm using the julienschmidt router because it has parameters that I can use
//Create a router
router := httprouter.New()
//Create the route with the parameters
router.GET("/:chapter/:page",paramHandler)
//Create the default route last
router.GET("/",defaultHandler)
//get all of the static files working
router.ServeFiles("/:chapter/:page/*filepath",http.Dir("/js/"))
//http.Handle("/js/",http.StripPrefix("/js/",http.FileServer(http.Dir("./public/js/"))))
//http.Handle("/viewjs/",http.StripPrefix("/viewjs/",http.FileServer(http.Dir("./public/viewjs/"))))
//crate a message telling the user which port the server is running on
fmt.Println("Now serving on port 8080")
//Start the server on the specified port
http.ListenAndServe(":8080",router)
}
func defaultHandler(rw http.ResponseWriter,r *http.Request,p httprouter.Params){
//Parse the html file
index := template.Must(template.ParseFiles("public/index.html"))
chapter := 1
page := 1
//Get data from server
//TODO
//Test Data
//defaultPage := PageData{Chapter:chapter,Page:page,Source:"http://lokeshdhakar.com/projects/lightbox2/images/image-4.jpg"}
//Send the html file to the browser
fmt.Printf("\nThe chapter is %d and the page is %d",chapter,page)
index.Execute(rw,nil)
}
func paramHandler(rw http.ResponseWriter,r*http.Request,p httprouter.Params){
index := template.Must(template.ParseFiles("public/index.html"))
//Get the page parameters
chapter := p.ByName("chapter")
page:= p.ByName("page")
//Get data from server
//TODO
所以基本上,我想根据章节和页面变量提供不同的图像(这不是我当前的问题,但这是我需要 url 参数的原因),但路由器认为静态文件路径(我用来服务js和css)充满了参数。我试图将“/foo/foo/”添加到html中每个路径的begging中,
繁星点点滴滴
相关分类