Skip to content

Commit d9faea6

Browse files
authored
Use go:embed to fix template mounting in binary deployment mode (#1394)
1 parent 488bb6f commit d9faea6

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

backend/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package main
33
import (
44
"fmt"
55
"html/template"
6+
"io/fs"
67
"log"
78
"net/http"
89
"os"
910
"time"
1011

12+
"embed"
13+
1114
"github.com/alextanhongpin/go-gin-starter/config"
1215
"github.com/diggerhq/digger/backend/controllers"
1316
"github.com/diggerhq/digger/backend/middleware"
@@ -22,6 +25,9 @@ import (
2225
// based on https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications
2326
var Version = "dev"
2427

28+
//go:embed templates
29+
var templates embed.FS
30+
2531
func main() {
2632

2733
initLogging()
@@ -53,8 +59,6 @@ func main() {
5359

5460
r.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
5561

56-
r.Static("/static", "./templates/static")
57-
5862
r.GET("/health", func(c *gin.Context) {
5963
c.JSON(http.StatusOK, gin.H{
6064
"build_date": cfg.GetString("build_date"),
@@ -70,7 +74,17 @@ func main() {
7074
},
7175
})
7276

73-
r.LoadHTMLGlob("templates/*.tmpl")
77+
if _, err := os.Stat("templates"); err != nil {
78+
matches, _ := fs.Glob(templates, "templates/*.tmpl")
79+
for _, match := range matches {
80+
r.LoadHTMLFiles(match)
81+
}
82+
r.StaticFS("/static", http.FS(templates))
83+
} else {
84+
r.Static("/static", "./templates/static")
85+
r.LoadHTMLGlob("templates/*.tmpl")
86+
}
87+
7488
r.GET("/", web.RedirectToLoginOrProjects)
7589

7690
r.POST("/github-app-webhook", controllers.GithubAppWebHook)

0 commit comments

Comments
 (0)