PageRenderTime 68ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/fan/Main.fan

https://bitbucket.org/afrankvt/draft/
Unknown | 122 lines | 105 code | 17 blank | 0 comment | 0 complexity | 00d411ad7fc97ced01f31d41de0df4a6 MD5 | raw file
  1. //
  2. // Copyright (c) 2011, Andy Frank
  3. // Licensed under the MIT License
  4. //
  5. // History:
  6. // 7 Jun 2011 Andy Frank Creation
  7. //
  8. using concurrent
  9. using util
  10. using web
  11. using wisp
  12. **
  13. ** Main entry-point for Draft CLI tools.
  14. **
  15. class Main
  16. {
  17. ** True for production mode.
  18. Bool prod := false
  19. ** HTTP port to run Wisp on.
  20. Int port := 8080
  21. ** Entry-point.
  22. Int main()
  23. {
  24. // check args
  25. args := Env.cur.args
  26. if (checkArgs(args) != 0) return -1
  27. // find and verify webmod
  28. pod := Pod.find(args.last, false)
  29. type := pod==null ? Type.find(args.last, false) : pod.types.find |t| { t.fits(DraftMod#) }
  30. if (type == null) return err("$args.first not found")
  31. if (!type.fits(DraftMod#)) return err("$type does not extend DraftMod")
  32. // check for production mode
  33. if (prod)
  34. {
  35. runServices([WispService
  36. {
  37. it.httpPort = this.port
  38. it.root = type.make
  39. }])
  40. return 0
  41. }
  42. // start restarter actor
  43. pool := ActorPool()
  44. restarter := DevRestarter(pool)
  45. {
  46. it.type = type
  47. it.port = this.port + 1
  48. }
  49. // boot proxy
  50. restarter.checkPods
  51. // start proxy server
  52. mod := DevMod(restarter)
  53. runServices([WispService { it.httpPort=this.port; it.root=mod }])
  54. return 0
  55. }
  56. ** Check arguments.
  57. private Int checkArgs(Str[] args)
  58. {
  59. if (args.size < 1) return help
  60. for (i:=0; i<args.size-1; i++)
  61. {
  62. arg := args[i]
  63. switch (arg)
  64. {
  65. case "-prod":
  66. this.prod = true
  67. case "-port":
  68. p := args[i+1].toInt(10, false)
  69. if (p == null || p < 0) return err("Invalid port ${args[i+1]}")
  70. this.port = p
  71. }
  72. }
  73. return 0
  74. }
  75. ** Run services.
  76. private Void runServices(Service[] services)
  77. {
  78. Env.cur.addShutdownHook |->| { shutdownServices }
  79. services.each |Service s| { s.install }
  80. services.each |Service s| { s.start }
  81. Actor.sleep(Duration.maxVal)
  82. }
  83. ** Cleanup services on exit.
  84. private static Void shutdownServices()
  85. {
  86. Service.list.each |Service s| { s.stop }
  87. Service.list.each |Service s| { s.uninstall }
  88. }
  89. ** Print usage.
  90. private Int help()
  91. {
  92. echo("usage: fan draft [options] <pod | pod::Type>
  93. -prod run in production mode
  94. -port <port> port to run HTTP server on (defaults to 8080)")
  95. return -1
  96. }
  97. ** Print error message.
  98. private Int err(Str msg)
  99. {
  100. echo("ERR: $msg")
  101. return -1
  102. }
  103. ** Log file.
  104. private const Log log := Log.get("draft")
  105. }