package httpx import ( //"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 key type for internal usage. type CKey string const ( KeyRequest CKey = "http-request" ) // Simple PHP-like server implementation. var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) { var ( addr, ext, src, mod string ) flags.StringVar(&addr, "addr", ":3000", "address to serve at") flags.StringVar(&mod, "mod", "./mod", "path to store Tengo modules") flags.StringVar(&src, "src", "./src", "directory with source files") flags.StringVar(&ext, "ext", ".tpp", "extension for TPP files") 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, 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), } err := srv.ListenAndServe() if err != nil { log.Printf("Error: srv.ListenAndServe(...): %s\n", err) } })