diff --git a/httpx/handler.go b/httpx/handler.go index 9fb64e1..8d88835 100644 --- a/httpx/handler.go +++ b/httpx/handler.go @@ -1,6 +1,7 @@ package httpx import ( + //"github.com/d5/tengo/v2" "surdeus.su/util/tpp" "path/filepath" "net/http" @@ -28,22 +29,61 @@ type Handler struct { // For example "file.html.tpp" will be // first preprocessed TPP and sent back as simple HTML. ext string + + // The global map accessable from all the + // request so you can keep the states + // between requests. + global any } // Returns the new Handler with -// specified preprocessor, source path and -// extension. +// specified preprocessor, source path, +// preprocessor extension and the global value. func NewHandler( pp *tpp.Preprocessor, src, ext string, + global any, ) *Handler { ret := &Handler{} ret.sourcePath = src ret.ext = ext ret.pp = pp + ret.global = global return ret } +// Returns the default tpp.Preprocessor suitable for +// the most needs. +func DefaultPP(mod string) *tpp.Preprocessor { + t := tpp.NewTengo().SetPreCompile(func( + ctx context.Context, + s *tpp.Script, + ) { + s.SetImportDir(mod) + s.SetImports(&ModuleGetter{}) + s.EnableFileImport(true) + + s.Add("__http_request__", ctx.Value(KeyRequest)) + s.Add("__global__", ctx.Value(KeyGlobal)) + }).SetPreCode(func(ctx context.Context) []byte { + return []byte(` + http := immutable({ + request : __http_request__ + }) + context.http = http + context.pp = pp + context.global = __global__ + import("./pre")(context) + `) + }).SetPostCode(func(ctx context.Context) []byte { + return []byte(` + import("./post")(context) + `) + }) + + return tpp.New(t) +} + func (h *Handler) ServeHTTP( w http.ResponseWriter, r *http.Request, @@ -86,6 +126,12 @@ func (h *Handler) ServeHTTP( }, ) + ctx = context.WithValue( + ctx, + KeyGlobal, + h.global, + ) + // Setting before the code to let it change own // content type. contentType := mime.TypeByExtension(urlExt) diff --git a/httpx/tool.go b/httpx/tool.go index cbcfbb9..89afdd0 100644 --- a/httpx/tool.go +++ b/httpx/tool.go @@ -1,13 +1,12 @@ package httpx import ( - //"github.com/d5/tengo/v2" + "github.com/d5/tengo/v2" //"github.com/d5/tengo/v2/stdlib" - "surdeus.su/util/tpp" "vultras.su/core/cli/mtool" "net/http" "log" - "context" + //"context" ) // Context key type for internal usage. @@ -15,6 +14,7 @@ type CKey string const ( KeyRequest CKey = "http-request" + KeyGlobal = "global" ) // Simple PHP-like server implementation. @@ -30,33 +30,14 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { flags.Parse() - t := tpp.NewTengo().SetPreCompile(func( - ctx context.Context, - s *tpp.Script, - ) { - s.SetImportDir(mod) - s.SetImports(&ModuleGetter{}) - s.EnableFileImport(true) - - s.Add("__http_request__", ctx.Value(KeyRequest)) - }).SetPreCode(func(ctx context.Context) []byte { - return []byte(` - http := immutable({ - request : __http_request__ - }) - __context__.http = http - __context__.pp = pp - import("./pre")(__context__) - `) - }).SetPostCode(func(ctx context.Context) []byte { - return []byte(` - import("./post")(__context__) - `) - }) srv := &http.Server{ Addr: addr, - Handler: NewHandler(tpp.New(t), src, ext), + Handler: NewHandler( + DefaultPP(mod), + src, ext, + map[string] tengo.Object{}, + ), } err := srv.ListenAndServe() if err != nil { diff --git a/src/main.htm.tpp b/src/main.htm.tpp index 03f6537..f3feef1 100644 --- a/src/main.htm.tpp +++ b/src/main.htm.tpp @@ -1,11 +1,23 @@ {{# - __context__.title = "The example.site main page" + context.title = "The example.site main page" }}