Project can now be compiled into one file

This commit is contained in:
Shane C 2024-04-23 19:57:11 -04:00
parent b073b2cdb2
commit 6e495de6dd
No known key found for this signature in database
GPG key ID: 565F3A1C80F8AA9D
2 changed files with 42 additions and 3 deletions

12
main.go
View file

@ -3,8 +3,18 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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()
}

View file

@ -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{})