factor out logging
This commit is contained in:
@@ -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 {
|
||||
@@ -352,18 +353,18 @@ 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"
|
||||
@@ -391,26 +392,28 @@ func Start(dir string) error {
|
||||
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[path]
|
||||
var status error = nil
|
||||
|
||||
route = basePath(route)
|
||||
|
||||
if !ok && strings.HasPrefix(path, "/archive") {
|
||||
fsPath := "." + path
|
||||
@@ -421,12 +424,11 @@ func Start(dir string) error {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
} else if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(notFoundData))
|
||||
color.Yellow("not found")
|
||||
|
||||
ui.Path(path, config.ErrRouteMiss)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -443,27 +445,26 @@ func Start(dir string) error {
|
||||
}
|
||||
|
||||
var data []byte
|
||||
var err error
|
||||
|
||||
if strings.HasSuffix(p, ".lua") {
|
||||
if strings.HasSuffix(route, ".lua") {
|
||||
var b string
|
||||
b, err = loadLua(dir, p, &cfg, req)
|
||||
b, err = loadLua(dir, route, &cfg, req)
|
||||
data = []byte(b)
|
||||
} else if strings.HasSuffix(p, ".md") {
|
||||
data, err = os.ReadFile(p)
|
||||
} 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")
|
||||
status = err
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(data)
|
||||
color.Green("ok")
|
||||
ui.Path(path, status)
|
||||
})
|
||||
|
||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
||||
|
||||
Reference in New Issue
Block a user