Class: Homesteading::Server

Inherits:
Command
  • Object
show all
Defined in:
lib/homesteading/commands/server.rb

Constant Summary

Constants inherited from Command

Command::COMMANDS

Instance Method Summary collapse

Methods inherited from Command

create, register

Instance Method Details

#defaultObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/homesteading/commands/server.rb', line 8

def default
  puts
  options = parse_options

  router_port = options[:port] || 3000

  # Kill all Homesteading apps when `hs server` is stopped
  servers = []
  Signal.trap("INT") do
    servers.each do |io|
      puts "Stopping HS:Publisher app at PID: #{io.pid}"
      Process.kill "SIGINT", io.pid
    end

    system "homesteading server:stop"
    exit
  end

  homesteading_info = "#{Homesteading::VERSION}, codename: #{Homesteading::CODENAME}"
  ruby_info         = RUBY_DESCRIPTION
  puma_info         = "#{Puma::Const::PUMA_VERSION}, codename: #{Puma::Const::CODE_NAME}"
  env_info          = "development"

  puts "* Homesteading: #{homesteading_info}"
  puts "* Puma:         #{puma_info}"
  puts "* Ruby:         #{ruby_info}"
  puts "* Environment:  #{env_info}"
  puts "* Running on:   http://0.0.0.0:#{router_port}"
  puts

  puts "* Homesteading servers starting..."
  puts

  routes = []

  app_port = 3000
  Dir.glob("#{Dir.pwd}/homesteading-*/").each_with_index do |app_dir, index|
    app_file_path = app_dir + "app.json"

    if File.exist?(app_file_path)
      app_file  = JSON.parse(File.read(app_file_path))
      app_route = app_file["routes"]

      if app_route
        app_name = app_dir.split("/").last

        app_path = app_route["path"]
        if app_name =~ /feed/
          app_path = "feed"
        end

        unless app_name == "homesteading-router-rack"
          app_port += 1
          routes << "#{app_path}@@@localhost:#{app_port}"
        end

        if app_name =~ /homesteading-router-rack/
          # TODO TEMP FIXME serve assets from hub
          # skipping assets and router
        else
          puts "* Starting on port #{app_port} : #{app_name.sub(/homesteading-/, "")}"
          servers << IO.popen("cd #{app_dir} && bin/rails server -d --port #{app_port}", "w")
        end
      end

    end
  end

  # write routes to an env var for hs:router's routes table
  ENV["HOMESTEADING_ROUTES"] = routes.join(",")

  # start up router after ENV var is set
  puts
  puts "* Starting on port #{router_port} : router"
  servers << IO.popen("cd #{Dir.pwd}/homesteading-router-rack/ && rackup -p #{router_port}", "w")


  puts
  puts "* To stop Homesteading servers:"
  puts "  ctrl-c"
  puts "  homesteading server:stop"
  puts
end

#stopObject



92
93
94
95
96
# File 'lib/homesteading/commands/server.rb', line 92

def stop
  # TEMP HACK TODO FIXME
  # This is overly aggressive, but Signal.trap isn't working
  system "killall ruby"
end