From 7716898713b0eaa4583d553bba1d9bbcca41f936 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Mon, 28 Sep 2020 17:36:40 +0200 Subject: [PATCH] Use pkger to pack templates --- .gitignore | 3 +++ Dockerfile | 1 - go.mod | 1 + go.sum | 5 +++++ main.go | 2 ++ web/web.go | 40 +++++++++++++++++++++++++++++++++++++++- web/web_test.go | 10 ++++++++-- 7 files changed, 58 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 629adca..5b55bf8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ # Configuration file ghostream.yml +# Compiled files +pkged.go + # Test video and audio files *.ivf *.ogg diff --git a/Dockerfile b/Dockerfile index cbf5e52..2321a3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,6 @@ FROM alpine:3.12 RUN apk add ca-certificates libressl libstdc++ libgcc COPY --from=build_base /code/out/ghostream /app/ghostream COPY --from=build_base /code/web/static /app/web/static -COPY --from=build_base /code/web/template /app/web/template COPY --from=build_base /usr/local/lib64/libsrt.so.1 /lib/libsrt.so.1 WORKDIR /app # 8080 for Web and Websocket, 2112 for prometheus monitoring and 9710 for SRT diff --git a/go.mod b/go.mod index cfcef65..4fa3bbb 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.13 require ( github.com/go-ldap/ldap/v3 v3.2.3 github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a + github.com/markbates/pkger v0.17.1 github.com/pion/rtp v1.6.0 github.com/pion/webrtc/v3 v3.0.0-beta.5 github.com/prometheus/client_golang v1.7.1 diff --git a/go.sum b/go.sum index aa73b35..a71c144 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,8 @@ github.com/go-ldap/ldap/v3 v3.2.3/go.mod h1:iYS1MdmrmceOJ1QOTnRXrIs7i3kloqtmGQjR github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI= +github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -180,6 +182,8 @@ github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno= +github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/marten-seemann/qpack v0.2.0/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc= github.com/marten-seemann/qtls v0.10.0 h1:ECsuYUKalRL240rRD4Ri33ISb7kAQ3qGDlrrl55b2pc= github.com/marten-seemann/qtls v0.10.0/go.mod h1:UvMd1oaYDACI99/oZUYLzMCkBXQVT0aGm99sJhbT8hs= @@ -552,6 +556,7 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/main.go b/main.go index 0cc94d8..eb3e963 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,5 @@ +//go:generate pkger + package main import ( diff --git a/web/web.go b/web/web.go index c621580..d27830b 100644 --- a/web/web.go +++ b/web/web.go @@ -3,11 +3,14 @@ package web import ( "encoding/json" "html/template" + "io/ioutil" "log" "net/http" "os" "regexp" + "strings" + "github.com/markbates/pkger" "github.com/pion/webrtc/v3" "gitlab.crans.org/nounous/ghostream/internal/monitoring" ) @@ -29,7 +32,7 @@ var ( localSdpChan chan webrtc.SessionDescription // Preload templates - templates = template.Must(template.ParseGlob("web/template/*.html")) + templates *template.Template // Precompile regex validPath = regexp.MustCompile("^\\/[a-z0-9_-]*\\/?$") @@ -115,12 +118,47 @@ func staticHandler(w http.ResponseWriter, r *http.Request) { } } +// Load templates with pkger +// templates will be packed in the compiled binary +func loadTemplates() error { + templates = template.New("") + return pkger.Walk("/web/template", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // Skip non-templates + if info.IsDir() || !strings.HasSuffix(path, ".html") { + return nil + } + + // Open file with pkger + f, err := pkger.Open(path) + if err != nil { + return err + } + + // Read and parse template + temp, err := ioutil.ReadAll(f) + if err != nil { + return err + } + templates, err = templates.Parse(string(temp)) + return err + }) +} + // Serve HTTP server func Serve(rSdpChan chan webrtc.SessionDescription, lSdpChan chan webrtc.SessionDescription, c *Options) { remoteSdpChan = rSdpChan localSdpChan = lSdpChan cfg = c + // Load templates + if err := loadTemplates(); err != nil { + log.Fatalln("Failed to load templates:", err) + } + // Set up HTTP router and server mux := http.NewServeMux() mux.HandleFunc("/", viewerHandler) diff --git a/web/web_test.go b/web/web_test.go index 63504fb..58c1db2 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -7,10 +7,16 @@ import ( ) func TestViewerPageGET(t *testing.T) { - r, _ := http.NewRequest("GET", "", nil) + // Load templates + if err := loadTemplates(); err != nil { + t.Errorf("Failed to load templates: %v", err) + } + + // Test GET request + r, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() http.HandlerFunc(viewerHandler).ServeHTTP(w, r) if w.Code != http.StatusOK { - t.Errorf("Viewer page didn't return %v on GET", http.StatusOK) + t.Errorf("Viewer page returned %v != %v on GET", w.Code, http.StatusOK) } }