add gemini support

This commit is contained in:
2026-02-16 21:00:04 -05:00
parent 94818e25fe
commit 472e27b1fa
16 changed files with 465 additions and 129 deletions

View File

@@ -12,6 +12,7 @@ var Color *bool
var Static *bool
var Docker *bool
var Verbose *bool
var Proto *string
type AppConfig struct {
App struct {

196
modules/gemini/gemini.go Normal file
View File

@@ -0,0 +1,196 @@
package gemini
import (
"bufio"
"crypto/tls"
"net"
"net/url"
"strings"
"sync"
)
const (
StatusSuccess = 20
StatusTemporaryFail = 40
StatusPermanentFail = 50
StatusNotFound = 51
StatusBadRequest = 59
)
type Handler interface {
ServeGemini(ResponseWriter, *Request)
}
type HandlerFunc func(ResponseWriter, *Request)
func (f HandlerFunc) ServeGemini(w ResponseWriter, r *Request) {
f(w, r)
}
type ServeMux struct {
mu sync.RWMutex
handlers map[string]Handler
}
func NewServeMux() *ServeMux {
return &ServeMux{
handlers: make(map[string]Handler),
}
}
func (m *ServeMux) Handle(pattern string, h Handler) {
m.mu.Lock()
m.handlers[pattern] = h
m.mu.Unlock()
}
func (m *ServeMux) HandleFunc(pattern string, f func(ResponseWriter, *Request)) {
m.Handle(pattern, HandlerFunc(f))
}
func (m *ServeMux) ServeGemini(w ResponseWriter, r *Request) {
m.mu.RLock()
defer m.mu.RUnlock()
path := r.URL.Path
for pattern, handler := range m.handlers {
if strings.HasPrefix(path, pattern) {
handler.ServeGemini(w, r)
return
}
}
w.WriteHeader(StatusNotFound, "not found")
}
var DefaultServeMux = NewServeMux()
func Handle(pattern string, h Handler) {
DefaultServeMux.Handle(pattern, h)
}
func HandleFunc(pattern string, f func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, f)
}
type Request struct {
URL *url.URL
Conn net.Conn
}
type ResponseWriter interface {
Write([]byte) (int, error)
WriteHeader(status int, meta string)
}
type response struct {
conn net.Conn
wroteHeader bool
}
func (r *response) WriteHeader(status int, meta string) {
if r.wroteHeader {
return
}
r.conn.Write([]byte(
strings.TrimSpace(
strings.Join([]string{
strings.TrimSpace(
strings.Join([]string{
string(rune(status/10 + '0')),
string(rune(status%10 + '0')),
}, ""),
),
meta,
}, " "),
) + "\r\n"))
r.wroteHeader = true
}
func (r *response) Write(b []byte) (int, error) {
if !r.wroteHeader {
r.WriteHeader(StatusSuccess, "text/gemini")
}
return r.conn.Write(b)
}
type Server struct {
Addr string
Handler Handler
TLSConfig *tls.Config
}
func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
handler := s.Handler
if handler == nil {
handler = DefaultServeMux
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
cfg := s.TLSConfig
if cfg == nil {
cfg = &tls.Config{}
}
cfg.Certificates = []tls.Certificate{cert}
ln, err := tls.Listen("tcp", s.Addr, cfg)
if err != nil {
return err
}
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go s.serve(conn, handler)
}
}
func (s *Server) serve(conn net.Conn, handler Handler) {
defer conn.Close()
reader := bufio.NewReader(conn)
line, err := reader.ReadString('\n')
if err != nil {
return
}
if len(line) > 1026 {
conn.Write([]byte("59 request too long\r\n"))
return
}
line = strings.TrimSpace(line)
u, err := url.Parse(line)
if err != nil {
conn.Write([]byte("59 bad request\r\n"))
return
}
req := &Request{
URL: u,
Conn: conn,
}
rw := &response{
conn: conn,
}
handler.ServeGemini(rw, req)
}
func ListenAndServeTLS(addr, certFile, keyFile string, h Handler) error {
s := &Server{
Addr: addr,
Handler: h,
}
return s.ListenAndServeTLS(certFile, keyFile)
}

View File

