mirror of https://github.com/aziis98/go-vite-kit
New multi page architecture
parent
4653793eea
commit
4a738260d9
@ -0,0 +1,3 @@
|
||||
MODE=development
|
||||
HOST=:4000
|
||||
BASE_URL=http://localhost:4000
|
@ -1,10 +1,8 @@
|
||||
# NodeJS
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
# Server Executable
|
||||
server
|
||||
|
||||
# Local Files
|
||||
.env
|
||||
*.local*
|
||||
*.local*
|
||||
bin/
|
||||
|
||||
.out/
|
||||
out/
|
||||
dist/
|
||||
node_modules/
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,3 +0,0 @@
|
||||
export default {
|
||||
'/': './index.html',
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
import { basename } from 'path'
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
// Routes
|
||||
import routes from './routes.js'
|
||||
|
||||
const entryPoints = Object.fromEntries(
|
||||
Object.values(routes).map(path => [basename(path, '.html'), path])
|
||||
)
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
rollupOptions: {
|
||||
input: entryPoints,
|
||||
},
|
||||
},
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': 'http://127.0.0.1:4000/',
|
||||
},
|
||||
},
|
||||
})
|
@ -0,0 +1,17 @@
|
||||
package routes
|
||||
|
||||
import "go-vite-kit/backend/database"
|
||||
|
||||
type Router struct {
|
||||
Database database.Database
|
||||
}
|
||||
|
||||
type htmlEntrypoint struct {
|
||||
Route string `json:"route"`
|
||||
Filename string `json:"filename"`
|
||||
}
|
||||
|
||||
var HtmlEntrypoints = []htmlEntrypoint{
|
||||
{"/", "./index.html"},
|
||||
// {"/u/:username", "./user.html"},
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var logger = log.New(os.Stderr, "", 0)
|
||||
|
||||
func run(command string) {
|
||||
logger.Printf(`> %s`, color.HiWhiteString(command))
|
||||
|
||||
cmd := exec.Command("sh", "-c", command)
|
||||
cmdOutReader, _ := cmd.StdoutPipe()
|
||||
cmdErrReader, _ := cmd.StderrPipe()
|
||||
|
||||
s := bufio.NewScanner(io.MultiReader(cmdOutReader, cmdErrReader))
|
||||
if err := cmd.Start(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
for s.Scan() {
|
||||
logger.Printf(" %s", color.WhiteString(s.Text()))
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
logger.Println()
|
||||
}
|
||||
|
||||
func main() {
|
||||
run(`npm run build`)
|
||||
run(`go build -v -o ./out/server ./cmd/server`)
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"go-vite-kit/backend/routes"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(routes.HtmlEntrypoints); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
|
||||
"go-vite-kit/backend/config"
|
||||
"go-vite-kit/backend/database"
|
||||
"go-vite-kit/backend/routes"
|
||||
)
|
||||
|
||||
const FrontendOutDir = "./out/frontend"
|
||||
|
||||
func main() {
|
||||
db := database.NewInMemoryDB()
|
||||
|
||||
router := &routes.Router{
|
||||
Database: db,
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(logger.New())
|
||||
app.Use(recover.New())
|
||||
|
||||
app.Static("/", FrontendOutDir)
|
||||
for _, entrypoint := range routes.HtmlEntrypoints {
|
||||
app.Get(entrypoint.Route, func(c *fiber.Ctx) error {
|
||||
return c.SendFile(path.Join(FrontendOutDir, entrypoint.Filename))
|
||||
})
|
||||
}
|
||||
|
||||
app.Route("/api", router.Api)
|
||||
|
||||
if strings.HasPrefix(config.Mode, "dev") {
|
||||
app.Get("/dev/routes", func(c *fiber.Ctx) error {
|
||||
return c.JSON(routes.HtmlEntrypoints)
|
||||
})
|
||||
|
||||
setupDevServer()
|
||||
}
|
||||
|
||||
log.Fatal(app.Listen(config.Host))
|
||||
}
|
||||
|
||||
func setupDevServer() {
|
||||
log.Printf(`Running dev server for frontend: "npm run dev"`)
|
||||
cmd := exec.Command("sh", "-c", "npm run dev")
|
||||
cmdStdout, _ := cmd.StdoutPipe()
|
||||
cmdStderr, _ := cmd.StderrPipe()
|
||||
|
||||
viteLogger := log.New(os.Stderr, color.HiGreenString("[ViteJS]")+" ", log.Ltime|log.Lmsgprefix)
|
||||
|
||||
go func() {
|
||||
s := bufio.NewScanner(io.MultiReader(cmdStdout, cmdStderr))
|
||||
for s.Scan() {
|
||||
viteLogger.Print(s.Text())
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
viteLogger.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os/exec"
|
||||
"server/config"
|
||||
"server/database"
|
||||
"server/routes"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := database.NewInMemoryDB()
|
||||
|
||||
router := &routes.Router{
|
||||
Database: db,
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(logger.New())
|
||||
app.Use(recover.New())
|
||||
|
||||
app.Static("/", "/_frontend/dist")
|
||||
|
||||
app.Route("/api", router.Api)
|
||||
|
||||
if strings.HasPrefix(config.Mode, "dev") {
|
||||
log.Printf(`Running dev server for frontend: "npm run dev"`)
|
||||
cmd := exec.Command("sh", "-c", "cd _frontend/ && npm run dev")
|
||||
cmdStdout, _ := cmd.StdoutPipe()
|
||||
|
||||
go func() {
|
||||
s := bufio.NewScanner(cmdStdout)
|
||||
for s.Scan() {
|
||||
log.Printf("[ViteJS] %s", s.Text())
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Fatal(app.Listen(config.Host))
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
import { spawn } from 'child_process'
|
||||
import axios from 'axios'
|
||||
|
||||
function transformRoutes(entrypoints) {
|
||||
return Object.fromEntries(entrypoints.map(({ route, filename }) => [route, filename]))
|
||||
}
|
||||
|
||||
export async function getDevelopmentRoutes() {
|
||||
const res = await axios.get('http://127.0.0.1:4000/dev/routes')
|
||||
return transformRoutes(res.data)
|
||||
}
|
||||
|
||||
export async function getBuildRoutes() {
|
||||
// Thanks to ChatGPT
|
||||
function readCommandOutputAsJSON(command) {
|
||||
const [cmd, ...args] = command.split(' ')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(cmd, args)
|
||||
|
||||
let stdout = ''
|
||||
|
||||
child.stdout.on('data', data => {
|
||||
stdout += data.toString()
|
||||
})
|
||||
|
||||
child.on('close', code => {
|
||||
if (code !== 0) {
|
||||
reject(`Command ${cmd} ${args.join(' ')} failed with code ${code}`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const output = JSON.parse(stdout)
|
||||
resolve(output)
|
||||
} catch (e) {
|
||||
reject(`Error parsing JSON output: ${e.message}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return transformRoutes(await readCommandOutputAsJSON('go run ./cmd/routes'))
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package routes
|
||||
|
||||
import "server/database"
|
||||
|
||||
type Router struct {
|
||||
Database database.Database
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { getBuildRoutes, getDevelopmentRoutes } from './meta/routes.js'
|
||||
|
||||
import { join } from 'path'
|
||||
|
||||
export default defineConfig(async config => {
|
||||
const routes = config.command === 'build' ? await getBuildRoutes() : await getDevelopmentRoutes()
|
||||
const entryPoints = Object.values(routes)
|
||||
console.log('Found entrypoints:', entryPoints)
|
||||
|
||||
return {
|
||||
root: './frontend',
|
||||
build: {
|
||||
outDir: '../out/frontend',
|
||||
emptyOutDir: true,
|
||||
rollupOptions: {
|
||||
input: entryPoints.map(e => join('./frontend', e)),
|
||||
},
|
||||
},
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': 'http://127.0.0.1:4000/',
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue