kyra/tool.go
2024-08-02 20:30:32 +05:00

52 lines
1.2 KiB
Go

package kyra
import (
"surdeus.su/core/xgo/v2"
"surdeus.su/util/kyra/mdx"
"surdeus.su/core/cli/mtool"
"net/http"
"log"
)
// Simple PHP-like server implementation.
var Tool = mtool.T("kyra").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", ".kr", "extension for Kyra 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]xgo.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)
}
}).Ldesc(`
Simple PHP-like server implementation of the Kyra
suitable for the most of the needs of websites.
`)