diff --git a/bun.lockb b/bun.lockb index cc89233..100e759 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/cmd/run.go b/cmd/run.go index 578a669..43cf50e 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -4,21 +4,13 @@ Copyright © 2024 Shane C. package cmd import ( - "fmt" "git.shadowhosting.xyz/shadow/utils" - "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/cobra" "github.com/spf13/viper" - "log" - "time" + "golang.org/x/sys/unix" + "os" + "os/signal" ) // runCmd represents the run command @@ -28,50 +20,16 @@ var runCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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") { - engine.Reload(true) + if !viper.GetBool("dev") { + utils.Webserver() } - 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")))) - }, } diff --git a/package.json b/package.json index c30fb7e..8d67554 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ "typescript": "^5.0.0" }, "dependencies": { + "sharp": "^0.33.3", + "vite-plugin-image-optimizer": "^1.1.7", "vitest": "^1.5.0" } } \ No newline at end of file diff --git a/public/Poppins-Regular.ttf b/src/assets/Poppins-Regular.ttf similarity index 100% rename from public/Poppins-Regular.ttf rename to src/assets/Poppins-Regular.ttf diff --git a/public/download.png b/src/assets/download.png similarity index 100% rename from public/download.png rename to src/assets/download.png diff --git a/src/styles/app.css b/src/styles/app.css index 3ea7ca1..9c97a68 100644 --- a/src/styles/app.css +++ b/src/styles/app.css @@ -5,7 +5,7 @@ @layer base { @font-face { font-family: 'Poppins'; - src: url('/Poppins-Regular.ttf') + src: url('../assets/Poppins-Regular.ttf') } html { font-family: 'Poppins'; diff --git a/src/views/components/navbar.html b/src/views/components/navbar.html index b6229a9..320125b 100644 --- a/src/views/components/navbar.html +++ b/src/views/components/navbar.html @@ -27,7 +27,7 @@ diff --git a/utils/server.go b/utils/server.go new file mode 100644 index 0000000..45bd252 --- /dev/null +++ b/utils/server.go @@ -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")))) + +} diff --git a/utils/vite_dev.go b/utils/vite_dev.go index 16f1aab..4c7d1a9 100644 --- a/utils/vite_dev.go +++ b/utils/vite_dev.go @@ -8,6 +8,7 @@ import ( "log" "os" "os/exec" + "strings" "sync" ) @@ -81,9 +82,31 @@ import ( // //} +var blueRGB = [3]int{71, 202, 255} +var purpleRGB = [3]int{189, 52, 254} + func RunViteServer() { 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() { cmd := exec.Command("bun", "run", "dev") @@ -123,7 +146,14 @@ func RunViteServer() { 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") } diff --git a/vite.config.ts b/vite.config.ts index e09f16b..95d622c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,15 +1,19 @@ import { defineConfig } from 'vitest/config'; import path from "path"; import { glob } from "glob"; +import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'; + export default defineConfig({ - plugins: [], + plugins: [ + ViteImageOptimizer(), + ], root: "src", build: { manifest: true, outDir: path.join(__dirname, "dist"), 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, },