mirror of
https://github.com/aziis98/go-vite-kit.git
synced 2026-10-06 22:24:52 +00:00
Small refactor, added ViteJS debug logging and server.js for serving dynamic routes in dev mode
This commit is contained in:
Generated
+2232
-1150
File diff suppressed because it is too large
Load Diff
@@ -3,13 +3,14 @@
|
||||
"version": "1.0.0",
|
||||
"description": "Javascript frontend for this Golang server built using ViteJS",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"start": "vite preview"
|
||||
"dev": "node server.js",
|
||||
"build": "vite build"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"express": "^4.18.1",
|
||||
"sass": "^1.53.0",
|
||||
"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 (
|
||||
"log"
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var Config struct {
|
||||
var (
|
||||
Mode string
|
||||
Host string
|
||||
BaseURL string
|
||||
}
|
||||
)
|
||||
|
||||
func loadEnv(key string, defaultValue ...string) string {
|
||||
env := os.Getenv(key)
|
||||
@@ -31,7 +31,7 @@ func init() {
|
||||
// Load Config
|
||||
godotenv.Load()
|
||||
|
||||
Config.Mode = loadEnv(os.Getenv("MODE"), "development")
|
||||
Config.Host = loadEnv(os.Getenv("HOST"), ":4000")
|
||||
Config.BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000")
|
||||
Mode = loadEnv(os.Getenv("MODE"), "development")
|
||||
Host = loadEnv(os.Getenv("HOST"), ":4000")
|
||||
BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000")
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
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() {
|
||||
@@ -18,18 +22,34 @@ func main() {
|
||||
}
|
||||
|
||||
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") {
|
||||
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()
|
||||
|
||||
err := exec.Command("sh", "-c", "cd _frontend/ && npm run dev").Start()
|
||||
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))
|
||||
log.Fatal(app.Listen(config.Host))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user