使用github.com/julienschmidt/httprouter在Golang中使用参数时提供静态css和java脚本

时间:2016-02-25 14:36:21

标签: go

所以我试图将静态css和java脚本提供给html模板,但这些参数阻碍了我这样做的能力。

这是我的代码

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

    //send the html to the page
    fmt.Printf("\nThe chapter is %s and the page is %s",chapter,page)
    index.Execute(rw,nil)
}

所以基本上,我想根据章节和页面变量提供不同的图像(这不是我当前的问题,但这是我需要url参数的原因),但是路由器认为是静态文件路径(我用来服务js和css的那个)充满了参数。我试着在html中的每个路径的乞讨中添加“/ foo / foo /”,但是这也没有用 以下是控制台的示例输出:

来自“/”

The chapter is 1 and the page is 1
The chapter is viewjs and the page is index.js
The chapter is viewjs and the page is index.js

来自“/ 1/2”

The chapter is 1 and the page is 2
The chapter is viewjs and the page is index.js
The chapter is viewjs and the page is index.js

here's包含所有文件的存储库,以便您可以看到我的项目结构。

谢谢!

1 个答案:

答案 0 :(得分:2)

我建议您重新构建项目文件

├── main.go
├── templates/
│   ├── index.html
├── assets/
    ├── js/
    └── react/

使用以下处理程序

router.GET("/", defaultHandler)
router.GET("/chapters/:chapter/pages/:page", paramHandler)
router.ServeFiles("/assets/*filepath", http.Dir("assets"))

并更改脚本来源以包含完整路径,例如

/assets/js/react/build/react.js

如果httprouter可以匹配像

这样的路线,事情就会轻松得多
router.GET("/:chapter/:page", paramHandler)
router.ServeFiles("/*filepath", http.Dir("assets"))

但是

  

仅显式匹配:根据此路由器的设计,请求只能匹配一个或不匹配   路由。

https://github.com/julienschmidt/httprouter#features