I’m building a UI for editing blogs, and I want to interact with middleman through an api. I’m exposing the api in config.rb like so:
require 'lib/apiserver.rb'
map "/api" do
run ApiServer
end
and inside of lib/apiserver.rb
I’m getting access to the pages like:
require 'sinatra'
class ApiServer < Sinatra::Base
get '/post' do
app = load_app
puts "Lookig up #{params[:path]}"
file = app.sitemap.find_resource_by_path params[:path]
raw = File.read file.source_file
body = raw.gsub( /^---\n.*?---\n*/m, "" ) # Remove the preyaml
{ meta: file.data, content: body }.to_json
end
private
def load_app
opts = {}
app = ::Middleman::Application.server.inst do
set :environment, opts[:environment].to_sym if opts[:environment]
::Middleman::Logger.singleton(opts[:debug] ? 0 : 1, opts[:instrumenting] || false)
end
app
end
end
Is there a better way to pass a reference to the current app to the sinatra component when it’s mounted?