Skip to content

Commit

Permalink
feat: support using external dist files (close #5531)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Nov 18, 2023
1 parent f904596 commit 6fc6751
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/conf/config.go
Expand Up @@ -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"`
Expand Down
35 changes: 29 additions & 6 deletions server/static/static.go
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 6fc6751

Please sign in to comment.