Switch to single embed fs and add vue and svelte frontends

This commit is contained in:
Lucas Schumacher 2024-06-23 12:43:28 -04:00
parent 2bcde24a2c
commit e19ff49bc4
5 changed files with 64 additions and 53 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
tmp tmp
server

View File

@ -5,13 +5,56 @@ nil:
reactNextJS/out/*: reactNextJS/app/* reactNextJS/*.js reactNextJS/*.json reactNextJS/out/*: reactNextJS/app/* reactNextJS/*.js reactNextJS/*.json
cd reactNextJS && npm install && npm run build cd reactNextJS && npm install && npm run build
nextjs: reactNextJS/out/* nextjs: reactNextJS/out/*
serverdeps: nextjs *.go reactNextJS/addToServer.go
build: serverdeps vuejs/dist/*: vuejs/src/*
cd vuejs && npm install && npm run build
vuejs: vuejs/dist/*
svelte/dist/*: svelte/src/*
cd svelte && npm install && npm run build
svelte: svelte/dist/*
cleanbuild:
rm -r tmp/build
tmp/build: cleanbuild
mkdir -p tmp/build
tmp/build/vanillaJS: tmp/build vanillaJS/index.html
mkdir tmp/build/vanillaJS && \
cp ./vanillaJS/index.html ./tmp/build/vanillaJS
tmp/build/react: tmp/build react/index.html
mkdir tmp/build/react && \
cp ./react/index.html ./tmp/build/react/
tmp/build/alpinejs: alpinejs/index.html
mkdir tmp/build/alpinejs && \
cp ./alpinejs/index.html ./tmp/build/alpinejs
tmp/build/_next: tmp/build reactNextJS/out/_next
mkdir ./tmp/build/_next && \
cp -r ./reactNextJS/out/_next ./tmp/build/
tmp/build/reactNextJS: tmp/build reactNextJS/out/*.*
mkdir ./tmp/build/reactNextJS && \
cp ./reactNextJS/out/*.* ./tmp/build/reactNextJS/
tmp/build/assets: tmp/build svelte/dist/assets vuejs/dist/assets
mkdir tmp/build/assets && \
cp svelte/dist/assets/* tmp/build/assets/ && \
cp vuejs/dist/assets/* tmp/build/assets/
tmp/build/vuejs: tmp/build vuejs/dist/*.*
mkdir ./tmp/build/vuejs && \
cp ./vuejs/dist/*.* ./tmp/build/vuejs/
tmp/build/svelte: tmp/build svelte/dist/*.*
mkdir ./tmp/build/svelte && \
cp -r ./svelte/dist/* ./tmp/build/svelte/
tmp/build/index.html: tmp/build index.html
cp index.html tmp/build/
web: tmp/build/assets tmp/build/vanillaJS tmp/build/react tmp/build/alpinejs tmp/build/_next tmp/build/reactNextJS tmp/build/assets tmp/build/vuejs tmp/build/svelte tmp/build/index.html
build: web
go build server.go go build server.go
run: serverdeps run: web
go run server.go go run server.go
air: nextjs nil air: nextjs nil web
air server.go air server.go
clean:
rm -r tmp
.PHONY: all build nextjs serverdeps run air nil .PHONY: all build clean cleanbuild nextjs vuejs svelte run air nil web

View File

@ -2,8 +2,12 @@
<body> <body>
<h1>Frontends</h1> <h1>Frontends</h1>
<ul> <ul>
<li><a href="react/index.html">React</a></li> <li><a href="vanillaJS/">JS Only</a></li>
<li><a href="vanillaJS/index.html">vanillaJS</a></li> <li><a href="react/">React</a></li>
<li><a href="reactNextJS/">React (with Next)</a></li>
<li><a href="alpinejs/">Alpine.js</a></li>
<li><a href="svelte/">Svelte</a></li>
<li><a href="vuejs/">Vue.js</a></li>
</ul> </ul>
</body> </body>
</html> </html>

View File

@ -1,28 +0,0 @@
package reactNextJS
import (
"embed"
"fmt"
"io/fs"
"net/http"
)
//go:embed "out/*"
var nextFiles embed.FS
//go:embed "out/_next"
var nextScripts embed.FS
// Call this from main once before starting the server
func AddNextStaticClient() {
scriptsSubFolder, err := fs.Sub(nextScripts, "out")
if err != nil {
panic(fmt.Sprint("Error: Could not find \"reactNextJS/out/\". Did you build the reactNextJS client?"))
}
filesSubFolder, _ := fs.Sub(nextFiles, "out")
var nextScriptsServer = http.FileServerFS(scriptsSubFolder)
var nextFilesServer = http.FileServerFS(filesSubFolder)
http.Handle("/reactNextJS/", http.StripPrefix("/reactNextJS/", nextFilesServer))
http.Handle("/_next/", nextScriptsServer)
}

View File

@ -3,32 +3,23 @@ package main
import ( import (
"embed" "embed"
"fmt" "fmt"
"frontends/reactNextJS" "io/fs"
"net/http" "net/http"
) )
const PORT = 3003 const PORT = 3003
//go:embed "vanillaJS" //go:embed "all:tmp/build"
var vanillaJS embed.FS var webfs embed.FS
//go:embed "react"
var react embed.FS
//go:embed "reactNextJS/out"
var nextStatic embed.FS
//go:embed "alpinejs"
var alpinejs embed.FS
func main() { func main() {
webfs, err := fs.Sub(webfs, "tmp/build")
if err != nil {
panic(fmt.Sprintf("Error embeding web files: %s\nTry running \"make web\"", err))
}
http.Handle("/", http.FileServerFS(webfs))
http.Handle("/vanillaJS/", http.FileServerFS(vanillaJS)) err = http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
http.Handle("/react/", http.FileServerFS(react))
http.Handle("/alpinejs/", http.FileServerFS(alpinejs))
reactNextJS.AddNextStaticClient()
err := http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
if err != nil { if err != nil {
panic(fmt.Sprintf("Error starting server: %s", err)) panic(fmt.Sprintf("Error starting server: %s", err))
} }