1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

69 lines
1.4 KiB
Go
Raw Normal View History

2017-05-16 16:00:01 +02:00
package main
import (
"io"
"log"
"net/http"
"strings"
"github.com/GeertJohan/go.rice"
2017-05-16 17:24:08 +02:00
"github.com/julienschmidt/httprouter"
2017-05-16 16:00:01 +02:00
)
// Web Client
// Handle assets and client app preprarations
2017-05-22 15:36:46 +02:00
// Prepare client HTML:
// Set paths and add version to assets.
func webPrepareClientHtml(html string) string {
status, _ := NewAppStatus()
// Replace paths and tags
rewriter := strings.NewReplacer(
// Paths
"js/", "/static/js/",
"css/", "/static/css/",
// Tags
"APP_VERSION", status.Version,
)
html = rewriter.Replace(html)
return html
}
2017-05-16 16:00:01 +02:00
// Register assets handler and index handler
// at /static and /
2017-05-22 15:36:46 +02:00
func webRegisterAssets(router *httprouter.Router) error {
2017-05-16 16:00:01 +02:00
log.Println("Preparing and installing assets")
// Serve static assets
assets := rice.MustFindBox("../client/build")
assetsHandler := http.StripPrefix(
"/static/",
http.FileServer(assets.HTTPBox()))
// Register static assets
2017-05-16 17:24:08 +02:00
router.Handler("GET", "/static/*path", assetsHandler)
2017-05-16 16:00:01 +02:00
// Prepare client html: Rewrite paths
indexHtml, err := assets.String("index.html")
if err != nil {
return err
}
2017-05-22 15:36:46 +02:00
indexHtml = webPrepareClientHtml(indexHtml)
2017-05-16 16:00:01 +02:00
// Rewrite paths
2017-05-22 15:36:46 +02:00
// Serve index html as root...
2017-05-16 17:24:08 +02:00
router.GET("/", func(res http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
2017-05-16 16:00:01 +02:00
io.WriteString(res, indexHtml)
})
2017-05-22 15:36:46 +02:00
// ...and as catch all
router.GET("/alice/*path", func(res http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
io.WriteString(res, indexHtml)
})
2017-05-16 16:00:01 +02:00
return nil
}