diff --git a/src/config/config.go b/src/config/config.go
index 9f20968..e564136 100644
--- a/src/config/config.go
+++ b/src/config/config.go
@@ -1,6 +1,9 @@
package config
-import "embed"
+import (
+ "embed"
+ "errors"
+)
var Core embed.FS
var Port *int
@@ -13,3 +16,5 @@ type MyConfig struct {
Authors []string `toml:"authors"`
} `toml:"app"`
}
+
+var ErrRouteMiss = errors.New("not found")
diff --git a/src/server/server.go b/src/server/server.go
index 0eeb4a8..bab51c5 100644
--- a/src/server/server.go
+++ b/src/server/server.go
@@ -2,6 +2,7 @@ package server
import (
"fmt"
+ "html/template"
"io/fs"
"net/http"
"os"
@@ -13,13 +14,13 @@ import (
"time"
"fes/src/config"
- "github.com/fatih/color"
+ "fes/src/ui"
+
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/pelletier/go-toml/v2"
lua "github.com/yuin/gopher-lua"
- "html/template"
)
type reqData struct {
@@ -135,7 +136,7 @@ func loadIncludeModules(L *lua.LState, includeDir string) *lua.LTable {
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()
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 {
- return "", err
+ return []byte(""), err
}
if L.GetTop() == 0 {
- return "", nil
+ return []byte(""), nil
}
L.SetGlobal("__fes_result", L.Get(-1))
if err := L.DoString("return tostring(__fes_result)"); err != nil {
L.GetGlobal("__fes_result")
if s := L.ToString(-1); s != "" {
- return s, nil
+ return []byte(s), nil
}
- return "", nil
+ return []byte(""), nil
}
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) {
@@ -352,24 +353,24 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
func Start(dir string) error {
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 = "."
tomlDocument, err := os.ReadFile("Fes.toml")
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))
var cfg config.MyConfig
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.Name = "unknown"
cfg.App.Version = "unknown"
}
- notFoundData := `
+ notFoundData := []byte(`
404 Not Found
@@ -377,93 +378,85 @@ func Start(dir string) error {
fes
-`
+`)
if _, err := os.Stat(filepath.Join("www", "404.lua")); err == nil {
if nf, err := loadLua(dir, "www/404.lua", &cfg, reqData{}); err == nil {
notFoundData = nf
}
} else if _, err := os.Stat("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)
if entries, err := os.ReadDir("www"); 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 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 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) {
- path := r.URL.Path
- p, ok := routes[path]
- fmt.Printf("> %s ", basePath(filepath.Base(p)))
+ route, ok := routes[r.URL.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
- }
- }
- }
+ var err error = nil
+ defer func() {
+ ui.Path(route, err)
+ }()
if !ok {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(notFoundData))
- color.Yellow("not found")
- return
+ err = config.ErrRouteMiss
+ route = r.URL.Path
+
+ 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)
- for k, val := range r.URL.Query() {
- if len(val) > 0 {
- params[k] = val[0]
+ for k, v := range r.URL.Query() {
+ if len(v) > 0 {
+ params[k] = v[0]
}
}
- req := reqData{
- path: path,
- params: params,
- }
-
var data []byte
- var err error
-
- if strings.HasSuffix(p, ".lua") {
- var b string
- b, err = loadLua(dir, p, &cfg, req)
- data = []byte(b)
- } else if strings.HasSuffix(p, ".md") {
- data, err = os.ReadFile(p)
+ if strings.HasSuffix(route, ".lua") {
+ data, err = loadLua(dir, route, &cfg, reqData{path: r.URL.Path, params: params})
+ } else if strings.HasSuffix(route, ".md") {
+ data, err = os.ReadFile(route)
data = []byte(markdownToHTML(string(data)))
} else {
- data, err = os.ReadFile(p)
+ data, err = os.ReadFile(route)
}
if err != nil {
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
- color.Red("bad")
- return
}
w.Write(data)
- color.Green("ok")
})
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
diff --git a/src/ui/ui.go b/src/ui/ui.go
new file mode 100644
index 0000000..7d47350
--- /dev/null
+++ b/src/ui/ui.go
@@ -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)
+}