94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
const PORT = 3003
|
|
|
|
//go:embed "all:vuejs/dist"
|
|
var vueFiles embed.FS
|
|
|
|
//go:embed "all:reactNextJS/out"
|
|
var nextFiles embed.FS
|
|
|
|
//go:embed "all:svelte/dist"
|
|
var svelteFiles embed.FS
|
|
|
|
//go:embed "vanillaJS"
|
|
var vanillaFiles embed.FS
|
|
|
|
//go:embed "alpinejs"
|
|
var alpineFiles embed.FS
|
|
|
|
//go:embed "react"
|
|
var reactFiles embed.FS
|
|
|
|
//go:embed "all:reactNative/dist"
|
|
var reactNativeFiles embed.FS
|
|
|
|
type frontendInfo struct {
|
|
Name, Mountpoint string
|
|
Filesystem fs.FS
|
|
}
|
|
|
|
var frontends = []frontendInfo{
|
|
{"JS Only", "/vanillaJS/", getSubFS(vanillaFiles, "vanillaJS")},
|
|
{"Alpine.js", "/alpinejs/", getSubFS(alpineFiles, "alpinejs")},
|
|
{"React", "/react/", getSubFS(reactFiles, "react")},
|
|
{"React (Next)", "/reactNextJS/", getSubFS(nextFiles, "reactNextJS/out")},
|
|
{"React Native", "/reactNative/", getSubFS(reactNativeFiles, "reactNative/dist")},
|
|
{"Vue.js", "/vuejs/", getSubFS(vueFiles, "vuejs/dist")},
|
|
{"Svelte", "/svelte/", getSubFS(svelteFiles, "svelte/dist")},
|
|
}
|
|
|
|
func getSubFS(f fs.FS, dir string) fs.FS {
|
|
files, err := fs.Sub(f, dir)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return files
|
|
}
|
|
|
|
func outputIndexAndExit(indexTemplate template.Template, frontends []frontendInfo) {
|
|
file, err := os.OpenFile("index.html", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
panic(err) // Handle error
|
|
}
|
|
indexTemplate.Execute(file, frontends)
|
|
file.Close()
|
|
os.Exit(0)
|
|
}
|
|
|
|
func main() {
|
|
args := os.Args
|
|
|
|
home_template, err := template.ParseFiles("index.tmpl")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Error parsing homepage template\n%s", err))
|
|
}
|
|
|
|
if len(args) == 2 && args[1] == "generate" {
|
|
outputIndexAndExit(*home_template, frontends)
|
|
}
|
|
|
|
for _, info := range frontends {
|
|
fileServer := http.FileServerFS(info.Filesystem)
|
|
http.Handle(info.Mountpoint, http.StripPrefix(info.Mountpoint, fileServer))
|
|
}
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
err = home_template.Execute(w, frontends)
|
|
})
|
|
|
|
err = http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Error starting server: %s", err))
|
|
}
|
|
}
|