Skip to content

Commit

Permalink
fix: embed webapp on go build
Browse files Browse the repository at this point in the history
  • Loading branch information
buildtheui committed Dec 20, 2023
1 parent 1ca2291 commit ea0a385
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 2 deletions.
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package main

import (
"embed"

"github.com/buildtheui/DropMyFile/pkg/cmd"
"github.com/buildtheui/DropMyFile/pkg/global"
)

//go:embed views/*
var views embed.FS

func main() {
// Set views files to global variable so files are embed at compile time
global.ViewsContent = views

// Init command line
cmd.Execute()
}
14 changes: 12 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"encoding/json"
"fmt"
"log"
"net/http"

folderManager "github.com/buildtheui/DropMyFile/pkg/folder_manager"
"github.com/buildtheui/DropMyFile/pkg/global"
"github.com/buildtheui/DropMyFile/pkg/models"
"github.com/buildtheui/DropMyFile/pkg/utils"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
fiberUtils "github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/template/html/v2"
)
Expand All @@ -29,7 +31,7 @@ func setupRoutes() {


App.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{ "Session": global.GSession })
return c.Render("views/index", fiber.Map{ "Session": global.GSession })
})

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

func RouterInit() *fiber.App {
// Load templates
var engine = html.New("./views", ".html")
var engine = html.NewFileSystem(http.FS(global.ViewsContent), ".html")

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

// Without `PathPrefix`, you have to access it via URL:
// `http://<server>/assets/assets/asset_name_example.png`.
App.Use("/assets", filesystem.New(filesystem.Config{
Root: http.FS(global.ViewsContent),
PathPrefix: "assets",
Browse: true,
}))

// Call setupRoutes to set up your routes
setupRoutes()
setUpApis()
Expand Down
2 changes: 2 additions & 0 deletions pkg/global/global.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package global

import (
"embed"
"fmt"
"os"
"path/filepath"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/joho/godotenv"
)

var ViewsContent embed.FS
var ServerPort int
var SessionLength int
var GSession string
Expand Down
287 changes: 287 additions & 0 deletions vendor/github.com/gofiber/fiber/v2/middleware/filesystem/filesystem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ea0a385

Please sign in to comment.