ensure all files are built the first time before running the webserver
This commit is contained in:
parent
5dcad7fa38
commit
b073b2cdb2
10 changed files with 116 additions and 56 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
60
cmd/run.go
60
cmd/run.go
|
@ -4,21 +4,13 @@ Copyright © 2024 Shane C. <shane@scaffoe.com>
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"git.shadowhosting.xyz/shadow/utils"
|
"git.shadowhosting.xyz/shadow/utils"
|
||||||
"github.com/goccy/go-json"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/earlydata"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/etag"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/healthcheck"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/helmet"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
|
||||||
"github.com/gofiber/template/html/v2"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"log"
|
"golang.org/x/sys/unix"
|
||||||
"time"
|
"os"
|
||||||
|
"os/signal"
|
||||||
)
|
)
|
||||||
|
|
||||||
// runCmd represents the run command
|
// runCmd represents the run command
|
||||||
|
@ -28,50 +20,16 @@ var runCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if viper.GetBool("dev") && !fiber.IsChild() {
|
if viper.GetBool("dev") && !fiber.IsChild() {
|
||||||
go utils.RunViteServer()
|
utils.RunViteServer()
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, unix.SIGINT, unix.SIGTERM, os.Interrupt)
|
||||||
|
<-sc
|
||||||
}
|
}
|
||||||
|
|
||||||
engine := html.New("./dist", ".html")
|
if !viper.GetBool("dev") {
|
||||||
|
utils.Webserver()
|
||||||
if viper.GetBool("dev") {
|
|
||||||
engine.Reload(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.AddFunc("viteAsset", utils.ViteAsset)
|
|
||||||
|
|
||||||
app := fiber.New(fiber.Config{
|
|
||||||
AppName: "shadow",
|
|
||||||
EnableIPValidation: true,
|
|
||||||
Views: engine,
|
|
||||||
ViewsLayout: "index",
|
|
||||||
Prefork: true,
|
|
||||||
JSONEncoder: json.Marshal,
|
|
||||||
JSONDecoder: json.Unmarshal,
|
|
||||||
})
|
|
||||||
|
|
||||||
app.Use(earlydata.New())
|
|
||||||
app.Use(healthcheck.New())
|
|
||||||
app.Use(helmet.New())
|
|
||||||
app.Use(etag.New())
|
|
||||||
app.Use(idempotency.New())
|
|
||||||
app.Use(limiter.New(limiter.Config{
|
|
||||||
Max: 175,
|
|
||||||
Expiration: 1 * time.Minute,
|
|
||||||
KeyGenerator: func(c *fiber.Ctx) string {
|
|
||||||
return c.Get("x-forwarded-for")
|
|
||||||
},
|
|
||||||
LimiterMiddleware: limiter.SlidingWindow{},
|
|
||||||
}))
|
|
||||||
|
|
||||||
app.Static("/assets", "./dist/assets")
|
|
||||||
app.Static("/", "./public")
|
|
||||||
|
|
||||||
app.Get("/", func(ctx *fiber.Ctx) error {
|
|
||||||
return ctx.Render("views/index", fiber.Map{})
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt32("server.port"))))
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"sharp": "^0.33.3",
|
||||||
|
"vite-plugin-image-optimizer": "^1.1.7",
|
||||||
"vitest": "^1.5.0"
|
"vitest": "^1.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 6.4 KiB |
|
@ -5,7 +5,7 @@
|
||||||
@layer base {
|
@layer base {
|
||||||
@font-face {
|
@font-face {
|
||||||
font-family: 'Poppins';
|
font-family: 'Poppins';
|
||||||
src: url('/Poppins-Regular.ttf')
|
src: url('../assets/Poppins-Regular.ttf')
|
||||||
}
|
}
|
||||||
html {
|
html {
|
||||||
font-family: 'Poppins';
|
font-family: 'Poppins';
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<div class="navbar-end">
|
<div class="navbar-end">
|
||||||
<div tabindex="0" role="button" class="btn btn-ghost btn-circle avatar">
|
<div tabindex="0" role="button" class="btn btn-ghost btn-circle avatar">
|
||||||
<div class="w-10 rounded-full">
|
<div class="w-10 rounded-full">
|
||||||
<img alt="User Profile Image" src="/download.png" />
|
<img alt="User Profile Image" src="{{ viteAsset `/assets/download.png` }}" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
66
utils/server.go
Normal file
66
utils/server.go
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/earlydata"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/etag"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/healthcheck"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/helmet"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||||
|
"github.com/gofiber/template/html/v2"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Webserver() {
|
||||||
|
|
||||||
|
engine := html.New("./dist", ".html")
|
||||||
|
|
||||||
|
if viper.GetBool("dev") {
|
||||||
|
engine.Reload(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.AddFunc("viteAsset", ViteAsset)
|
||||||
|
|
||||||
|
webConfig := fiber.Config{
|
||||||
|
AppName: "shadow",
|
||||||
|
EnableIPValidation: true,
|
||||||
|
Views: engine,
|
||||||
|
ViewsLayout: "index",
|
||||||
|
JSONEncoder: json.Marshal,
|
||||||
|
JSONDecoder: json.Unmarshal,
|
||||||
|
}
|
||||||
|
|
||||||
|
if !viper.GetBool("dev") {
|
||||||
|
webConfig.Prefork = true
|
||||||
|
}
|
||||||
|
|
||||||
|
app := fiber.New(webConfig)
|
||||||
|
|
||||||
|
app.Use(earlydata.New())
|
||||||
|
app.Use(healthcheck.New())
|
||||||
|
app.Use(helmet.New())
|
||||||
|
app.Use(etag.New())
|
||||||
|
app.Use(idempotency.New())
|
||||||
|
app.Use(limiter.New(limiter.Config{
|
||||||
|
Max: 175,
|
||||||
|
Expiration: 1 * time.Minute,
|
||||||
|
KeyGenerator: func(c *fiber.Ctx) string {
|
||||||
|
return c.Get("x-forwarded-for")
|
||||||
|
},
|
||||||
|
LimiterMiddleware: limiter.SlidingWindow{},
|
||||||
|
}))
|
||||||
|
|
||||||
|
app.Static("/assets", "./dist/assets")
|
||||||
|
|
||||||
|
app.Get("/", func(ctx *fiber.Ctx) error {
|
||||||
|
return ctx.Render("views/index", fiber.Map{})
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt32("server.port"))))
|
||||||
|
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -81,9 +82,31 @@ import (
|
||||||
//
|
//
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
var blueRGB = [3]int{71, 202, 255}
|
||||||
|
var purpleRGB = [3]int{189, 52, 254}
|
||||||
|
|
||||||
func RunViteServer() {
|
func RunViteServer() {
|
||||||
if viper.GetBool("dev") {
|
if viper.GetBool("dev") {
|
||||||
|
|
||||||
|
firstBuilt := false
|
||||||
|
|
||||||
|
var niceText strings.Builder
|
||||||
|
str := "Vite Dev"
|
||||||
|
steps := len(str) - 1
|
||||||
|
|
||||||
|
// Why do this? Idk. I like having separation between things on the console.
|
||||||
|
for i := 0; i < len(str); i++ {
|
||||||
|
char := str[i]
|
||||||
|
|
||||||
|
r := (blueRGB[0]*(steps-i) + purpleRGB[0]*i) / steps
|
||||||
|
g := (blueRGB[1]*(steps-i) + purpleRGB[1]*i) / steps
|
||||||
|
b := (blueRGB[2]*(steps-i) + purpleRGB[2]*i) / steps
|
||||||
|
|
||||||
|
if _, err := niceText.WriteString(fmt.Sprintf("\033[1m\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, char)); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
cmd := exec.Command("bun", "run", "dev")
|
cmd := exec.Command("bun", "run", "dev")
|
||||||
|
@ -123,7 +146,14 @@ func RunViteServer() {
|
||||||
|
|
||||||
output := scanner.Text()
|
output := scanner.Text()
|
||||||
|
|
||||||
fmt.Println("VITE (Bun) | " + output)
|
if strings.Contains(output, "built in") && !firstBuilt {
|
||||||
|
|
||||||
|
firstBuilt = true
|
||||||
|
go Webserver()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(niceText.String() + " \033[1m\033[38;5;244m|\033[22m " + output + "\033[0m")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
import { defineConfig } from 'vitest/config';
|
import { defineConfig } from 'vitest/config';
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { glob } from "glob";
|
import { glob } from "glob";
|
||||||
|
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer';
|
||||||
|
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [],
|
plugins: [
|
||||||
|
ViteImageOptimizer(),
|
||||||
|
],
|
||||||
root: "src",
|
root: "src",
|
||||||
build: {
|
build: {
|
||||||
manifest: true,
|
manifest: true,
|
||||||
outDir: path.join(__dirname, "dist"),
|
outDir: path.join(__dirname, "dist"),
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
input: glob.sync([path.resolve(__dirname, "src", "**/*.html"),path.resolve(__dirname, "src", "**/*.js"), path.resolve(__dirname, "src", "**/*.css")]),
|
input: glob.sync([path.resolve(__dirname, "src/assets", "**/*.*"), path.resolve(__dirname, "src", "**/*.html"),path.resolve(__dirname, "src", "**/*.js"), path.resolve(__dirname, "src", "**/*.css")]),
|
||||||
},
|
},
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue