77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package llms
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
const (
|
|
versionPlaceholder = "{{VERSION}}"
|
|
baseURLPlaceholder = "{{BASE_URL}}"
|
|
pageSuffix = ".md"
|
|
cacheControl = "public, max-age=300"
|
|
notBuiltNotice = "The LLM documentation was not generated for this build. Run `task build:llms`."
|
|
)
|
|
|
|
type Handler struct {
|
|
docs fs.FS
|
|
version string
|
|
baseURL string
|
|
}
|
|
|
|
func NewHandler(docs fs.FS, version string, publicURL string) *Handler {
|
|
return &Handler{
|
|
docs: docs,
|
|
version: version,
|
|
baseURL: strings.TrimRight(publicURL, "/"),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Register(r chi.Router) {
|
|
r.Get("/llms.txt", h.serve("llms.txt", "text/plain; charset=utf-8"))
|
|
r.Get("/llms-full.txt", h.serve("llms-full.txt", "text/plain; charset=utf-8"))
|
|
r.Get("/openapi.json", h.serve("openapi.json", "application/json"))
|
|
r.Get("/llms/{page}", h.page)
|
|
}
|
|
|
|
func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
|
name := chi.URLParam(r, "page")
|
|
if !strings.HasSuffix(name, pageSuffix) || strings.ContainsAny(name, "/\\") {
|
|
h.notFound(w)
|
|
return
|
|
}
|
|
h.write(w, path.Join("llms", name), "text/markdown; charset=utf-8")
|
|
}
|
|
|
|
func (h *Handler) serve(name string, contentType string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, _ *http.Request) {
|
|
h.write(w, name, contentType)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) write(w http.ResponseWriter, name string, contentType string) {
|
|
if h.docs == nil {
|
|
h.notFound(w)
|
|
return
|
|
}
|
|
raw, err := fs.ReadFile(h.docs, name)
|
|
if err != nil {
|
|
h.notFound(w)
|
|
return
|
|
}
|
|
text := strings.NewReplacer(versionPlaceholder, h.version, baseURLPlaceholder, h.baseURL).Replace(string(raw))
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("Cache-Control", cacheControl)
|
|
_, _ = w.Write([]byte(text))
|
|
}
|
|
|
|
func (h *Handler) notFound(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(notBuiltNotice))
|
|
}
|