feat: added more sufficient way to add values to the context via the struct (obviosu, damn).
This commit is contained in:
parent
a960290dec
commit
c9e78e8b7e
5 changed files with 48 additions and 28 deletions
20
context.go
Normal file
20
context.go
Normal file
|
@ -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
|
||||||
|
}
|
2
go.mod
2
go.mod
|
@ -7,5 +7,5 @@ require (
|
||||||
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
|
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
|
||||||
surdeus.su/core/cli v0.1.2
|
surdeus.su/core/cli v0.1.2
|
||||||
surdeus.su/core/xgo v0.6.1
|
surdeus.su/core/xgo v0.6.1
|
||||||
surdeus.su/util/tpp v0.3.2
|
surdeus.su/util/tpp v0.3.3
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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/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 h1:ebcnEcY+4tgB4a6trs4GBd2CJjrZJaPKh3i5RKQf8/U=
|
||||||
surdeus.su/util/tpp v0.3.2/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw=
|
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=
|
||||||
|
|
43
handler.go
43
handler.go
|
@ -70,9 +70,13 @@ func DefaultPP(mod string) *tpp.Preprocessor {
|
||||||
s.SetImports(xmodules.Modules)
|
s.SetImports(xmodules.Modules)
|
||||||
s.EnableFileImport(true)
|
s.EnableFileImport(true)
|
||||||
|
|
||||||
s.Add("__http_request__", ctx.Value(KeyRequest))
|
vals := ctx.Value(KeyValues).(*ContextValues)
|
||||||
s.Add("__global__", ctx.Value(KeyGlobal))
|
s.Add("__http_request__", vals.Request)
|
||||||
s.Add("__markdown__", ctx.Value(KeyMarkdown))
|
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 {
|
}).SetPreCode(func(ctx context.Context) []byte {
|
||||||
return []byte(`
|
return []byte(`
|
||||||
markdown := func(...args) {
|
markdown := func(...args) {
|
||||||
|
@ -81,9 +85,14 @@ func DefaultPP(mod string) *tpp.Preprocessor {
|
||||||
__http__ := immutable({
|
__http__ := immutable({
|
||||||
request : __http_request__
|
request : __http_request__
|
||||||
})
|
})
|
||||||
|
|
||||||
context.http = __http__
|
context.http = __http__
|
||||||
context.pp = pp
|
context.pp = pp
|
||||||
context.global = __global__
|
context.global = __global__
|
||||||
|
context.is_index = __is_index__
|
||||||
|
context.index_suffix = __index_suffix__
|
||||||
|
context.tpp_ext = __tpp_ext__
|
||||||
|
|
||||||
import("./pre")(context)
|
import("./pre")(context)
|
||||||
`)
|
`)
|
||||||
}).SetPostCode(func(ctx context.Context) []byte {
|
}).SetPostCode(func(ctx context.Context) []byte {
|
||||||
|
@ -104,6 +113,7 @@ func (h *Handler) ServeHTTP(
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request,
|
r *http.Request,
|
||||||
) {
|
) {
|
||||||
|
isIndex := false
|
||||||
shouldProcess := true
|
shouldProcess := true
|
||||||
urlPath := r.URL.Path
|
urlPath := r.URL.Path
|
||||||
// Cleaning URL path to prevent injections.
|
// Cleaning URL path to prevent injections.
|
||||||
|
@ -125,6 +135,7 @@ func (h *Handler) ServeHTTP(
|
||||||
filePath,
|
filePath,
|
||||||
h.indexSuffix,
|
h.indexSuffix,
|
||||||
)
|
)
|
||||||
|
isIndex = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,26 +158,22 @@ func (h *Handler) ServeHTTP(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ctx := context.WithValue(
|
ctx := context.WithValue(
|
||||||
r.Context(),
|
r.Context(),
|
||||||
KeyRequest,
|
KeyValues,
|
||||||
&httpx.Request{
|
&ContextValues{
|
||||||
Request: r,
|
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
|
// Setting before the code to let it change own
|
||||||
// content type?
|
// content type?
|
||||||
fileExt := filepath.Ext(filePath)
|
fileExt := filepath.Ext(filePath)
|
||||||
|
|
9
tool.go
9
tool.go
|
@ -9,15 +9,6 @@ import (
|
||||||
"log"
|
"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.
|
// Simple PHP-like server implementation.
|
||||||
var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
|
var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
|
||||||
|
|
Loading…
Reference in a new issue