module Vapid module Controllers # Project routes module ProjectController extend Sinatra::Extension # rubocop:disable Metrics/AbcSize, MethodLength def self.registered(app) app.configure do root_path = Pathname.new(app.settings.root) tmp_path = Pathname.new(app.settings.project_cache) relative_path = tmp_path.relative_path_from(root_path) app.set :cache_enabled, false app.set :cache_path, File.join(relative_path, "server") app.set :layouts, File.join(app.settings.project_views, "layouts") end app.configure :production do app.set :cache_enabled, true end # rubocop:disable Lint/NestedMethodDefinition app.helpers do def asset_timestamp(*args) super(args) unless settings.deploy end def dynamic_layout(path) path_layout = path.split("/")[1] if path_layout && Dir.glob(File.join(settings.layouts, "#{path_layout}.*")).any? "layouts/#{path_layout}" else :default end end def render_or_index(path) render path, views: settings.project_views, layout: dynamic_layout(path) rescue render "#{path.chomp('/')}/index", views: settings.project_views, layout: dynamic_layout(path) end end # rubocop:enable Lint/NestedMethodDefinition app.get "*" do path = params[:splat].first.to_s cache_block "#{path}_" do html = render_or_index(path) if app.deploy headers["Content-Disposition"] = "inline" html else Template.new(html).render end end end end # rubocop:enable Metrics/AbcSize, MethodLength end end end