use httprouter

This commit is contained in:
Matthias Hannig 2017-05-16 17:24:08 +02:00
parent 66845f5861
commit a5f067b708
2 changed files with 14 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func main() {
@ -11,9 +13,15 @@ func main() {
// Load configuration
log.Println("Using configuration: ...")
// Setup request routing
router := httprouter.New()
// Serve static content
httpRegisterAssets()
err := httpRegisterAssets(router)
if err != nil {
log.Fatal(err)
}
// Start http server
http.ListenAndServe(":7340", nil)
log.Fatal(http.ListenAndServe(":7340", router))
}

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/GeertJohan/go.rice"
"github.com/julienschmidt/httprouter"
)
// Web Client
@ -14,7 +15,7 @@ import (
// Register assets handler and index handler
// at /static and /
func httpRegisterAssets() error {
func httpRegisterAssets(router *httprouter.Router) error {
log.Println("Preparing and installing assets")
// Serve static assets
@ -24,7 +25,7 @@ func httpRegisterAssets() error {
http.FileServer(assets.HTTPBox()))
// Register static assets
http.Handle("/static/", assetsHandler)
router.Handler("GET", "/static/*path", assetsHandler)
// Prepare client html: Rewrite paths
indexHtml, err := assets.String("index.html")
@ -39,7 +40,7 @@ func httpRegisterAssets() error {
// Rewrite paths
// Serve index html as root
http.HandleFunc("/", func(res http.ResponseWriter, _ *http.Request) {
router.GET("/", func(res http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
io.WriteString(res, indexHtml)
})