Merge pull request 'rich-ui' (#3) from rich-ui into main
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "embed"
|
import (
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
var Core embed.FS
|
var Core embed.FS
|
||||||
var Port *int
|
var Port *int
|
||||||
@@ -13,3 +16,5 @@ type MyConfig struct {
|
|||||||
Authors []string `toml:"authors"`
|
Authors []string `toml:"authors"`
|
||||||
} `toml:"app"`
|
} `toml:"app"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrRouteMiss = errors.New("not found")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,13 +14,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fes/src/config"
|
"fes/src/config"
|
||||||
"github.com/fatih/color"
|
"fes/src/ui"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type reqData struct {
|
type reqData struct {
|
||||||
@@ -135,7 +136,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) (string, error) {
|
func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqData) ([]byte, error) {
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
@@ -238,26 +239,26 @@ func loadLua(luaDir string, entry string, cfg *config.MyConfig, requestData reqD
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err := L.DoFile(entry); err != nil {
|
if err := L.DoFile(entry); err != nil {
|
||||||
return "", err
|
return []byte(""), err
|
||||||
}
|
}
|
||||||
|
|
||||||
if L.GetTop() == 0 {
|
if L.GetTop() == 0 {
|
||||||
return "", nil
|
return []byte(""), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
L.GetGlobal("__fes_result")
|
L.GetGlobal("__fes_result")
|
||||||
if s := L.ToString(-1); s != "" {
|
if s := L.ToString(-1); s != "" {
|
||||||
return s, nil
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return []byte(""), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if s := L.ToString(-1); s != "" {
|
if s := L.ToString(-1); s != "" {
|
||||||
return s, nil
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return []byte(""), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
||||||
@@ -352,24 +353,24 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
|
|||||||
|
|
||||||
func Start(dir string) error {
|
func Start(dir string) error {
|
||||||
if err := os.Chdir(dir); err != nil {
|
if err := os.Chdir(dir); err != nil {
|
||||||
return fmt.Errorf("failed to change directory to %s: %w", dir, err)
|
return ui.Error(fmt.Sprintf("failed to change directory to %s", dir), err)
|
||||||
}
|
}
|
||||||
dir = "."
|
dir = "."
|
||||||
|
|
||||||
tomlDocument, err := os.ReadFile("Fes.toml")
|
tomlDocument, err := os.ReadFile("Fes.toml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read Fes.toml: %w", err)
|
return ui.Error("failed to read Fes.toml", err)
|
||||||
}
|
}
|
||||||
docStr := fixMalformedToml(string(tomlDocument))
|
docStr := fixMalformedToml(string(tomlDocument))
|
||||||
var cfg config.MyConfig
|
var cfg config.MyConfig
|
||||||
if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil {
|
if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil {
|
||||||
fmt.Printf("Warning: failed to parse Fes.toml: %v\n", err)
|
ui.Warning("failed to parse Fes.toml", err)
|
||||||
cfg.App.Authors = []string{"unknown"}
|
cfg.App.Authors = []string{"unknown"}
|
||||||
cfg.App.Name = "unknown"
|
cfg.App.Name = "unknown"
|
||||||
cfg.App.Version = "unknown"
|
cfg.App.Version = "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
notFoundData := `
|
notFoundData := []byte(`
|
||||||
<html>
|
<html>
|
||||||
<head><title>404 Not Found</title></head>
|
<head><title>404 Not Found</title></head>
|
||||||
<body>
|
<body>
|
||||||
@@ -377,93 +378,85 @@ func Start(dir string) error {
|
|||||||
<hr><center>fes</center>
|
<hr><center>fes</center>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`)
|
||||||
if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
|
if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
|
||||||
if nf, err := loadLua(dir, "www/404.lua", &cfg, reqData{}); err == nil {
|
if nf, err := loadLua(dir, "www/404.lua", &cfg, reqData{}); err == nil {
|
||||||
notFoundData = nf
|
notFoundData = nf
|
||||||
}
|
}
|
||||||
} else if _, err := os.Stat("www/404.html"); err == nil {
|
} else if _, err := os.Stat("www/404.html"); err == nil {
|
||||||
if buf, err := os.ReadFile("www/404.html"); err == nil {
|
if buf, err := os.ReadFile("www/404.html"); err == nil {
|
||||||
notFoundData = string(buf)
|
notFoundData = buf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
routes := make(map[string]string)
|
routes := make(map[string]string)
|
||||||
if entries, err := os.ReadDir("www"); err == nil {
|
if entries, err := os.ReadDir("www"); err == nil {
|
||||||
if err := handleDir(entries, "www", routes, "", false); err != nil {
|
if err := handleDir(entries, "www", routes, "", false); err != nil {
|
||||||
fmt.Printf("Warning: failed to handle www directory: %v\n", err)
|
ui.Warning("failed to handle www directory", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if entries, err := os.ReadDir("static"); err == nil {
|
if entries, err := os.ReadDir("static"); err == nil {
|
||||||
if err := handleDir(entries, "static", routes, "/static", true); err != nil {
|
if err := handleDir(entries, "static", routes, "/static", true); err != nil {
|
||||||
fmt.Printf("Warning: failed to handle static directory: %v\n", err)
|
ui.Warning("failed to handle static directory", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if entries, err := os.ReadDir("archive"); err == nil {
|
if entries, err := os.ReadDir("archive"); err == nil {
|
||||||
if err := handleDir(entries, "archive", routes, "/archive", true); err != nil {
|
if err := handleDir(entries, "archive", routes, "/archive", true); err != nil {
|
||||||
fmt.Printf("Warning: failed to handle archive directory: %v\n", err)
|
ui.Warning("failed to handle archive directory", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
route, ok := routes[r.URL.Path]
|
||||||
p, ok := routes[path]
|
|
||||||
fmt.Printf("> %s ", basePath(filepath.Base(p)))
|
|
||||||
|
|
||||||
if !ok && strings.HasPrefix(path, "/archive") {
|
var err error = nil
|
||||||
fsPath := "." + path
|
defer func() {
|
||||||
info, err := os.Stat(fsPath)
|
ui.Path(route, err)
|
||||||
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)
|
err = config.ErrRouteMiss
|
||||||
w.Write([]byte(notFoundData))
|
route = r.URL.Path
|
||||||
color.Yellow("not found")
|
|
||||||
return
|
if strings.HasPrefix(route, "/archive") {
|
||||||
|
fsPath := "." + route
|
||||||
|
if info, err := os.Stat(fsPath); err == nil && info.IsDir() {
|
||||||
|
if page, err := generateArchiveIndex(fsPath, route); err == nil {
|
||||||
|
w.Write([]byte(page))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
w.Write([]byte(notFoundData))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
params := make(map[string]string)
|
params := make(map[string]string)
|
||||||
for k, val := range r.URL.Query() {
|
for k, v := range r.URL.Query() {
|
||||||
if len(val) > 0 {
|
if len(v) > 0 {
|
||||||
params[k] = val[0]
|
params[k] = v[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req := reqData{
|
|
||||||
path: path,
|
|
||||||
params: params,
|
|
||||||
}
|
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
if strings.HasSuffix(route, ".lua") {
|
||||||
|
data, err = loadLua(dir, route, &cfg, reqData{path: r.URL.Path, params: params})
|
||||||
if strings.HasSuffix(p, ".lua") {
|
} else if strings.HasSuffix(route, ".md") {
|
||||||
var b string
|
data, err = os.ReadFile(route)
|
||||||
b, err = loadLua(dir, p, &cfg, req)
|
|
||||||
data = []byte(b)
|
|
||||||
} else if strings.HasSuffix(p, ".md") {
|
|
||||||
data, err = os.ReadFile(p)
|
|
||||||
data = []byte(markdownToHTML(string(data)))
|
data = []byte(markdownToHTML(string(data)))
|
||||||
} else {
|
} else {
|
||||||
data, err = os.ReadFile(p)
|
data, err = os.ReadFile(route)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
|
||||||
color.Red("bad")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
color.Green("ok")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
||||||
|
|||||||
55
src/ui/ui.go
Normal file
55
src/ui/ui.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fes/src/config"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Path(path string, err error) {
|
||||||
|
path = strings.TrimPrefix(path, "/")
|
||||||
|
|
||||||
|
if path == "" {
|
||||||
|
path = "(null)"
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" > %s ", path)
|
||||||
|
if err == nil {
|
||||||
|
OK("ok")
|
||||||
|
return
|
||||||
|
} else if errors.Is(err, config.ErrRouteMiss) {
|
||||||
|
WARN(config.ErrRouteMiss.Error())
|
||||||
|
} else {
|
||||||
|
ERROR("bad")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Warning(msg string, err error) error {
|
||||||
|
fmt.Printf("fes: %s: %v\n", color.MagentaString("warning"), err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Error(msg string, err error) error {
|
||||||
|
fmt.Printf("fes: %s: %v\n", color.RedString("error"), err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatal(msg string, err error) error {
|
||||||
|
fmt.Printf("fes: %s: %v\n", color.RedString("fatal"), err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func OK(msg string) {
|
||||||
|
color.Green(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WARN(msg string) {
|
||||||
|
color.Magenta(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ERROR(msg string) {
|
||||||
|
color.Red(msg)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user