Skip to content

Commit 4389b5a

Browse files
committed
Move admin ui into embedded package, update ui routes to work with embedded content, update makefile
1 parent c93b852 commit 4389b5a

Some content is hidden

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

73 files changed

+62
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ docker:
2222
sudo docker run -u "$(ID):$(GID)" --rm -t -v `pwd`:/wag wag_builder
2323

2424
.build_ui:
25-
cd adminui/src; npm install; gulp build
25+
cd adminui/frontend; npm run build
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

adminui/frontend/embed.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package frontend
2+
3+
import (
4+
"embed"
5+
"io"
6+
"io/fs"
7+
"net/http"
8+
)
9+
10+
var (
11+
//go:embed dist/assets/*
12+
adminResources embed.FS
13+
14+
//go:embed dist/index.html dist/favicon.ico
15+
index embed.FS
16+
17+
distFiles = must(fs.Sub(adminResources, "dist"))
18+
)
19+
20+
func must(f fs.FS, err error) fs.FS {
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
return f
26+
}
27+
28+
func Index(w http.ResponseWriter, r *http.Request) {
29+
30+
f, err := index.Open("dist/index.html")
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
w.Header().Set("content-type", "text/html; charset=utf-8")
36+
io.Copy(w, f)
37+
}
38+
39+
func Favicon(w http.ResponseWriter, r *http.Request) {
40+
41+
f, err := index.Open("dist/favicon.ico")
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
w.Header().Set("content-type", "image/x-icon")
47+
io.Copy(w, f)
48+
}
49+
50+
func Assets(w http.ResponseWriter, r *http.Request) {
51+
52+
http.StripPrefix("/", http.FileServer(http.FS(distFiles))).ServeHTTP(w, r)
53+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

adminui/ui_webserver.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/NHAS/session"
18+
"github.com/NHAS/wag/adminui/frontend"
1819
"github.com/NHAS/wag/internal/config"
1920
"github.com/NHAS/wag/internal/data"
2021
"github.com/NHAS/wag/internal/router"
@@ -182,6 +183,12 @@ func New(firewall *router.Firewall, errs chan<- error) (ui *AdminUI, err error)
182183
protectedRoutes := http.NewServeMux()
183184
allRoutes := http.NewServeMux()
184185

186+
allRoutes.HandleFunc("/", frontend.Index)
187+
allRoutes.HandleFunc("GET /index.html", frontend.Index)
188+
189+
allRoutes.HandleFunc("GET /favicon.ico", frontend.Favicon)
190+
allRoutes.HandleFunc("GET /assets/", frontend.Assets)
191+
185192
allRoutes.HandleFunc("POST /api/login", adminUI.doLogin)
186193
allRoutes.HandleFunc("POST /api/refresh", adminUI.doAuthRefresh)
187194

@@ -197,7 +204,7 @@ func New(firewall *router.Firewall, errs chan<- error) (ui *AdminUI, err error)
197204
allRoutes.HandleFunc("/login/oidc/callback", adminUI.oidcCallback)
198205
}
199206

200-
allRoutes.Handle("/", adminUI.sessionManager.AuthorisationChecks(protectedRoutes,
207+
allRoutes.Handle("/api/", adminUI.sessionManager.AuthorisationChecks(protectedRoutes,
201208
func(w http.ResponseWriter, r *http.Request) {
202209
http.Error(w, "Unauthorized", http.StatusUnauthorized)
203210
},

0 commit comments

Comments
 (0)