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