gopp/httpx/tool.go

62 lines
1.2 KiB
Go
Raw Normal View History

package httpx
import (
//"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"vultras.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 string
handler Handler
)
flags.StringVar(&addr, "addr", ":3000", "address to serve at")
flags.StringVar(&handler.SourcePath, "src", "./src", "directory with source files")
flags.StringVar(&handler.Ext, "ext", ".tpp", "extension for TPP files")
flags.Parse()
t := tpp.NewTengo().SetPreCompile(func(
ctx context.Context,
s *tpp.Script,
){
s.SetImportDir(handler.SourcePath)
s.SetImports(stdlib.GetModuleMap(
stdlib.AllModuleNames()...,
))
s.EnableFileImport(true)
s.Add("__http_request__", ctx.Value(KeyRequest))
}).SetPreCode(func(ctx context.Context) []byte {
return []byte(`
http := {
request : __http_request__
}
`)
})
handler.PP = tpp.New(t)
srv := &http.Server{
Addr: addr,
Handler: &handler,
}
err := srv.ListenAndServe()
if err != nil {
log.Printf("Error: srv.ListenAndServe(...): %s\n", err)
}
})