Skip to content

Commit

Permalink
chore: prepare for user UI and also fix build dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Oct 16, 2024
1 parent 96a4206 commit 2629786
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
15 changes: 4 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ all:

ui:
cd ui/admin && \
npm install && \
touch build/client/placeholder && \
touch build/client/assets/_placeholder

touch:
mkdir -p ui/admin/build/client/assets && \
touch ui/admin/build/client/placeholder && \
touch ui/admin/build/client/assets/_placeholder
npm install

clean:
rm -rf ui/admin/build
$(MAKE) touch
rm -rf ui/user/build

# Build the project
build: touch
build:
go build -o bin/otto -v

.PHONY: ui build all touch clean
.PHONY: ui build all clean
1 change: 1 addition & 0 deletions ui/admin/.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# DO NOT REMOVE: This is used to make the go embedded FS work when ./build doesn't exist
Empty file.
Empty file removed ui/admin/build/client/placeholder
Empty file.
24 changes: 18 additions & 6 deletions ui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

//go:embed admin/build*/client* admin/build*/client*/assets*/_*
//go:embed all:admin/*build all:user/*build
var embedded embed.FS

func Handler(devPort int) http.Handler {
Expand All @@ -20,7 +20,11 @@ func Handler(devPort int) http.Handler {
rp := httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL.Scheme = "http"
r.URL.Host = fmt.Sprintf("localhost:%d", devPort)
if strings.HasPrefix(r.URL.Path, "/admin") {
r.URL.Host = fmt.Sprintf("localhost:%d", devPort)
} else {
r.URL.Host = fmt.Sprintf("localhost:%d", devPort+1)
}
},
}
return &rp
Expand All @@ -32,10 +36,18 @@ func serve(w http.ResponseWriter, r *http.Request) {
return
}

path := path.Join("admin/build/client", strings.TrimPrefix(r.URL.Path, "/admin"))
if _, err := fs.Stat(embedded, path); err == nil {
http.ServeFileFS(w, r, embedded, path)
} else {
userPath := path.Join("user/build/", r.URL.Path)
adminPath := path.Join("admin/build/client", strings.TrimPrefix(r.URL.Path, "/admin"))

if _, err := fs.Stat(embedded, userPath); err == nil {
http.ServeFileFS(w, r, embedded, userPath)
} else if r.URL.Path == "/" {
http.Redirect(w, r, "/admin", http.StatusFound)
} else if _, err := fs.Stat(embedded, adminPath); err == nil {
http.ServeFileFS(w, r, embedded, adminPath)
} else if strings.HasPrefix(r.URL.Path, "/admin") {
http.ServeFileFS(w, r, embedded, "admin/build/client/index.html")
} else {
http.ServeFileFS(w, r, embedded, "user/build/index.html")
}
}
1 change: 1 addition & 0 deletions ui/user/.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# DO NOT REMOVE: This is used to make the go embedded FS work when ./build doesn't exist

0 comments on commit 2629786

Please sign in to comment.