diff --git a/context.go b/context.go new file mode 100644 index 0000000..5b8813d --- /dev/null +++ b/context.go @@ -0,0 +1,20 @@ +package tht + +import "surdeus.su/core/xgo/xmodules/httpx" +import "surdeus.su/util/tht/mdx" + +// Context key type for internal usage. +type CKey string + +const ( + KeyValues CKey = "values" +) + +type ContextValues struct { + Request *httpx.Request + Global any + Markdown *mdx.Markdown + IsIndex bool + IndexSuffix string + Ext string +} diff --git a/go.mod b/go.mod index aed22e4..f491278 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,5 @@ require ( github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 surdeus.su/core/cli v0.1.2 surdeus.su/core/xgo v0.6.1 - surdeus.su/util/tpp v0.3.2 + surdeus.su/util/tpp v0.3.3 ) diff --git a/go.sum b/go.sum index b5b770d..c84df67 100644 --- a/go.sum +++ b/go.sum @@ -12,3 +12,5 @@ surdeus.su/core/xgo v0.6.1 h1:ssF7LrTyANmIVIqO6E2TprueNDup11/NWH8dbzue4VQ= surdeus.su/core/xgo v0.6.1/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc= surdeus.su/util/tpp v0.3.2 h1:ebcnEcY+4tgB4a6trs4GBd2CJjrZJaPKh3i5RKQf8/U= surdeus.su/util/tpp v0.3.2/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw= +surdeus.su/util/tpp v0.3.3 h1:GEyOlt4M1jE9q9HSPAds9X85qEc/FUpD/M90vlROgLw= +surdeus.su/util/tpp v0.3.3/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw= diff --git a/handler.go b/handler.go index 0914f98..2ef2326 100644 --- a/handler.go +++ b/handler.go @@ -70,9 +70,13 @@ func DefaultPP(mod string) *tpp.Preprocessor { s.SetImports(xmodules.Modules) s.EnableFileImport(true) - s.Add("__http_request__", ctx.Value(KeyRequest)) - s.Add("__global__", ctx.Value(KeyGlobal)) - s.Add("__markdown__", ctx.Value(KeyMarkdown)) + vals := ctx.Value(KeyValues).(*ContextValues) + s.Add("__http_request__", vals.Request) + s.Add("__global__", vals.Global) + s.Add("__markdown__", vals.Markdown) + s.Add("__is_index__", vals.IsIndex) + s.Add("__index_suffix__", vals.IndexSuffix) + s.Add("__tpp_ext__", vals.Ext) }).SetPreCode(func(ctx context.Context) []byte { return []byte(` markdown := func(...args) { @@ -81,9 +85,14 @@ func DefaultPP(mod string) *tpp.Preprocessor { __http__ := immutable({ request : __http_request__ }) + context.http = __http__ context.pp = pp context.global = __global__ + context.is_index = __is_index__ + context.index_suffix = __index_suffix__ + context.tpp_ext = __tpp_ext__ + import("./pre")(context) `) }).SetPostCode(func(ctx context.Context) []byte { @@ -104,6 +113,7 @@ func (h *Handler) ServeHTTP( w http.ResponseWriter, r *http.Request, ) { + isIndex := false shouldProcess := true urlPath := r.URL.Path // Cleaning URL path to prevent injections. @@ -125,6 +135,7 @@ func (h *Handler) ServeHTTP( filePath, h.indexSuffix, ) + isIndex = true } } @@ -147,26 +158,22 @@ func (h *Handler) ServeHTTP( return } + ctx := context.WithValue( r.Context(), - KeyRequest, - &httpx.Request{ - Request: r, + KeyValues, + &ContextValues{ + Request: &httpx.Request{ + Request: r, + }, + Global: h.global, + Markdown: h.md, + IsIndex: isIndex, + IndexSuffix: h.indexSuffix, + Ext: h.ext, }, ) - ctx = context.WithValue( - ctx, - KeyGlobal, - h.global, - ) - - ctx = context.WithValue( - ctx, - KeyMarkdown, - h.md, - ) - // Setting before the code to let it change own // content type? fileExt := filepath.Ext(filePath) diff --git a/tool.go b/tool.go index 3203c03..3d6490d 100644 --- a/tool.go +++ b/tool.go @@ -9,15 +9,6 @@ import ( "log" ) -// Context key type for internal usage. -type CKey string - -const ( - KeyRequest CKey = "http-request" - KeyGlobal = "global" - KeyMarkdown = "markdown" - KeyHTML = "html" -) // Simple PHP-like server implementation. var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {