Class: Opal::Server

Inherits:
Object show all
Defined in:
lib/wedge/opal.rb

Defined Under Namespace

Classes: Headers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug_or_options = {}) {|_self| ... } ⇒ Server

Returns a new instance of Server.

Yields:

  • (_self)

Yield Parameters:

  • _self (Opal::Server)

    the object that the method was called on



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wedge/opal.rb', line 77

def initialize debug_or_options = {}
  unless Hash === debug_or_options
    warn "passing a boolean to control debug is deprecated.\n"+
      "Please pass an Hash instead: Server.new(debug: true)"
    options = {:debug => debug_or_options}
  else
    options = debug_or_options
  end

  @headers     = {}
  @gzip        = false
  @use_index   = true
  @public_root = nil
  @public_urls = ['/']
  @sprockets   = options.fetch(:sprockets, ::Sprockets::Environment.new)
  @debug       = options.fetch(:debug, true)
  @prefix      = options.fetch(:prefix, '/assets')

  Opal.paths.each { |p| @sprockets.append_path(p) }

  yield self if block_given?
  create_app
end

Instance Attribute Details

#gzipObject

Returns the value of attribute gzip.



75
76
77
# File 'lib/wedge/opal.rb', line 75

def gzip
  @gzip
end

#headersObject

Returns the value of attribute headers.



75
76
77
# File 'lib/wedge/opal.rb', line 75

def headers
  @headers
end

Instance Method Details

#create_appObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/wedge/opal.rb', line 101

def create_app
  server, sprockets, prefix, headers = self, @sprockets, self.prefix, @headers
  sprockets.logger.level ||= Logger::DEBUG
  source_map_enabled = self.source_map_enabled
  if source_map_enabled
    maps_prefix = SOURCE_MAPS_PREFIX_PATH
    maps_app = SourceMapServer.new(sprockets, maps_prefix)
    ::Opal::Sprockets::SourceMapHeaderPatch.inject!(maps_prefix)
  end

  @app = Rack::Builder.app do
    not_found = lambda { |env| [404, {}, []] }
    use Rack::Deflater
    use Rack::ShowExceptions
    use Index, server if server.use_index
    use Headers, server
    if source_map_enabled
      map(maps_prefix) do
        require 'rack/conditionalget'
        require 'rack/etag'
        use Rack::ConditionalGet
        use Rack::ETag
        run maps_app
      end
    end
    map(prefix) { run sprockets }
    run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
  end
end