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

93 lines
2.2 KiB
Go
Raw Normal View History

2021-03-22 16:25:47 +01:00
package backend
2017-05-16 16:00:01 +02:00
import (
"io"
"log"
"net/http"
"strings"
2017-05-16 17:24:08 +02:00
"github.com/julienschmidt/httprouter"
2021-03-22 15:11:31 +01:00
"github.com/alice-lg/alice-lg/client"
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.
2021-03-22 15:11:31 +01:00
func webPrepareClientHTML(html string) string {
2017-05-22 15:36:46 +02:00
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 /
2018-06-20 12:03:23 +02:00
func webRegisterAssets(ui UiConfig, router *httprouter.Router) error {
2017-05-16 16:00:01 +02:00
log.Println("Preparing and installing assets")
// Prepare client html: Rewrite paths
2021-03-22 15:41:36 +01:00
indexHTMLData, err := client.Assets.ReadFile("build/index.html")
2017-05-16 16:00:01 +02:00
if err != nil {
return err
}
2021-03-22 15:41:36 +01:00
indexHTML := string(indexHTMLData) // TODO: migrate to []byte
2017-05-16 16:00:01 +02:00
2018-06-25 23:44:02 +02:00
theme := NewTheme(ui.Theme)
err = theme.RegisterThemeAssets(router)
if err != nil {
log.Println("Warning:", err)
}
2018-06-20 12:03:23 +02:00
// Update paths
2021-03-22 15:11:31 +01:00
indexHTML = webPrepareClientHTML(indexHTML)
2017-05-16 16:00:01 +02:00
2018-06-20 12:03:23 +02:00
// Register static assets
2021-03-22 15:41:36 +01:00
router.Handler("GET", "/static/*path", client.AssetsHTTPHandler("/static"))
2018-06-20 12:03:23 +02:00
2017-05-16 16:00:01 +02:00
// Rewrite paths
2017-05-22 15:36:46 +02:00
// Serve index html as root...
2018-06-25 23:44:02 +02:00
router.GET("/",
func(res http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
// Include theme, we need to update the
// hashes on reload, so we can check if the theme has
// changed without restarting the app
2021-03-22 15:11:31 +01:00
themedHTML := theme.PrepareClientHTML(indexHTML)
io.WriteString(res, themedHTML)
2018-06-25 23:44:02 +02:00
})
2017-05-16 16:00:01 +02:00
2018-12-09 17:31:02 +01:00
// ...and all alice related paths aswell
alicePaths := []string{
"/routeservers/*path",
"/search/*path",
}
for _, path := range alicePaths {
// respond with app html
router.GET(path,
func(res http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
// ditto here
2021-03-22 15:11:31 +01:00
themedHTML := theme.PrepareClientHTML(indexHTML)
io.WriteString(res, themedHTML)
2018-12-09 17:31:02 +01:00
})
}
2017-05-22 15:36:46 +02:00
2018-12-10 10:46:28 +01:00
// ...install a catch all for /alice for graceful backwards compatibility
router.GET("/alice/*path",
func(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
http.Redirect(res, req, "/", 301)
})
2017-05-16 16:00:01 +02:00
return nil
}