This commit is contained in:
2026-02-24 15:59:26 -05:00
parent ebd14a1b36
commit 1dd76cf1d2
24 changed files with 338 additions and 227 deletions

View File

@@ -1,8 +1,9 @@
package server
import (
"errors"
"fmt"
"net/http"
"io"
"os"
"path"
"path/filepath"
@@ -95,15 +96,22 @@ func generateArchiveIndex(fsPath string, urlPath string) (string, error) {
}
/* helper to read the archive files */
func readArchive(w http.ResponseWriter, route string) error {
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 nil
} else {
return err
func readArchive(w io.Writer, route string, protcol Protocols) error {
switch protcol {
case HTTP:
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 nil
} else {
return err
}
}
case GEMINI:
panic(errors.New("TODO"))
default:
panic(errors.New("Invalid Protocol"))
}
return nil
}

View File

@@ -25,17 +25,9 @@ func geminiHandler(w gemini.ResponseWriter, r *gemini.Request) {
route = r.URL.Path
if strings.HasPrefix(route, "/archive") {
w.Write([]byte("# error: not implemented"))
// err = readArchive(w, route)
err = readArchive(w, route, GEMINI)
} else {
w.WriteHeader(gemini.StatusNotFound, "StatusNotFound")
w.Write([]byte(`<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>fes</center>
</body>
</html>`))
}
return
}
@@ -49,7 +41,7 @@ func geminiHandler(w gemini.ResponseWriter, r *gemini.Request) {
var data []byte
if strings.HasSuffix(route, ".lua") {
data, err = render(route, reqData{path: r.URL.Path, params: params}, &Sets)
data, err = render(route, reqData{path: r.URL.Path, params: params}, GEMINI)
} else if strings.HasSuffix(route, ".md") {
data, err = os.ReadFile(route)
data = []byte(markdownToHTML(string(data)))

View File

@@ -26,7 +26,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
route = r.URL.Path
if strings.HasPrefix(route, "/archive") {
err = readArchive(w, route)
err = readArchive(w, route, HTTP)
} else {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`<html>
@@ -49,7 +49,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
var data []byte
if strings.HasSuffix(route, ".lua") {
data, err = render(route, reqData{path: r.URL.Path, params: params}, &Sets)
data, err = render(route, reqData{path: r.URL.Path, params: params}, HTTP)
} else if strings.HasSuffix(route, ".md") {
data, err = os.ReadFile(route)
data = []byte(markdownToHTML(string(data)))

View File

@@ -14,14 +14,7 @@ type reqData struct {
params map[string]string
}
type DeclarativeSets struct {
protos struct {
http bool
gemini bool
}
}
func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]byte, error) {
func render(luapath string, requestData reqData, protocol Protocols) ([]byte, error) {
L := lua.NewState()
defer L.Close()
@@ -74,6 +67,36 @@ func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]
panic("fes module did not return table")
}
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal("require"),
NRet: 1,
Protect: true,
}, lua.LString("lib.std")); err != nil {
panic(err)
}
stdMod := L.Get(-1)
L.Pop(1)
stdTbl, ok := stdMod.(*lua.LTable)
if !ok {
panic("lib.std did not return table")
}
proto := func() string {
switch protocol {
case HTTP:
return "http"
case GEMINI:
return "gemini"
default:
return "http"
}
}()
stdTbl.RawSetString("proto", lua.LString(proto))
tbl.RawSetString("std", stdTbl)
bus := L.NewTable()
bus.RawSetString("url", lua.LString(requestData.path))

View File

@@ -11,8 +11,14 @@ import (
"sync"
)
type Protocols int
const (
HTTP Protocols = iota
GEMINI
)
var Routes map[string]string
var Sets DeclarativeSets
func Start(dir string) {
if err := os.Chdir(dir); err != nil {