Skip to content

Commit d12557c

Browse files
author
James / creativenucleus
committed
Embedded web-static assets
1 parent e3b5687 commit d12557c

57 files changed

Lines changed: 122 additions & 62 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api-json.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ import (
77

88
func apiOutErr(w http.ResponseWriter, err error, statusCode int) {
99
w.Header().Set("Content-Type", "application/json")
10-
http.Error(w, err.Error(), statusCode)
10+
w.WriteHeader(statusCode)
11+
12+
type errJson struct {
13+
Error string `json:"error"`
14+
}
15+
16+
out := errJson{
17+
Error: err.Error(),
18+
}
19+
20+
json.NewEncoder(w).Encode(out)
1121
}
1222

1323
func apiOutResponse(w http.ResponseWriter, data interface{}, statusCode int) {

client-panel.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"log"
89
"net/http"
910
"strconv"
@@ -40,12 +41,11 @@ func startClientPanel(port int) error {
4041
ReadHeaderTimeout: 3 * time.Second,
4142
}
4243

43-
fs := http.FileServer(http.Dir("./web-static"))
44-
http.Handle("/static/", http.StripPrefix("/static/", fs))
45-
46-
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
47-
http.ServeFile(w, r, "/web-static/favicon/favicon.ico")
48-
})
44+
subFs, err := fs.Sub(embed.WebStaticAssets, "web-static")
45+
if err != nil {
46+
return err
47+
}
48+
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(subFs))))
4949

5050
fmt.Printf("In a web browser, go to http://localhost:%d/%s\n", port, session)
5151

embed/embed.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package embed
22

33
import (
4+
"embed"
45
_ "embed"
56
)
67

@@ -13,6 +14,9 @@ var ServerIndexHtml []byte
1314
//go:embed web/client/index.html
1415
var ClientIndexHtml []byte
1516

17+
//go:embed web-static
18+
var WebStaticAssets embed.FS
19+
1620
//go:embed tic-code/jukebox.lua
1721
var LuaJukebox []byte
1822

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,38 @@ class BjmrAjax {
6565
}
6666

6767
makeReq = async(method, endpoint, data) => {
68-
// Making our request
69-
const response = await fetch(`/${this.sessionKey}/api/${endpoint}.json`, {
70-
method: method,
71-
// mode: "cors", // no-cors, *cors, same-origin
72-
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
73-
// credentials: "same-origin", // include, *same-origin, omit
74-
headers: {
75-
"Content-Type": "application/json",
76-
},
77-
// redirect: "follow", // manual, *follow, error
78-
// referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
79-
body: JSON.stringify(data), // body data type must match "Content-Type" header
80-
});
81-
return await response.json();
68+
const out = {
69+
ok: false,
70+
code: null,
71+
data: null,
72+
}
73+
74+
try {
75+
const response = await fetch(`/${this.sessionKey}/api/${endpoint}.json`, {
76+
method: method,
77+
// mode: "cors", // no-cors, *cors, same-origin
78+
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
79+
// credentials: "same-origin", // include, *same-origin, omit
80+
headers: {
81+
"Content-Type": "application/json",
82+
},
83+
// redirect: "follow", // manual, *follow, error
84+
// referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
85+
body: JSON.stringify(data), // body data type must match "Content-Type" header
86+
});
87+
88+
out.code = response.status;
89+
out.data = await response.json();
90+
} catch (error) {
91+
console.error("There has been a problem with your fetch operation:", error);
92+
}
93+
94+
out.ok = (out.code >= 200 && out.code <= 299)
95+
if (!out.ok) {
96+
addToLog(`ERROR ${out.code}: ${out.data?.error}`);
97+
}
98+
99+
return out;
82100
}
83101
}
84102

File renamed without changes.
File renamed without changes.

web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.css renamed to embed/web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.css

File renamed without changes.

web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.css.map renamed to embed/web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.css.map

File renamed without changes.

web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.min.css renamed to embed/web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.min.css

File renamed without changes.

web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.min.css.map renamed to embed/web-static/package/bootstrap-5.3.2-dist/css/bootstrap-grid.min.css.map

File renamed without changes.

0 commit comments

Comments
 (0)