extract out protocol

This commit is contained in:
2026-02-14 08:26:50 -05:00
parent d340c55e8c
commit 8bfe979093
3 changed files with 109 additions and 58 deletions

View File

@@ -8,10 +8,10 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
)
var routes map[string]string
var Routes map[string]string
var Sets DeclarativeSets
func Start(dir string) {
if err := os.Chdir(dir); err != nil {
@@ -30,61 +30,9 @@ func Start(dir string) {
ui.Log("running root=%s, port=%d.", root, *config.Port)
routes := loadDirs()
Routes = loadDirs()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
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") {
err = readArchive(w, route)
} else {
w.WriteHeader(http.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})
} 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 {
data, err = os.ReadFile(route)
}
if err != nil {
http.Error(w, fmt.Sprintf("Error loading page: %v", err), http.StatusInternalServerError)
}
w.Write(data)
})
http.HandleFunc("/", httpHandler)
ui.Log("Server initialized")
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil))