factor out logging
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 {
|
||||||
@@ -352,18 +353,18 @@ 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"
|
||||||
@@ -391,26 +392,28 @@ func Start(dir string) error {
|
|||||||
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
|
path := r.URL.Path
|
||||||
p, ok := routes[path]
|
route, ok := routes[path]
|
||||||
fmt.Printf("> %s ", basePath(filepath.Base(p)))
|
var status error = nil
|
||||||
|
|
||||||
|
route = basePath(route)
|
||||||
|
|
||||||
if !ok && strings.HasPrefix(path, "/archive") {
|
if !ok && strings.HasPrefix(path, "/archive") {
|
||||||
fsPath := "." + path
|
fsPath := "." + path
|
||||||
@@ -421,12 +424,11 @@ func Start(dir string) error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if !ok {
|
||||||
|
|
||||||
if !ok {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Write([]byte(notFoundData))
|
w.Write([]byte(notFoundData))
|
||||||
color.Yellow("not found")
|
|
||||||
|
ui.Path(path, config.ErrRouteMiss)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,27 +445,26 @@ func Start(dir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
|
||||||
|
|
||||||
if strings.HasSuffix(p, ".lua") {
|
if strings.HasSuffix(route, ".lua") {
|
||||||
var b string
|
var b string
|
||||||
b, err = loadLua(dir, p, &cfg, req)
|
b, err = loadLua(dir, route, &cfg, req)
|
||||||
data = []byte(b)
|
data = []byte(b)
|
||||||
} else if strings.HasSuffix(p, ".md") {
|
} else if strings.HasSuffix(route, ".md") {
|
||||||
data, err = os.ReadFile(p)
|
data, err = os.ReadFile(route)
|
||||||
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")
|
status = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
color.Green("ok")
|
ui.Path(path, status)
|
||||||
})
|
})
|
||||||
|
|
||||||
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 = "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
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