add static embed fs
This commit is contained in:
1 parent
a6f8afbd7d
commit
17a912e7f1
6 files changed
+228
-47
No files matched your search
@@ -0,0 +1,56 @@
|
||||
//go:build !embed
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kaedwen/webrtc/pkg/common"
|
||||
)
|
||||
|
||||
type staticSourceFile struct {
|
||||
target *os.File
|
||||
}
|
||||
|
||||
func (ss *staticSourceFile) Reader() io.ReadCloser {
|
||||
return ss.target
|
||||
}
|
||||
|
||||
func (ss *staticSourceFile) Stat() (fs.FileInfo, error) {
|
||||
return ss.target.Stat()
|
||||
}
|
||||
|
||||
type staticDiskSource struct {
|
||||
base string
|
||||
}
|
||||
|
||||
func (s staticDiskSource) Open(p string) (common.StaticSourceFile, error) {
|
||||
t, err := os.Open(path.Join(s.base, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &staticSourceFile{t}, nil
|
||||
}
|
||||
|
||||
func SetupHandler(e *gin.Engine, cfg common.ConfigHTTP) {
|
||||
if cfg.StaticPath != nil {
|
||||
handler := common.NewStaticHandler(staticDiskSource{*cfg.StaticPath}, "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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
//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/*
|
||||
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, _ common.ConfigHTTP) {
|
||||
handler := common.NewStaticHandler(staticSource{"dist"}, "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)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user