#!/usr/bin/ruby
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "rack/json_schema"
require "yaml"

begin
  path = ARGV.shift or raise
  str = File.read(path)
rescue
  puts "Usage: #{$0} schema.json [rack options]"
  exit
end

case File.extname(path)
when ".yml", ".yaml"
  schema = YAML.load(str)
else
  schema = JSON.parse(str)
end

app = Rack::Builder.new do
  use Rack::JsonSchema::Docs, schema: schema
  use Rack::JsonSchema::SchemaProvider, schema: schema
  use Rack::JsonSchema::ErrorHandler
  use Rack::JsonSchema::RequestValidation, schema: schema
  use Rack::JsonSchema::ResponseValidation, schema: schema
  use Rack::JsonSchema::Mock, schema: schema
  run ->(env) { [404, {}, ["Not found"]] }
end

options = Rack::Server::Options.new.parse!(ARGV).merge(app: app)
Rack::Server.start(options)
