feat: support using external dist files (close #5531)

pull/5554/head
Andy Hsu 2023-11-18 19:56:22 +08:00
parent f904596cbc
commit 6fc6751463
2 changed files with 30 additions and 6 deletions

View File

@ -49,6 +49,7 @@ type Config struct {
Scheme Scheme `json:"scheme"`
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
BleveDir string `json:"bleve_dir" env:"BLEVE_DIR"`
DistDir string `json:"dist_dir"`
Log LogConfig `json:"log"`
DelayedStart int `json:"delayed_start" env:"DELAYED_START"`
MaxConnections int `json:"max_connections" env:"MAX_CONNECTIONS"`

View File

@ -3,25 +3,48 @@ package static
import (
"errors"
"fmt"
"github.com/alist-org/alist/v3/public"
"io"
"io/fs"
"net/http"
"os"
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/public"
"github.com/gin-gonic/gin"
)
func InitIndex() {
index, err := public.Public.ReadFile("dist/index.html")
var static fs.FS = public.Public
func initStatic() {
if conf.Conf.DistDir == "" {
dist, err := fs.Sub(static, "dist")
if err != nil {
utils.Log.Fatalf("failed to read dist dir")
}
static = dist
return
}
static = os.DirFS(conf.Conf.DistDir)
}
func initIndex() {
indexFile, err := static.Open("index.html")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
utils.Log.Fatalf("index.html not exist, you may forget to put dist of frontend to public/dist")
}
utils.Log.Fatalf("failed to read index.html: %v", err)
}
defer func() {
_ = indexFile.Close()
}()
index, err := io.ReadAll(indexFile)
if err != nil {
utils.Log.Fatalf("failed to read dist/index.html")
}
conf.RawIndexHtml = string(index)
siteConfig := getSiteConfig()
replaceMap := map[string]string{
@ -60,7 +83,8 @@ func UpdateIndex() {
}
func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
InitIndex()
initStatic()
initIndex()
folders := []string{"assets", "images", "streamer", "static"}
r.Use(func(c *gin.Context) {
for i := range folders {
@ -70,8 +94,7 @@ func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
}
})
for i, folder := range folders {
folder = "dist/" + folder
sub, err := fs.Sub(public.Public, folder)
sub, err := fs.Sub(static, folder)
if err != nil {
utils.Log.Fatalf("can't find folder: %s", folder)
}