Add a simple go server to serve the frontends
This commit is contained in:
parent
c57fc11c5e
commit
58e435a7bd
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
tmp
|
||||||
17
Makefile
Normal file
17
Makefile
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
all: build
|
||||||
|
|
||||||
|
nil:
|
||||||
|
|
||||||
|
reactNextJS/out/*: reactNextJS/app/* reactNextJS/*.js reactNextJS/*.json
|
||||||
|
cd reactNextJS && npm run build
|
||||||
|
nextjs: reactNextJS/out/*
|
||||||
|
serverdeps: nextjs *.go reactNextJS/addToServer.go
|
||||||
|
|
||||||
|
build: serverdeps
|
||||||
|
go build server.go
|
||||||
|
run: serverdeps
|
||||||
|
go run server.go
|
||||||
|
air: nextjs nil
|
||||||
|
air server.go
|
||||||
|
|
||||||
|
.PHONY: all build nextjs serverdeps run air nil
|
||||||
28
reactNextJS/addToServer.go
Normal file
28
reactNextJS/addToServer.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
31
server.go
Normal file
31
server.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
"frontends/reactNextJS"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PORT = 3003
|
||||||
|
|
||||||
|
//go:embed "vanillaJS"
|
||||||
|
var vanillaJS embed.FS
|
||||||
|
|
||||||
|
//go:embed "react"
|
||||||
|
var react embed.FS
|
||||||
|
|
||||||
|
//go:embed "reactNextJS/out"
|
||||||
|
var nextStatic embed.FS
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
http.Handle("/vanillaJS/", http.FileServerFS(vanillaJS))
|
||||||
|
http.Handle("/react/", http.FileServerFS(react))
|
||||||
|
reactNextJS.AddNextStaticClient()
|
||||||
|
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Error starting server: %s", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user