@@ -59,18 +59,21 @@ func loadDirs() map[string]string {
routes := make(map[string]string)
if entries, err := os.ReadDir("www"); err == nil {
ui.LogVerbose("reading www/")
if err := handleDir(entries, "www", routes, "", false); err != nil {
ui.Warning("failed to handle www directory", err)
}
}
if entries, err := os.ReadDir("static"); err == nil {
ui.LogVerbose("reading static/")
if err := handleDir(entries, "static", routes, "/static", true); err != nil {
ui.Warning("failed to handle static directory", err)
}
}
if entries, err := os.ReadDir("archive"); err == nil {
ui.LogVerbose("reading archive/")
if err := handleDir(entries, "archive", routes, "/archive", true); err != nil {
ui.Warning("failed to handle archive directory", err)
}

67
modules/server/gemini.go Normal file
View File

@@ -0,0 +1,67 @@
package server
import (
"fes/modules/config"
"fes/modules/gemini"
"fes/modules/ui"
"os"
"strings"
)
func geminiHandler(w gemini.ResponseWriter, r *gemini.Request) {
ui.LogVerbose("Received %s", r.URL.Path)
route, ok := Routes[r.URL.Path]
var err error = nil
/* defer won't update paramaters unless we do this. */
defer func() {
ui.Path(route, err)
}()
if !ok {
err = config.ErrRouteMiss
route = r.URL.Path
if strings.HasPrefix(route, "/archive") {
w.Write([]byte("# error: not implemented"))
// err = readArchive(w, route)
} 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
}
params := make(map[string]string)
for k, v := range r.URL.Query() {
if len(v) > 0 {
params[k] = v[0]
}
}
var data []byte
if strings.HasSuffix(route, ".lua") {
data, err = render(route, reqData{path: r.URL.Path, params: params}, &Sets)
} else if strings.HasSuffix(route, ".md") {
data, err = os.ReadFile(route)
data = []byte(markdownToHTML(string(data)))
data = []byte("<style>body {max-width: 80ch;}</style>\n" + string(data))
} else {
ui.LogVerbose("serving unrecognized file")
data, err = os.ReadFile(route)
}
if err != nil {
w.WriteHeader(-1, err.Error())
}
w.Write(data)
}

View File

@@ -10,6 +10,8 @@ import (
)
func httpHandler(w http.ResponseWriter, r *http.Request) {
ui.LogVerbose("Received %s", r.URL.Path)
route, ok := Routes[r.URL.Path]
var err error = nil
@@ -53,6 +55,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
data = []byte(markdownToHTML(string(data)))
data = []byte("<style>body {max-width: 80ch;}</style>\n" + string(data))
} else {
ui.LogVerbose("serving unrecognized file")
data, err = os.ReadFile(route)
}

View File

@@ -1,23 +1,19 @@
package server
import (
"errors"
"fes/modules/config"
"io/fs"
"os"
"path/filepath"
"strings"
lua "github.com/yuin/gopher-lua"
)
/* this is the request data we pass over the bus to the application, via the fes.bus interface */
type reqData struct {
path string
params map[string]string
}
/* declarative sets in the page declaration */
type DeclarativeSets struct {
protos struct {
http bool
@@ -25,7 +21,6 @@ type DeclarativeSets struct {
}
}
/* returns a string of rendered markup */
func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]byte, error) {
L := lua.NewState()
defer L.Close()
@@ -36,10 +31,8 @@ func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]
continue
}
path := filepath.Join("lib", de.Name())
if data, err := config.Lib.ReadFile(path); err != nil {
continue
} else {
L.DoString(string(data))
if data, err := config.Lib.ReadFile(path); err == nil {
_ = L.DoString(string(data))
}
}
}
@@ -50,10 +43,11 @@ func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]
if err != nil {
panic(err)
}
if err := L.DoString(string(fileData)); err != nil {
panic(err)
}
L.Push(L.Get(-1))
return 1
})
}
@@ -63,88 +57,40 @@ func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]
preloadLuaModule("lib.util", "lib/util.lua")
L.PreloadModule("fes", func(L *lua.LState) int {
mod := L.NewTable()
libModules := []string{}
if ents, err := fs.ReadDir(config.Lib, "lib"); err == nil {
for _, e := range ents {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") {
continue
}
libModules = append(libModules, strings.TrimSuffix(e.Name(), ".lua"))
}
}
for _, modName := range libModules {
path := filepath.Join("lib", modName+".lua")
fileData, err := config.Lib.ReadFile(path)
if err != nil {
continue
}
if err := L.DoString(string(fileData)); err != nil {
continue
}
val := L.Get(-1)
L.Pop(1)
tbl, ok := val.(*lua.LTable)
if !ok || tbl == nil {
tbl = L.NewTable()
}
if modName == "fes" {
tbl.ForEach(func(k, v lua.LValue) { mod.RawSet(k, v) })
} else {
mod.RawSetString(modName, tbl)
}
fileData, err := config.Lib.ReadFile("lib/fes.lua")
if err != nil {
panic(err)
}
mod.RawSetString("app", func() *lua.LTable {
app := L.NewTable()
includeDir := "include"
if err := L.DoString(string(fileData)); err != nil {
panic(err)
}
includes, err := os.ReadDir(includeDir)
if err != nil {
return app // load no includes
}
mod := L.Get(-1)
L.Pop(1)
for _, de := range includes {
if de.IsDir() || !strings.HasSuffix(de.Name(), ".lua") {
continue
}
base := strings.TrimSuffix(de.Name(), ".lua")
path := filepath.Join(includeDir, de.Name())
if _, err := os.Stat(path); err != nil {
continue
} else if err := L.DoFile(path); err != nil {
continue
}
val := L.Get(-1)
L.Pop(1)
tbl, ok := val.(*lua.LTable)
if !ok || tbl == nil {
tbl = L.NewTable()
}
app.RawSetString(base, tbl)
}
return app
}())
tbl, ok := mod.(*lua.LTable)
if !ok {
panic("fes module did not return table")
}
bus := L.NewTable()
bus.RawSetString("url", lua.LString(requestData.path))
params := L.NewTable()
for k, v := range requestData.params {
params.RawSetString(k, lua.LString(v))
}
bus.RawSetString("params", params)
mod.RawSetString("bus", bus)
mod.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
tbl.RawSetString("bus", bus)
tbl.RawSetString("markdown_to_html", L.NewFunction(func(L *lua.LState) int {
L.Push(lua.LString(markdownToHTML(L.ToString(1))))
return 1
}))
L.Push(mod)
L.Push(tbl)
return 1
})
@@ -156,48 +102,27 @@ func render(luapath string, requestData reqData, setBuffer *DeclarativeSets) ([]
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 []byte(s), nil
}
val := L.Get(-1)
L.Pop(1)
if val == lua.LNil {
return []byte(""), nil
}
if s := L.ToString(-1); s != "" {
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal("tostring"),
NRet: 1,
Protect: true,
}, val); err != nil {
return nil, err
}
s := L.ToString(-1)
L.Pop(1)
if s != "" {
return []byte(s), nil
}
L.GetGlobal("require")
L.Push(lua.LString("std"))
L.Call(1, 1)
ret := L.Get(-1)
L.Pop(1)
tbl, ok := ret.(*lua.LTable)
if !ok {
return nil, errors.New("Could not determine protocol")
}
proto := tbl.RawGetString("proto")
protoTbl, ok := proto.(*lua.LTable)
if !ok {
return nil, errors.New("Could not determine protocol")
}
protoTbl.ForEach(func(key lua.LValue, value lua.LValue) {
if str, ok := value.(lua.LString); ok {
switch str.String() {
case "http":
setBuffer.protos.http = true
case "gemini":
setBuffer.protos.gemini = true
}
}
})
return []byte(""), nil
}

View File

@@ -2,12 +2,13 @@ package server
import (
"fes/modules/config"
"fes/modules/gemini"
"fes/modules/ui"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sync"
)
var Routes map[string]string
@@ -30,10 +31,41 @@ func Start(dir string) {
ui.Log("running root=%s, port=%d.", root, *config.Port)
ui.LogVerbose("start loading directories")
Routes = loadDirs()
http.HandleFunc("/", httpHandler)
ui.Log("Server initialized")
gemini.HandleFunc("/", geminiHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil))
var wg sync.WaitGroup
errs := make(chan error, 2)
wg.Add(2)
go func() {
defer wg.Done()
errs <- http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil)
}()
go func() {
defer wg.Done()
errs <- gemini.ListenAndServeTLS(fmt.Sprintf("0.0.0.0:%d", *config.Port-1035), "cert.pem", "key.pem", nil)
}()
ui.Log("Server initialized")
wg.Wait()
close(errs)
var collected []error
for err := range errs {
if err != nil {
collected = append(collected, err)
}
}
if len(collected) > 0 {
fmt.Printf("errors: %v\n", collected)
return
}
}