From 6e495de6dd03c421392a75b44ed8258cbde49c0e Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 23 Apr 2024 19:57:11 -0400 Subject: [PATCH] Project can now be compiled into one file --- main.go | 12 +++++++++++- utils/server.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 2e47708..da0adac 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,18 @@ Copyright © 2024 NAME HERE */ package main -import "git.shadowhosting.xyz/shadow/cmd" +import ( + "embed" + "git.shadowhosting.xyz/shadow/cmd" + "git.shadowhosting.xyz/shadow/utils" +) + +//go:embed dist/* +var distFiles embed.FS func main() { + + utils.DistFiles = distFiles + cmd.Execute() } diff --git a/utils/server.go b/utils/server.go index 45bd252..09361f3 100644 --- a/utils/server.go +++ b/utils/server.go @@ -1,24 +1,45 @@ package utils import ( + "embed" "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/filesystem" "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" + "io/fs" "log" + "net/http" "time" ) +var DistFiles embed.FS + +var distFS *fs.FS + func Webserver() { - engine := html.New("./dist", ".html") + var engine *html.Engine + + if !viper.GetBool("dev") { + + subFS, err := fs.Sub(DistFiles, "dist") + if err != nil { + log.Fatalln(err) + } + distFS = &subFS + + engine = html.NewFileSystem(http.FS(*distFS), ".html") + } else { + engine = html.New("./dist", ".html") + } if viper.GetBool("dev") { engine.Reload(true) @@ -55,7 +76,15 @@ func Webserver() { LimiterMiddleware: limiter.SlidingWindow{}, })) - app.Static("/assets", "./dist/assets") + if !viper.GetBool("dev") { + app.Use("/assets", filesystem.New(filesystem.Config{ + Root: http.FS(*distFS), + PathPrefix: "assets", + })) + } else { + app.Static("/assets", "./dist/assets") + + } app.Get("/", func(ctx *fiber.Ctx) error { return ctx.Render("views/index", fiber.Map{})