Class: Xing::DevAssets::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/dev-assets/rack_app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backend_portObject

Returns the value of attribute backend_port.



18
19
20
# File 'lib/xing/dev-assets/rack_app.rb', line 18

def backend_port
  @backend_port
end

#backend_urlObject



37
38
39
# File 'lib/xing/dev-assets/rack_app.rb', line 37

def backend_url
  @backend_url ||= ENV["XING_BACKEND_URL"] || ENV["LRD_BACKEND_URL"] || "http://localhost:#{backend_port}/"
end

#builderObject

Returns the value of attribute builder.



18
19
20
# File 'lib/xing/dev-assets/rack_app.rb', line 18

def builder
  @builder
end

#envObject



41
42
43
# File 'lib/xing/dev-assets/rack_app.rb', line 41

def env
  @env ||= ENV['RAILS_ENV'] || 'development'
end

#loggerObject



45
46
47
# File 'lib/xing/dev-assets/rack_app.rb', line 45

def logger
  @logger ||= Logger.new(logpath_for_env)
end

#out_streamObject



33
34
35
# File 'lib/xing/dev-assets/rack_app.rb', line 33

def out_stream
  @out_stream ||= $stdout
end

#root_pathObject

Returns the value of attribute root_path.



18
19
20
# File 'lib/xing/dev-assets/rack_app.rb', line 18

def root_path
  @root_path
end

Class Method Details

.build(root_path, backend_port) {|rack_app| ... } ⇒ Object

Yields:

  • (rack_app)


10
11
12
13
14
15
16
# File 'lib/xing/dev-assets/rack_app.rb', line 10

def self.build(root_path, backend_port)
  rack_app = new
  rack_app.root_path = root_path
  rack_app.backend_port = backend_port
  yield rack_app if block_given?
  rack_app.build
end

Instance Method Details

#buildObject



109
110
111
112
113
114
115
116
# File 'lib/xing/dev-assets/rack_app.rb', line 109

def build
  report_startup

  setup_middleware
  stub_application

  builder
end

#cookiesObject



60
61
62
63
# File 'lib/xing/dev-assets/rack_app.rb', line 60

def cookies
  builder.use CookieSetter, "lrdBackendUrl", backend_url
  builder.use CookieSetter, "xingBackendUrl", backend_url
end

#disable_cachingObject



65
66
67
# File 'lib/xing/dev-assets/rack_app.rb', line 65

def disable_caching
  builder.use StripIncomingCacheHeaders
end

#goto_redirectObject



56
57
58
# File 'lib/xing/dev-assets/rack_app.rb', line 56

def goto_redirect
  builder.use GotoParam
end

#log_rootObject

Should be override by client app. Ironically, override with exactly this definition will usually work. (because this will log into a dir in the gem, but copied into subclass will be relative to that file and therefore into the project)



25
26
27
# File 'lib/xing/dev-assets/rack_app.rb', line 25

def log_root
  File.expand_path("../../log", __FILE__)
end

#loggingObject



69
70
71
# File 'lib/xing/dev-assets/rack_app.rb', line 69

def logging
  builder.use Rack::CommonLogger, logger
end

#logpath_for_envObject



29
30
31
# File 'lib/xing/dev-assets/rack_app.rb', line 29

def logpath_for_env
  File.join( log_root, "#{env}_static.log")
end

#report_startupObject



49
50
51
52
53
54
# File 'lib/xing/dev-assets/rack_app.rb', line 49

def report_startup
  out_stream.puts "Setting up static app:"
  out_stream.puts "  serving files from #{root_path}"
  out_stream.puts "  using #{backend_url} for API"
  out_stream.puts "  logging to #{logpath_for_env}"
end

#setup_middlewareObject



96
97
98
99
100
101
102
103
# File 'lib/xing/dev-assets/rack_app.rb', line 96

def setup_middleware
  goto_redirect
  cookies
  disable_caching
  logging
  shortcut_livereload
  static_assets
end

#shortcut_livereloadObject



73
74
75
76
77
78
79
# File 'lib/xing/dev-assets/rack_app.rb', line 73

def shortcut_livereload
  if env != "development"
    builder.map "/assets/livereload.js" do
      run EmptyFile.new
    end
  end
end

#static_assetsObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/xing/dev-assets/rack_app.rb', line 81

def static_assets
  builder.use Rack::Static, {
    :urls => [""],
    :root => root_path,
    :index => "index.html",
    :header_rules => {
      :all => {"Cache-Control" => "no-cache, max-age=0" } #no caching development assets
    }
  }
end

#stub_applicationObject



92
93
94
# File 'lib/xing/dev-assets/rack_app.rb', line 92

def stub_application
  builder.run proc{ [500, {}, ["Something went wrong"]] }
end