tool.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package kyra
  2. import (
  3. "surdeus.su/core/xgo/v2"
  4. "surdeus.su/util/kyra/mdx"
  5. "surdeus.su/core/cli/mtool"
  6. "net/http"
  7. "log"
  8. )
  9. // Simple PHP-like server implementation.
  10. var Tool = mtool.T("kyra").Func(func(flags *mtool.Flags) {
  11. var (
  12. addr, ext, src, mod, index string
  13. noLog bool
  14. )
  15. flags.StringVar(&addr, "addr", ":3000", "address to serve at")
  16. flags.StringVar(&mod, "mod", "./mod", "path to store Tengo modules")
  17. flags.StringVar(&src, "src", "./src", "directory with source files")
  18. flags.StringVar(&ext, "ext", ".kr", "extension for Kyra files")
  19. flags.StringVar(&index, "index", "index.htm", "index file name")
  20. flags.BoolVar(&noLog, "no-log", false, "disable logging")
  21. flags.Parse()
  22. handler := &LoggerMiddle{
  23. After: NewHandler(
  24. DefaultPP(mod),
  25. src, ext, index,
  26. map[string]xgo.Object{},
  27. ).SetMD(mdx.MakeDefaultMarkdown()),
  28. }
  29. if !noLog {
  30. handler.Logger = log.Default()
  31. }
  32. srv := &http.Server{
  33. Addr: addr,
  34. Handler: handler,
  35. }
  36. log.Printf("Listening on %q\n", addr)
  37. err := srv.ListenAndServe()
  38. if err != nil {
  39. log.Printf("Error: srv.ListenAndServe(...): %s\n", err)
  40. }
  41. }).Ldesc(`
  42. Simple PHP-like server implementation of the Kyra
  43. suitable for the most of the needs of websites.
  44. `)