1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 10:22:19 +00:00

Use pkger to pack templates

This commit is contained in:
Alexandre Iooss 2020-09-28 17:36:40 +02:00
parent 92f44c981d
commit 7716898713
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
7 changed files with 58 additions and 4 deletions

3
.gitignore vendored
View File

@ -4,6 +4,9 @@
# Configuration file
ghostream.yml
# Compiled files
pkged.go
# Test video and audio files
*.ivf
*.ogg

View File

@ -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

1
go.mod
View File

@ -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

5
go.sum
View File

@ -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=

View File

@ -1,3 +1,5 @@
//go:generate pkger
package main
import (

View File

@ -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)

View File

@ -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)
}
}