kyra/tool.go
2024-06-09 18:26:31 +05:00

64 lines
1.3 KiB
Go

package tht
import (
"github.com/d5/tengo/v2"
"surdeus.su/util/tht/mdx"
"surdeus.su/core/cli/mtool"
"net/http"
"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) {
var (
addr, ext, src, mod, index string
noLog bool
)
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.StringVar(&index, "index", "index.htm", "index file name")
flags.BoolVar(
&noLog,
"no-log",
false,
"disable logging",
)
flags.Parse()
handler := &LoggerMiddle{
After: NewHandler(
DefaultPP(mod),
src, ext, index,
map[string] tengo.Object{},
).SetMD(mdx.MakeDefaultMarkdown()),
}
if !noLog {
handler.Logger = log.Default()
}
srv := &http.Server{
Addr: addr,
Handler: handler,
}
log.Printf("Listening on %q\n", addr)
err := srv.ListenAndServe()
if err != nil {
log.Printf("Error: srv.ListenAndServe(...): %s\n", err)
}
})