gopp/server/tool.go

54 lines
1.1 KiB
Go
Raw Normal View History

2024-06-09 14:51:03 +03:00
package server
import (
2024-05-20 20:34:11 +03:00
"github.com/d5/tengo/v2"
//"github.com/d5/tengo/v2/stdlib"
2024-05-20 20:34:11 +03:00
"surdeus.su/util/tpp/mdx"
"surdeus.su/core/cli/mtool"
"net/http"
"log"
//"context"
)
// Context key type for internal usage.
type CKey string
const (
KeyRequest CKey = "http-request"
KeyGlobal = "global"
2024-05-20 20:34:11 +03:00
KeyMarkdown = "markdown"
KeyHTML = "html"
)
// Simple PHP-like server implementation.
var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
var (
2024-06-09 14:51:03 +03:00
addr, ext, src, mod, index 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")
2024-06-09 14:51:03 +03:00
flags.StringVar(&index, "index", "index.htm", "index file name")
flags.Parse()
srv := &http.Server{
Addr: addr,
Handler: NewHandler(
DefaultPP(mod),
2024-06-09 14:51:03 +03:00
src, ext, index,
map[string] tengo.Object{},
2024-05-20 20:34:11 +03:00
).SetMD(mdx.MakeDefaultMarkdown()),
}
2024-05-20 20:34:11 +03:00
log.Printf("Listening on %q\n", addr)
err := srv.ListenAndServe()
if err != nil {
log.Printf("Error: srv.ListenAndServe(...): %s\n", err)
}
})