mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
added client serving
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/GeertJohan/go.rice"
|
||||
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
@@ -11,21 +9,10 @@ func main() {
|
||||
printBanner()
|
||||
|
||||
// Load configuration
|
||||
log.Println("Using configuration: ...")
|
||||
|
||||
// Serve static assets
|
||||
assets := rice.MustFindBox("../client/build")
|
||||
assetsHandler := http.StripPrefix(
|
||||
"/static/",
|
||||
http.FileServer(assets.HTTPBox()))
|
||||
|
||||
index, err := assets.String("index.html")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println(index)
|
||||
|
||||
http.Handle("/static/", assetsHandler)
|
||||
// Serve static content
|
||||
httpRegisterAssets()
|
||||
|
||||
// Start http server
|
||||
http.ListenAndServe(":7340", nil)
|
||||
|
47
backend/web.go
Normal file
47
backend/web.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/GeertJohan/go.rice"
|
||||
)
|
||||
|
||||
// Web Client
|
||||
// Handle assets and client app preprarations
|
||||
|
||||
// Register assets handler and index handler
|
||||
// at /static and /
|
||||
func httpRegisterAssets() error {
|
||||
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
|
||||
http.Handle("/static/", assetsHandler)
|
||||
|
||||
// Prepare client html: Rewrite paths
|
||||
indexHtml, err := assets.String("index.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pathRewriter := strings.NewReplacer(
|
||||
"js/", "/static/js/",
|
||||
"css/", "/static/css/")
|
||||
indexHtml = pathRewriter.Replace(indexHtml)
|
||||
|
||||
// Rewrite paths
|
||||
// Serve index html as root
|
||||
http.HandleFunc("/", func(res http.ResponseWriter, _ *http.Request) {
|
||||
io.WriteString(res, indexHtml)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user