mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-04-05 08:53:52 -04:00
Maintenance: use embed for static UI, fix #134
This commit is contained in:
@@ -102,4 +102,3 @@ ENV \
|
|||||||
TZ=
|
TZ=
|
||||||
COPY --from=alpine --chown=1000 /tmp/data /updater/data/
|
COPY --from=alpine --chown=1000 /tmp/data /updater/data/
|
||||||
COPY --from=build --chown=1000 /tmp/gobuild/app /updater/app
|
COPY --from=build --chown=1000 /tmp/gobuild/app /updater/app
|
||||||
COPY --chown=1000 ui/* /updater/ui/
|
|
||||||
|
|||||||
@@ -177,9 +177,8 @@ func _main(ctx context.Context, timeNow func() time.Time) int {
|
|||||||
go healthServer.Run(ctx, wg)
|
go healthServer.Run(ctx, wg)
|
||||||
|
|
||||||
address := fmt.Sprintf("0.0.0.0:%d", p.listeningPort)
|
address := fmt.Sprintf("0.0.0.0:%d", p.listeningPort)
|
||||||
uiDir := p.dir + "/ui"
|
|
||||||
serverLogger := logger.NewChild(logging.Settings{Prefix: "http server: "})
|
serverLogger := logger.NewChild(logging.Settings{Prefix: "http server: "})
|
||||||
server := server.New(ctx, address, p.rootURL, uiDir, db, serverLogger, runner)
|
server := server.New(ctx, address, p.rootURL, db, serverLogger, runner)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go server.Run(ctx, wg)
|
go server.Run(ctx, wg)
|
||||||
notify(1, fmt.Sprintf("Launched with %d records to watch", len(records)))
|
notify(1, fmt.Sprintf("Launched with %d records to watch", len(records)))
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
|
||||||
)
|
|
||||||
|
|
||||||
func fileServer(router chi.Router, path string, root http.FileSystem) {
|
|
||||||
if path != "/" && path[len(path)-1] != '/' {
|
|
||||||
router.Get(path,
|
|
||||||
http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
|
|
||||||
path += "/"
|
|
||||||
}
|
|
||||||
path += "*"
|
|
||||||
|
|
||||||
router.Get(path, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
rctx := chi.RouteContext(r.Context())
|
|
||||||
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
|
|
||||||
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
|
|
||||||
fs.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
@@ -22,9 +23,12 @@ type handlers struct {
|
|||||||
timeNow func() time.Time
|
timeNow func() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandler(ctx context.Context, rootURL, uiDir string,
|
//go:embed ui/*
|
||||||
|
var uiFS embed.FS //nolint:gochecknoglobals
|
||||||
|
|
||||||
|
func newHandler(ctx context.Context, rootURL string,
|
||||||
db data.Database, runner update.Runner) http.Handler {
|
db data.Database, runner update.Runner) http.Handler {
|
||||||
indexTemplate := template.Must(template.ParseFiles(uiDir + "/index.html"))
|
indexTemplate := template.Must(template.ParseFS(uiFS, "ui/index.html"))
|
||||||
|
|
||||||
handlers := &handlers{
|
handlers := &handlers{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
@@ -43,8 +47,5 @@ func newHandler(ctx context.Context, rootURL, uiDir string,
|
|||||||
|
|
||||||
router.Get(rootURL+"/update", handlers.update)
|
router.Get(rootURL+"/update", handlers.update)
|
||||||
|
|
||||||
// UI file server for other paths
|
|
||||||
fileServer(router, rootURL+"/", http.Dir(uiDir))
|
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ type server struct {
|
|||||||
handler http.Handler
|
handler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, address, rootURL, uiDir string, db data.Database, logger logging.Logger,
|
func New(ctx context.Context, address, rootURL string, db data.Database, logger logging.Logger,
|
||||||
runner update.Runner) Server {
|
runner update.Runner) Server {
|
||||||
handler := newHandler(ctx, rootURL, uiDir, db, runner)
|
handler := newHandler(ctx, rootURL, db, runner)
|
||||||
return &server{
|
return &server{
|
||||||
address: address,
|
address: address,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
Reference in New Issue
Block a user