mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 22:24:52 +01:00
embed static files
This commit is contained in:
@@ -3,6 +3,7 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -21,12 +22,14 @@ const (
|
|||||||
|
|
||||||
// HomeController : HomeController struct
|
// HomeController : HomeController struct
|
||||||
type HomeController struct {
|
type HomeController struct {
|
||||||
svc *service.LndhubService
|
svc *service.LndhubService
|
||||||
|
html string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHomeController(svc *service.LndhubService) *HomeController {
|
func NewHomeController(svc *service.LndhubService, html string) *HomeController {
|
||||||
return &HomeController{
|
return &HomeController{
|
||||||
svc: svc,
|
svc: svc,
|
||||||
|
html: html,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +60,6 @@ func Max(x, y int) int {
|
|||||||
}
|
}
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
func (controller *HomeController) QR(c echo.Context) error {
|
func (controller *HomeController) QR(c echo.Context) error {
|
||||||
customPath := strings.Replace(c.Request().URL.Path, "/qr", "", 1)
|
customPath := strings.Replace(c.Request().URL.Path, "/qr", "", 1)
|
||||||
encoded := url.QueryEscape(fmt.Sprintf("%s://%s%s", c.Request().URL.Scheme, c.Request().Host, customPath))
|
encoded := url.QueryEscape(fmt.Sprintf("%s://%s%s", c.Request().URL.Scheme, c.Request().Host, customPath))
|
||||||
@@ -78,7 +80,8 @@ func (controller *HomeController) Home(c echo.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tmpl, err := template.ParseFiles("templates/index.html")
|
|
||||||
|
tmpl, err := template.New("index").Parse(controller.html)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
15
main.go
15
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -31,6 +32,12 @@ import (
|
|||||||
"github.com/ziflex/lecho/v3"
|
"github.com/ziflex/lecho/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/index.html
|
||||||
|
var html string
|
||||||
|
|
||||||
|
//go:embed static/*
|
||||||
|
var staticContent embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := &service.Config{}
|
c := &service.Config{}
|
||||||
|
|
||||||
@@ -140,12 +147,16 @@ func main() {
|
|||||||
|
|
||||||
// These endpoints are currently not supported and we return a blank response for backwards compatibility
|
// These endpoints are currently not supported and we return a blank response for backwards compatibility
|
||||||
blankController := controllers.NewBlankController(svc)
|
blankController := controllers.NewBlankController(svc)
|
||||||
homeController := controllers.NewHomeController(svc)
|
|
||||||
secured.GET("/getbtc", blankController.GetBtc)
|
secured.GET("/getbtc", blankController.GetBtc)
|
||||||
secured.GET("/getpending", blankController.GetPending)
|
secured.GET("/getpending", blankController.GetPending)
|
||||||
|
|
||||||
|
//Index page endpoints, no Authorization required
|
||||||
|
homeController := controllers.NewHomeController(svc, html)
|
||||||
e.GET("/", homeController.Home)
|
e.GET("/", homeController.Home)
|
||||||
e.GET("/qr", homeController.QR)
|
e.GET("/qr", homeController.QR)
|
||||||
e.Static("/static", "static")
|
//workaround, just adding /static would make a request to these resources hit the authorized group
|
||||||
|
e.GET("/static/css/*", echo.WrapHandler(http.FileServer(http.FS(staticContent))))
|
||||||
|
e.GET("/static/img/*", echo.WrapHandler(http.FileServer(http.FS(staticContent))))
|
||||||
|
|
||||||
// Subscribe to LND invoice updates in the background
|
// Subscribe to LND invoice updates in the background
|
||||||
go svc.InvoiceUpdateSubscription(context.Background())
|
go svc.InvoiceUpdateSubscription(context.Background())
|
||||||
|
|||||||
Reference in New Issue
Block a user