58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
//go:build embed
|
|
|
|
package static
|
|
|
|
import (
|
|
"embed"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"path"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/kaedwen/webrtc/pkg/common"
|
|
)
|
|
|
|
//go:embed dist/browser/*
|
|
var f embed.FS
|
|
|
|
type staticSourceFile struct {
|
|
target fs.File
|
|
}
|
|
|
|
func (ss *staticSourceFile) Reader() io.ReadCloser {
|
|
return ss.target
|
|
}
|
|
|
|
func (ss *staticSourceFile) Stat() (fs.FileInfo, error) {
|
|
return ss.target.Stat()
|
|
}
|
|
|
|
type staticSource struct {
|
|
base string
|
|
}
|
|
|
|
func (s staticSource) Open(p string) (common.StaticSourceFile, error) {
|
|
t, err := f.Open(path.Join(s.base, p))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &staticSourceFile{t}, nil
|
|
}
|
|
|
|
func SetupHandler(e *gin.Engine, cfg *common.Config) {
|
|
handler := common.NewStaticHandler(staticSource{"dist/browser"}, "index.html")
|
|
e.NoRoute(func(c *gin.Context) {
|
|
encoding := c.Request.Header.Get("Accept-Encoding")
|
|
|
|
p := c.Request.URL.Path
|
|
if i, err := handler.Get(p, encoding); err == nil {
|
|
c.DataFromReader(http.StatusOK, i.Size, i.Mime, i.Reader, i.ExtraHeaders)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNotFound)
|
|
})
|
|
}
|