Small refactor, added ViteJS debug logging and server.js for serving dynamic routes in dev mode

main
Antonio De Lucreziis 2 years ago
parent 7b9204db4a
commit 2d1ecedcf0

File diff suppressed because it is too large Load Diff

@ -3,13 +3,14 @@
"version": "1.0.0", "version": "1.0.0",
"description": "Javascript frontend for this Golang server built using ViteJS", "description": "Javascript frontend for this Golang server built using ViteJS",
"main": "index.js", "main": "index.js",
"type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "node server.js",
"build": "vite build", "build": "vite build"
"start": "vite preview"
}, },
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"express": "^4.18.1",
"sass": "^1.53.0", "sass": "^1.53.0",
"vite": "^2.9.13" "vite": "^2.9.13"
} }

@ -0,0 +1,34 @@
import { dirname, resolve } from 'path'
import express from 'express'
import { createServer as createViteServer } from 'vite'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
async function createServer(customHtmlRoutes) {
const app = express()
// In middleware mode, if you want to use Vite's own HTML serving logic
// use `'html'` as the `middlewareMode` (ref https://vitejs.dev/config/#server-middlewaremode)
const vite = await createViteServer({
server: { middlewareMode: 'html' },
})
for (const [route, file] of Object.entries(customHtmlRoutes)) {
app.get(route, (req, res) => {
const filePath = resolve(__dirname, file)
console.log(`Custom Route: %s`, req.url)
return res.sendFile(filePath)
})
}
app.use(vite.middlewares)
app.listen(3000)
}
createServer({
// Useful for developing with dynamic routes
'/': './index.html',
})

@ -1,4 +1,4 @@
package main package config
import ( import (
"log" "log"
@ -7,11 +7,11 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
var Config struct { var (
Mode string Mode string
Host string Host string
BaseURL string BaseURL string
} )
func loadEnv(key string, defaultValue ...string) string { func loadEnv(key string, defaultValue ...string) string {
env := os.Getenv(key) env := os.Getenv(key)
@ -31,7 +31,7 @@ func init() {
// Load Config // Load Config
godotenv.Load() godotenv.Load()
Config.Mode = loadEnv(os.Getenv("MODE"), "development") Mode = loadEnv(os.Getenv("MODE"), "development")
Config.Host = loadEnv(os.Getenv("HOST"), ":4000") Host = loadEnv(os.Getenv("HOST"), ":4000")
Config.BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000") BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000")
} }

@ -1,13 +1,17 @@
package main package main
import ( import (
"bufio"
"log" "log"
"os/exec" "os/exec"
"server/config"
"server/database" "server/database"
"server/routes" "server/routes"
"strings" "strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
) )
func main() { func main() {
@ -18,18 +22,34 @@ func main() {
} }
app := fiber.New() app := fiber.New()
app.Use(logger.New())
app.Use(recover.New())
app.Static("/", "/_frontend/dist") app.Static("/", "/_frontend/dist")
app.Route("/api", router.Api) app.Route("/api", router.Api)
if strings.HasPrefix(Config.Mode, "dev") { if strings.HasPrefix(config.Mode, "dev") {
log.Printf(`Running dev server for frontend: "npm run 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 := exec.Command("sh", "-c", "cd _frontend/ && npm run dev").Start() err := cmd.Start()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
log.Fatal(app.Listen(Config.Host)) log.Fatal(app.Listen(config.Host))
} }

Loading…
Cancel
Save