Class: AppProxy

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/brisk/server/proxy.rb

Instance Method Summary collapse

Instance Method Details

#rewrite_env(env) ⇒ Object



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
# File 'lib/brisk/server/proxy.rb', line 14

def rewrite_env(env)
  # Get all the necessary info
  request = BriskRequestHelper.new env
  site = request.get_site
  valet_home_path = request.valet_home_path
  config = request.get_config
  site_path = Site.get_site_paths(config, site)

  port = "3001"
  port = "3003" if Site.is_secured?(config, site)

  # Check if site is running
  # check if site is equal to written site
  # Check if site is secured
  # otherwise, kill process
  # Kill existing servers, so we don't get conflitcs
  if Server.is_running(valet_home_path)

    if Site.read_site(valet_home_path) == site
      if Site.is_secured?(config, site)
        env["SERVER_PORT"] = "443" if env["SERVER_PORT"] == "80"
        env["rack.url_scheme"] = "https" if env["rack.url_scheme"] == "http"
        env["rack.ssl_verify_none"] = true
        env["HTTP_HOST"] = "localhost:3003"
        return env
      end
      env["HTTP_HOST"] = "localhost:3001"
      return env
    end

    existing_pid = Pid.get(port)
    Server.shutdown(existing_pid)
  end


  if Site.is_secured?(config, site)

    Server.start_https(site_path)
    # Check if proccess is up and running
    started = Server.is_started(site_path, port)

    # Write the pid to a file so we can delete it later on
    if started
      Site.write_site(valet_home_path, site)
    end

    env["SERVER_PORT"] = "443" if env["SERVER_PORT"] == "80"
    env["rack.url_scheme"] = "https" if env["rack.url_scheme"] == "http"
    env["rack.ssl_verify_none"] = true
    env["HTTP_HOST"] = "localhost:3003"
    sleep(1)
    return env
  end

  # Boot up the server
  Server.start(site_path)

  # Check if proccess is up and running
  started = Server.is_started(site_path, port)

#    Write the pid to a file so we can delete it later on
  if started
    Site.write_site(valet_home_path, site)
  end

  run env
end

#run(env, counter = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/brisk/server/proxy.rb', line 82

def run(env, counter = 0)

  raise Exception.new "Brisk is not able to run the server.. Connection refused" if counter == 10

  sleep(0.5)
  uri = URI('http://localhost:3001')

  begin
    # Check if the server is up and running. Find a cleaner way to do this
    Net::HTTP.get(uri)
    env['HTTP_HOST'] = 'localhost:3001'
    return env
  rescue Errno::ECONNREFUSED
    run(env, counter + 1)
  end
end