Add preforking, middlewares, organize some code
This commit is contained in:
parent
23b284f993
commit
de7e426598
4 changed files with 56 additions and 27 deletions
51
cmd/run.go
51
cmd/run.go
|
@ -8,35 +8,19 @@ import (
|
||||||
"git.shadowhosting.xyz/shadow/utils"
|
"git.shadowhosting.xyz/shadow/utils"
|
||||||
"github.com/goccy/go-json"
|
"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/gofiber/template/html/v2"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"time"
|
||||||
"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 ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// runCmd represents the run command
|
// runCmd represents the run command
|
||||||
var runCmd = &cobra.Command{
|
var runCmd = &cobra.Command{
|
||||||
Use: "run",
|
Use: "run",
|
||||||
|
@ -49,7 +33,7 @@ This application is a tool to generate the needed files
|
||||||
to quickly create a Cobra application.`,
|
to quickly create a Cobra application.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if viper.GetBool("dev") {
|
if viper.GetBool("dev") && !fiber.IsChild() {
|
||||||
go utils.RunViteServer()
|
go utils.RunViteServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,15 +43,32 @@ to quickly create a Cobra application.`,
|
||||||
engine.Reload(true)
|
engine.Reload(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.AddFunc("viteAsset", viteAsset)
|
engine.AddFunc("viteAsset", utils.ViteAsset)
|
||||||
|
|
||||||
app := fiber.New(fiber.Config{
|
app := fiber.New(fiber.Config{
|
||||||
AppName: "shadow",
|
AppName: "shadow",
|
||||||
EnableIPValidation: true,
|
EnableIPValidation: true,
|
||||||
Views: engine,
|
Views: engine,
|
||||||
ViewsLayout: "index",
|
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("/assets", "./dist/assets")
|
||||||
|
|
||||||
app.Get("/", func(ctx *fiber.Ctx) error {
|
app.Get("/", func(ctx *fiber.Ctx) error {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"name": "shadow",
|
"name": "shadow",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun run vite build --watch --debug"
|
"dev": "bun run vite build --watch"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
|
|
|
@ -6,5 +6,4 @@
|
||||||
Hello world!
|
Hello world!
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
29
utils/template_funcs.go
Normal file
29
utils/template_funcs.go
Normal file
|
@ -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 ""
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue