Sha256: 308014090d22159799eb3b1a098c1ce976a0f4cc4b4d0237508dda977598f5e4

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'sinatra'

Dir.glob(__dir__ + '/../commands/**/*.rb').each { |f| require f }
Dir.glob(__dir__ + '/../projections/**/query.rb').each { |f| require f }

module <%= project_class_name %>
  class Server < Sinatra::Base
    BadRequest = Class.new(StandardError)
    UnprocessableEntity = Class.new(StandardError)

    # Ensure our error handlers are triggered in development
    set :show_exceptions, :after_handler

    configure :development do
      require 'better_errors'
      use BetterErrors::Middleware
      BetterErrors.application_root = __dir__
    end

    error UnprocessableEntity do |error|
      body "Unprocessable Entity: #{error.message}"
      status 422
    end

    error BadRequest do |error|
      body "Bad Request: #{error.message}"
      status 400
    end

    before do
      content_type :json
    end

    def json_params
      # Coerce this into a symbolised Hash so Sinatra data
      # structures don't leak into the command layer

      request_body = request.body.read
      unless request_body.empty?
        params.merge!(JSON.parse(request_body))
      end

      Hash[
        params.map{ |k, v| [k.to_sym, v] }
      ]
    end

    # Add your API routes here!
    #
    # eg.
    # get '/todos' do
    #   ...
    # end
    #
    # post '/todo/:id' do
    #   ...
    # end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_sourcery_generators-0.2.0 lib/event_sourcery_generators/generators/templates/project/server.rb.tt