lib/murlsh/dispatch.rb in murlsh-1.4.1 vs lib/murlsh/dispatch.rb in murlsh-1.5.0
- old
+ new
@@ -12,29 +12,38 @@
# Set up database connection and dispatch table.
def initialize(config)
@config = config
- url_server = Murlsh::UrlServer.new(config)
+ atom_server = Murlsh::AtomServer.new(config)
json_server = Murlsh::JsonServer.new(config)
+ m3u_server = Murlsh::M3uServer.new(config)
+ podcast_server = Murlsh::PodcastServer.new(config)
+ pop_server = Murlsh::PopServer.new(config)
+ random_server = Murlsh::RandomServer.new(config)
+ rss_server = Murlsh::RssServer.new(config)
+ url_server = Murlsh::UrlServer.new(config)
+
root_path = URI(config.fetch('root_url')).path
@routes = [
- [%r{^HEAD #{root_path}(url)?$}, url_server.method(:head)],
- [%r{^GET #{root_path}(url)?$}, url_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}atom\.atom$}, atom_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}json\.json$}, json_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}m3u\.m3u$}, m3u_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}podcast\.rss$}, podcast_server.method(:get)],
+ [%r{^POST #{root_path}pop$}, pop_server.method(:post)],
+ [%r{^(?:HEAD|GET) #{root_path}random$}, random_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}rss\.rss$}, rss_server.method(:get)],
+ [%r{^(?:HEAD|GET) #{root_path}(url)?$}, url_server.method(:get)],
[%r{^POST #{root_path}(url)?$}, url_server.method(:post)],
- [%r{^HEAD #{root_path}json\.json$}, json_server.method(:head)],
- [%r{^GET #{root_path}json\.json$}, json_server.method(:get)],
]
db_init
end
def db_init
- ActiveRecord::Base.establish_connection(
- :adapter => 'sqlite3', :database => @config.fetch('db_file'))
-
+ ActiveRecord::Base.establish_connection config.fetch('db')
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.include_root_in_json = false
# ActiveRecord::Base.logger = Logger.new(STDERR)
end
@@ -53,16 +62,21 @@
dispatch(req).call(req).finish
end
# Called if the request is not found.
def not_found(req)
- Rack::Response.new "<p>#{req.url} not found</p>
+ if req.head?
+ Rack::Response.new([], 404)
+ else
+ Rack::Response.new("<p>#{req.url} not found</p>
-<p><a href=\"#{@config['root_url']}\">root<a></p>
+<p><a href=\"#{config.fetch('root_url')}\">root<a></p>
",
- 404, { 'Content-Type' => 'text/html' }
+ 404, { 'Content-Type' => 'text/html' })
+ end
end
+ attr_reader :config
attr_accessor :routes
end
end