diff --git a/cmd/run.go b/cmd/run.go index 40cca4c..ee9c622 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -8,35 +8,19 @@ import ( "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" - "os" - "strings" + "time" ) -func viteAsset(src string) string { - file, err := os.ReadFile("dist/.vite/manifest.json") - if err != nil { - log.Fatalln(err) - } - - var viteManifestData map[string]map[string]interface{} - - if err := json.Unmarshal(file, &viteManifestData); err != nil { - log.Fatalln(err) - } - - src = strings.TrimPrefix(src, "/") - - if _, ok := viteManifestData[src]; ok { - return "/" + viteManifestData[src]["file"].(string) - } else { - return "" - } -} - // runCmd represents the run command var runCmd = &cobra.Command{ Use: "run", @@ -49,7 +33,7 @@ This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - if viper.GetBool("dev") { + if viper.GetBool("dev") && !fiber.IsChild() { go utils.RunViteServer() } @@ -59,15 +43,32 @@ to quickly create a Cobra application.`, engine.Reload(true) } - engine.AddFunc("viteAsset", viteAsset) + 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.Get("/", func(ctx *fiber.Ctx) error { diff --git a/package.json b/package.json index b6aeef4..bc937d3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "shadow", "type": "module", "scripts": { - "dev": "bun run vite build --watch --debug" + "dev": "bun run vite build --watch" }, "devDependencies": { "@types/bun": "latest", diff --git a/src/views/index.html b/src/views/index.html index 663cf91..fb88c7b 100644 --- a/src/views/index.html +++ b/src/views/index.html @@ -6,5 +6,4 @@ Hello world! - {{end}} \ No newline at end of file diff --git a/utils/template_funcs.go b/utils/template_funcs.go new file mode 100644 index 0000000..a8aa10c --- /dev/null +++ b/utils/template_funcs.go @@ -0,0 +1,29 @@ +package utils + +import ( + "github.com/goccy/go-json" + "log" + "os" + "strings" +) + +func ViteAsset(src string) string { + file, err := os.ReadFile("dist/.vite/manifest.json") + if err != nil { + log.Fatalln(err) + } + + var viteManifestData map[string]map[string]interface{} + + if err := json.Unmarshal(file, &viteManifestData); err != nil { + log.Fatalln(err) + } + + src = strings.TrimPrefix(src, "/") + + if _, ok := viteManifestData[src]; ok { + return "/" + viteManifestData[src]["file"].(string) + } else { + return "" + } +}