alpha p8
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
package new
|
package new
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -9,7 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getName() string {
|
func getName() string {
|
||||||
out, err := exec.Command("git", "config", "user.name").Output(); if err == nil {
|
out, err := exec.Command("git", "config", "user.name").Output()
|
||||||
|
if err == nil {
|
||||||
s := strings.TrimSpace(string(out))
|
s := strings.TrimSpace(string(out))
|
||||||
if s != "" {
|
if s != "" {
|
||||||
return s
|
return s
|
||||||
|
|||||||
@@ -2,11 +2,15 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
@@ -14,7 +18,7 @@ import (
|
|||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
"html/template"
|
||||||
"fes/src/config"
|
"fes/src/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,33 +121,35 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadLua(luaDir, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
|
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
|
coreFiles, err := fs.ReadDir(config.Core, "core")
|
||||||
|
if err == nil {
|
||||||
|
for _, de := range coreFiles {
|
||||||
|
if de.IsDir() || !strings.HasSuffix(de.Name(), ".lua") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
path := filepath.Join("core", de.Name())
|
||||||
|
fileData, err := config.Core.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
L.DoString(string(fileData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
preloadLuaModule := func(name, path string) {
|
preloadLuaModule := func(name, path string) {
|
||||||
L.PreloadModule(name, func(L *lua.LState) int {
|
L.PreloadModule(name, func(L *lua.LState) int {
|
||||||
data, err := config.Core.ReadFile(path)
|
fileData, err := config.Core.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tbl := L.NewTable()
|
panic(err)
|
||||||
tbl.RawSetString("error", lua.LString(err.Error()))
|
|
||||||
L.Push(tbl)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
if _, err := L.LoadString(string(data)); err != nil {
|
if err := L.DoString(string(fileData)); err != nil {
|
||||||
tbl := L.NewTable()
|
panic(err)
|
||||||
tbl.RawSetString("error", lua.LString(err.Error()))
|
|
||||||
L.Push(tbl)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
if err := L.CallByParam(lua.P{Fn: L.Get(-1), NRet: 1, Protect: true}); err != nil {
|
L.Push(L.Get(-1))
|
||||||
tbl := L.NewTable()
|
|
||||||
tbl.RawSetString("error", lua.LString(err.Error()))
|
|
||||||
L.Push(tbl)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
ret := L.Get(-1)
|
|
||||||
L.Push(ret)
|
|
||||||
return 1
|
return 1
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -154,8 +160,40 @@ func loadLua(luaDir, entry string, cfg *config.MyConfig, requestData reqData) (s
|
|||||||
|
|
||||||
L.PreloadModule("fes", func(L *lua.LState) int {
|
L.PreloadModule("fes", func(L *lua.LState) int {
|
||||||
mod := L.NewTable()
|
mod := L.NewTable()
|
||||||
|
coreModules := []string{}
|
||||||
|
if ents, err := fs.ReadDir(config.Core, "core"); err == nil {
|
||||||
|
for _, e := range ents {
|
||||||
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
coreModules = append(coreModules, strings.TrimSuffix(e.Name(), ".lua"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, modName := range coreModules {
|
||||||
|
path := filepath.Join("core", modName+".lua")
|
||||||
|
fileData, err := config.Core.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := L.DoString(string(fileData)); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val := L.Get(-1)
|
||||||
|
L.Pop(1)
|
||||||
|
tbl, ok := val.(*lua.LTable)
|
||||||
|
if !ok || tbl == nil {
|
||||||
|
tbl = L.NewTable()
|
||||||
|
}
|
||||||
|
if modName == "builtin" {
|
||||||
|
tbl.ForEach(func(k, v lua.LValue) { mod.RawSet(k, v) })
|
||||||
|
} else {
|
||||||
|
mod.RawSetString(modName, tbl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
includeDir := filepath.Join(luaDir, "include")
|
includeDir := filepath.Join(luaDir, "include")
|
||||||
mod.RawSetString("app", loadIncludeModules(L, includeDir))
|
mod.RawSetString("app", loadIncludeModules(L, includeDir))
|
||||||
|
|
||||||
if cfg != nil {
|
if cfg != nil {
|
||||||
site := L.NewTable()
|
site := L.NewTable()
|
||||||
site.RawSetString("version", lua.LString(cfg.App.Version))
|
site.RawSetString("version", lua.LString(cfg.App.Version))
|
||||||
@@ -167,6 +205,7 @@ func loadLua(luaDir, entry string, cfg *config.MyConfig, requestData reqData) (s
|
|||||||
site.RawSetString("authors", authors)
|
site.RawSetString("authors", authors)
|
||||||
mod.RawSetString("site", site)
|
mod.RawSetString("site", site)
|
||||||
}
|
}
|
||||||
|
|
||||||
bus := L.NewTable()
|
bus := L.NewTable()
|
||||||
bus.RawSetString("url", lua.LString(requestData.path))
|
bus.RawSetString("url", lua.LString(requestData.path))
|
||||||
params := L.NewTable()
|
params := L.NewTable()
|
||||||
@@ -175,10 +214,12 @@ func loadLua(luaDir, entry string, cfg *config.MyConfig, requestData reqData) (s
|
|||||||
}
|
}
|
||||||
bus.RawSetString("params", params)
|
bus.RawSetString("params", params)
|
||||||
mod.RawSetString("bus", bus)
|
mod.RawSetString("bus", bus)
|
||||||
|
|
||||||
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
|
||||||
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
|
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
|
||||||
return 1
|
return 1
|
||||||
}))
|
}))
|
||||||
|
|
||||||
L.Push(mod)
|
L.Push(mod)
|
||||||
return 1
|
return 1
|
||||||
})
|
})
|
||||||
@@ -193,10 +234,110 @@ func loadLua(luaDir, entry string, cfg *config.MyConfig, requestData reqData) (s
|
|||||||
|
|
||||||
L.SetGlobal("__fes_result", L.Get(-1))
|
L.SetGlobal("__fes_result", L.Get(-1))
|
||||||
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
if err := L.DoString("return tostring(__fes_result)"); err != nil {
|
||||||
return "", err
|
L.GetGlobal("__fes_result")
|
||||||
|
if s := L.ToString(-1); s != "" {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return L.ToString(-1), nil
|
if s := L.ToString(-1); s != "" {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
||||||
|
info, err := os.Stat(fsPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
return "", fmt.Errorf("not a directory")
|
||||||
|
}
|
||||||
|
ents, err := os.ReadDir(fsPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
type entryInfo struct {
|
||||||
|
name string
|
||||||
|
isDir bool
|
||||||
|
href string
|
||||||
|
size int64
|
||||||
|
mod time.Time
|
||||||
|
}
|
||||||
|
var list []entryInfo
|
||||||
|
for _, e := range ents {
|
||||||
|
n := e.Name()
|
||||||
|
full := filepath.Join(fsPath, n)
|
||||||
|
st, err := os.Stat(full)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
isd := st.IsDir()
|
||||||
|
displayName := n
|
||||||
|
if isd {
|
||||||
|
displayName = n + "/"
|
||||||
|
}
|
||||||
|
href := path.Join(urlPath, n)
|
||||||
|
if isd && !strings.HasSuffix(href, "/") {
|
||||||
|
href = href + "/"
|
||||||
|
}
|
||||||
|
size := int64(-1)
|
||||||
|
if !isd {
|
||||||
|
size = st.Size()
|
||||||
|
}
|
||||||
|
list = append(list, entryInfo{name: displayName, isDir: isd, href: href, size: size, mod: st.ModTime()})
|
||||||
|
}
|
||||||
|
sort.Slice(list, func(i, j int) bool {
|
||||||
|
if list[i].isDir != list[j].isDir {
|
||||||
|
return list[i].isDir
|
||||||
|
}
|
||||||
|
return strings.ToLower(list[i].name) < strings.ToLower(list[j].name)
|
||||||
|
})
|
||||||
|
|
||||||
|
urlPath = strings.TrimPrefix(urlPath, "/archive")
|
||||||
|
if urlPath == "" {
|
||||||
|
urlPath = "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("<html>\n<head><title>Index of ")
|
||||||
|
b.WriteString(template.HTMLEscapeString(urlPath))
|
||||||
|
b.WriteString("</title></head>\n<body>\n<h1>Index of ")
|
||||||
|
b.WriteString(template.HTMLEscapeString(urlPath))
|
||||||
|
b.WriteString("</h1><hr><pre>")
|
||||||
|
if urlPath != "/archive" && urlPath != "/archive/" {
|
||||||
|
up := path.Dir(urlPath)
|
||||||
|
if up == "." {
|
||||||
|
up = "/archive"
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(up, "/") {
|
||||||
|
up = up + "/"
|
||||||
|
}
|
||||||
|
b.WriteString(`<a href="` + template.HTMLEscapeString(up) + `">../</a>` + "\n")
|
||||||
|
} else {
|
||||||
|
b.WriteString(`<a href="../">../</a>` + "\n")
|
||||||
|
}
|
||||||
|
nameCol := 50
|
||||||
|
for _, ei := range list {
|
||||||
|
escapedName := template.HTMLEscapeString(ei.name)
|
||||||
|
dateStr := ei.mod.Local().Format("02-Jan-2006 15:04")
|
||||||
|
var sizeStr string
|
||||||
|
if ei.isDir {
|
||||||
|
sizeStr = "-"
|
||||||
|
} else {
|
||||||
|
sizeStr = fmt.Sprintf("%d", ei.size)
|
||||||
|
}
|
||||||
|
spaces := 1
|
||||||
|
if len(escapedName) < nameCol {
|
||||||
|
spaces = nameCol - len(escapedName)
|
||||||
|
}
|
||||||
|
line := `<a href="` + template.HTMLEscapeString(ei.href) + `">` + escapedName + `</a>` + strings.Repeat(" ", spaces) + dateStr + strings.Repeat(" ", 19-len(sizeStr)) + sizeStr + "\n"
|
||||||
|
b.WriteString(line)
|
||||||
|
}
|
||||||
|
b.WriteString("</pre><hr></body>\n</html>")
|
||||||
|
return b.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(dir string) error {
|
func Start(dir string) error {
|
||||||
@@ -259,6 +400,18 @@ func Start(dir string) error {
|
|||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
lp, ok := routes[path]
|
lp, ok := routes[path]
|
||||||
|
|
||||||
|
if !ok && strings.HasPrefix(path, "/archive") {
|
||||||
|
fsPath := "." + path
|
||||||
|
info, err := os.Stat(fsPath)
|
||||||
|
if err == nil && info.IsDir() {
|
||||||
|
if htmlStr, err := generateArchiveIndex(fsPath, path); err == nil {
|
||||||
|
w.Write([]byte(htmlStr))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Write([]byte(notFoundData))
|
w.Write([]byte(notFoundData))
|
||||||
|
|||||||
Reference in New Issue
Block a user