Skip to content

Commit ea0a385

Browse files
committed
fix: embed webapp on go build
1 parent 1ca2291 commit ea0a385

File tree

6 files changed

+377
-2
lines changed

6 files changed

+377
-2
lines changed

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package main
22

33
import (
4+
"embed"
5+
46
"github.com/buildtheui/DropMyFile/pkg/cmd"
7+
"github.com/buildtheui/DropMyFile/pkg/global"
58
)
69

10+
//go:embed views/*
11+
var views embed.FS
12+
713
func main() {
14+
// Set views files to global variable so files are embed at compile time
15+
global.ViewsContent = views
16+
817
// Init command line
918
cmd.Execute()
1019
}

pkg/api/api.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"net/http"
78

89
folderManager "github.com/buildtheui/DropMyFile/pkg/folder_manager"
910
"github.com/buildtheui/DropMyFile/pkg/global"
1011
"github.com/buildtheui/DropMyFile/pkg/models"
1112
"github.com/buildtheui/DropMyFile/pkg/utils"
1213
"github.com/gofiber/contrib/websocket"
1314
"github.com/gofiber/fiber/v2"
15+
"github.com/gofiber/fiber/v2/middleware/filesystem"
1416
fiberUtils "github.com/gofiber/fiber/v2/utils"
1517
"github.com/gofiber/template/html/v2"
1618
)
@@ -29,7 +31,7 @@ func setupRoutes() {
2931

3032

3133
App.Get("/", func(c *fiber.Ctx) error {
32-
return c.Render("index", fiber.Map{ "Session": global.GSession })
34+
return c.Render("views/index", fiber.Map{ "Session": global.GSession })
3335
})
3436

3537
App.Use("/ws", func(c *fiber.Ctx) error {
@@ -157,7 +159,7 @@ func setUpWebSockets() {
157159

158160
func RouterInit() *fiber.App {
159161
// Load templates
160-
var engine = html.New("./views", ".html")
162+
var engine = html.NewFileSystem(http.FS(global.ViewsContent), ".html")
161163

162164
// Create a new Fiber app
163165
App = fiber.New(fiber.Config{
@@ -171,6 +173,14 @@ func RouterInit() *fiber.App {
171173
// Only LAN users can access with the correct session printed in console
172174
App.Use(global.ValidateSession)
173175

176+
// Without `PathPrefix`, you have to access it via URL:
177+
// `http://<server>/assets/assets/asset_name_example.png`.
178+
App.Use("/assets", filesystem.New(filesystem.Config{
179+
Root: http.FS(global.ViewsContent),
180+
PathPrefix: "assets",
181+
Browse: true,
182+
}))
183+
174184
// Call setupRoutes to set up your routes
175185
setupRoutes()
176186
setUpApis()

pkg/global/global.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package global
22

33
import (
4+
"embed"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -12,6 +13,7 @@ import (
1213
"github.com/joho/godotenv"
1314
)
1415

16+
var ViewsContent embed.FS
1517
var ServerPort int
1618
var SessionLength int
1719
var GSession string

vendor/github.com/gofiber/fiber/v2/middleware/filesystem/filesystem.go

Lines changed: 287 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)