Sha256: e0b76c417ff52851319af360451fe8764001e1928a756532b2e81e6fdcec2745

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

module Low
  module Middleware
    # `SubdomainMap` is Rack middleware that delegates to other Rack apps based
    # upon subdomain. It takes two arguments upon initialization:
    #
    # * `base`, the app to be called if there is no subdomain.
    # * `map`, a hash that maps subdomain strings to apps.
    class SubdomainMap
      def initialize(base, map = {})
        @base = base
        @map = map
      end

      def call(env)
        # If the `SERVER_NAME` environment variable has a subdomain
        if env['SERVER_NAME'] =~ /(.*?)\.(?:.*)\..*/
          subdomain = $1
        end

        if subdomain
          # and we can find a corresponding app,
          if app = @map[subdomain]
            # call it and return the result.
            app.call(env)

          # Otherwise, return 403 Forbidden.
          else
            [403, {"Content-Type" => "text/plain"}, []]
          end

        # If there is no subdomain,
        else
          # call the base app and return the result.
          @base.call(env)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
low-0.0.12 lib/low/middleware/subdomain_map.rb
low-0.0.11 lib/low/middleware/subdomain_map.rb
low-0.0.10 lib/low/middleware/subdomain_map.rb
low-0.0.9 lib/low/middleware/subdomain_map.rb
low-0.0.8 lib/low/middleware/subdomain_map.rb
low-0.0.7 lib/low/middleware/subdomain_map.rb
low-0.0.6 lib/low/middleware/subdomain_map.rb