3
0

Implement asset serving

This commit is contained in:
Denis Arh 2022-02-21 15:46:21 +01:00
parent 7fa5e43d08
commit f66ab4550a
4 changed files with 75 additions and 0 deletions

View File

@ -1,11 +1,15 @@
package app
import (
"fmt"
"io/fs"
"net/http"
"os"
"path"
"regexp"
"strings"
"github.com/cortezaproject/corteza-server/assets"
automationRest "github.com/cortezaproject/corteza-server/automation/rest"
composeRest "github.com/cortezaproject/corteza-server/compose/rest"
"github.com/cortezaproject/corteza-server/docs"
@ -24,6 +28,40 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) {
ho = app.Opt.HTTPServer
)
func() {
// asset serving has some overlap with auth assets, web-console and webapp serving
// and might be joined with one or more of them in the later version
var (
url = options.CleanBase(ho.BaseUrl, "assets")
aPath = ho.AssetsPath
files fs.FS
err error
)
if len(aPath) > 0 {
if files, err = loadAssetsFromPath(aPath); err != nil {
// log warning but fallback to embedded assets
app.Log.Warn(
fmt.Sprintf("failed to use custom assets path (HTTP_SERVER_ASSETS_PATH=%s)", aPath),
zap.Error(err),
)
}
}
if files == nil {
aPath = "embedded"
files, err = fs.Sub(assets.Embedded, "src")
if err != nil {
// if this is off, we might as well panic
panic(err)
}
}
r.Handle(url+"/*", http.StripPrefix(url+"/", http.FileServer(http.FS(files))))
app.Log.Info("web assets mounted", zap.String("url", url), zap.String("path", aPath))
}()
func() {
if ho.WebappEnabled && ho.ApiEnabled && ho.ApiBaseUrl == ho.WebappBaseUrl {
app.Log.
@ -133,3 +171,30 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) {
r.Handle("/.well-known/openid-configuration", app.AuthService.WellKnownOpenIDConfiguration())
}()
}
func loadAssetsFromPath(path string) (assets fs.FS, err error) {
// at least favicon file should exist in the custom asset path
// otherwise we default to embedded files
const check = "favicon32x32.png"
var (
fi os.FileInfo
)
if fi, err = os.Stat(path); err != nil {
return
}
if !fi.IsDir() {
return nil, fmt.Errorf("expecting directory")
}
assets = os.DirFS(path)
if _, err = assets.Open(check); err != nil {
return nil, err
}
return
}

10
assets/embed.go Normal file
View File

@ -0,0 +1,10 @@
package assets
import (
"embed"
)
var (
//go:embed src/*
Embedded embed.FS
)

BIN
assets/src/favicon32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
assets/src/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB