alist/server/static.go

58 lines
1.5 KiB
Go
Raw Normal View History

2021-11-13 07:53:26 +00:00
package server
import (
"github.com/Xhofe/alist/conf"
2021-11-13 07:53:26 +00:00
"github.com/Xhofe/alist/public"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"io/fs"
"io/ioutil"
"net/http"
"strings"
2021-11-13 07:53:26 +00:00
)
2021-12-20 15:56:17 +00:00
func InitIndex() {
var index fs.File
var err error
2022-02-20 07:14:18 +00:00
if !strings.Contains(conf.Conf.Assets, "/") {
2022-01-14 12:56:20 +00:00
conf.Conf.Assets = conf.DefaultConfig().Assets
2021-12-20 15:56:17 +00:00
}
2022-02-20 07:14:18 +00:00
index, err = public.Public.Open("index.html")
2021-11-15 11:06:10 +00:00
if err != nil {
2022-02-20 07:14:18 +00:00
log.Fatal(err.Error())
2021-11-15 11:06:10 +00:00
}
data, _ := ioutil.ReadAll(index)
2022-02-20 07:14:18 +00:00
cdnUrl := strings.ReplaceAll(conf.Conf.Assets, "$version", conf.WebTag)
cdnUrl = strings.TrimRight(cdnUrl, "/")
conf.RawIndexHtml = string(data)
2022-02-21 13:47:40 +00:00
if strings.Contains(conf.RawIndexHtml, "CDN_URL") {
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "/CDN_URL", cdnUrl)
conf.RawIndexHtml = strings.ReplaceAll(conf.RawIndexHtml, "assets/", cdnUrl+"/assets/")
}
2021-11-13 07:53:26 +00:00
}
func Static(r *gin.Engine) {
2021-12-20 16:32:09 +00:00
//InitIndex()
2021-11-13 07:53:26 +00:00
assets, err := fs.Sub(public.Public, "assets")
if err != nil {
log.Fatalf("can't find assets folder")
}
2021-12-25 09:07:59 +00:00
pub, err := fs.Sub(public.Public, "public")
if err != nil {
log.Fatalf("can't find public folder")
}
2021-11-13 07:53:26 +00:00
r.StaticFS("/assets/", http.FS(assets))
2021-12-25 09:07:59 +00:00
r.StaticFS("/public/", http.FS(pub))
2021-11-13 07:53:26 +00:00
r.NoRoute(func(c *gin.Context) {
c.Header("Content-Type", "text/html")
2022-02-18 10:58:02 +00:00
c.Status(200)
if strings.HasPrefix(c.Request.URL.Path, "/@manage") {
_, _ = c.Writer.WriteString(conf.ManageHtml)
} else {
_, _ = c.Writer.WriteString(conf.IndexHtml)
}
2021-11-13 07:53:26 +00:00
c.Writer.Flush()
2021-11-22 10:10:09 +00:00
c.Writer.WriteHeaderNow()
2021-11-13 07:53:26 +00:00
})
}