Skip to content

Commit

Permalink
Move admin ui into embedded package, update ui routes to work with em…
Browse files Browse the repository at this point in the history
…bedded content, update makefile
  • Loading branch information
NHAS committed Nov 23, 2024
1 parent c93b852 commit 4389b5a
Show file tree
Hide file tree
Showing 73 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ docker:
sudo docker run -u "$(ID):$(GID)" --rm -t -v `pwd`:/wag wag_builder

.build_ui:
cd adminui/src; npm install; gulp build
cd adminui/frontend; npm run build
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions adminui/frontend/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package frontend

import (
"embed"
"io"
"io/fs"
"net/http"
)

var (
//go:embed dist/assets/*
adminResources embed.FS

//go:embed dist/index.html dist/favicon.ico
index embed.FS

distFiles = must(fs.Sub(adminResources, "dist"))
)

func must(f fs.FS, err error) fs.FS {
if err != nil {
panic(err)
}

return f
}

func Index(w http.ResponseWriter, r *http.Request) {

f, err := index.Open("dist/index.html")
if err != nil {
panic(err)
}

w.Header().Set("content-type", "text/html; charset=utf-8")
io.Copy(w, f)
}

func Favicon(w http.ResponseWriter, r *http.Request) {

f, err := index.Open("dist/favicon.ico")
if err != nil {
panic(err)
}

w.Header().Set("content-type", "image/x-icon")
io.Copy(w, f)
}

func Assets(w http.ResponseWriter, r *http.Request) {

http.StripPrefix("/", http.FileServer(http.FS(distFiles))).ServeHTTP(w, r)
}
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.
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.
9 changes: 8 additions & 1 deletion adminui/ui_webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/NHAS/session"
"github.com/NHAS/wag/adminui/frontend"
"github.com/NHAS/wag/internal/config"
"github.com/NHAS/wag/internal/data"
"github.com/NHAS/wag/internal/router"
Expand Down Expand Up @@ -182,6 +183,12 @@ func New(firewall *router.Firewall, errs chan<- error) (ui *AdminUI, err error)
protectedRoutes := http.NewServeMux()
allRoutes := http.NewServeMux()

allRoutes.HandleFunc("/", frontend.Index)
allRoutes.HandleFunc("GET /index.html", frontend.Index)

allRoutes.HandleFunc("GET /favicon.ico", frontend.Favicon)
allRoutes.HandleFunc("GET /assets/", frontend.Assets)

allRoutes.HandleFunc("POST /api/login", adminUI.doLogin)
allRoutes.HandleFunc("POST /api/refresh", adminUI.doAuthRefresh)

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

allRoutes.Handle("/", adminUI.sessionManager.AuthorisationChecks(protectedRoutes,
allRoutes.Handle("/api/", adminUI.sessionManager.AuthorisationChecks(protectedRoutes,
func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
},
Expand Down

0 comments on commit 4389b5a

Please sign in to comment.