Skip to content

Instantly share code, notes, and snippets.

@nbulaj
Last active May 6, 2020 15:31
Show Gist options
  • Save nbulaj/bd89ba366290033bf71c0f9b60927709 to your computer and use it in GitHub Desktop.
Save nbulaj/bd89ba366290033bf71c0f9b60927709 to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'sqlite3'
# gem 'pg'
gem 'rails', '~> 6.0'
gem 'grape', "~> 1.3"
gem 'puma'
end
require 'rails'
require 'active_model/railtie'
require 'action_controller/railtie'
require 'action_controller/railtie'
require 'rack/handler/puma'
require "grape"
class API < Grape::API
version :v1, using: :path
format :json
content_type :json, "application/json; charset=UTF-8"
resource :test do
desc "Test endpoint"
get do
{ status: "OK" }
end
end
end
class TestApp < Rails::Application
config.load_defaults 6.0
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.eager_load = false
config.logger = Logger.new($stdout)
Rails.logger = config.logger
end
Rails.application.initialize!
Rails.application.routes.draw do
mount API => "/"
end
Rack::Handler::Puma.run(TestApp, Port: 3000)
# Run the server:
# $ ruby main_api.rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem "sinatra", "~> 2.0", ">= 2.0.7"
gem "sinatra-contrib", "~> 2.0", ">= 2.0.7"
gem "puma", "~> 4.3"
gem "curb", "~> 0.9"
end
require 'sinatra'
require 'rack/handler/puma'
require 'curb'
LOGGER = Logger.new($stdout)
class Server < Sinatra::Base
register Sinatra::MultiRoute
route :head, :delete, :get, :options, :path, :post, :put, :patch, "/*" do |path|
LOGGER.info("Started request: /#{path}")
curl = Curl::Easy.new("http://localhost:3000/v1/test.json")
curl.perform
LOGGER.info("Headers:")
# copy headers
curl.header_str.to_s.split("\r\n").each do |header|
name, value = header.split(":", 2)
LOGGER.info("\t#{name}:#{value}")
response.headers[name] = value.strip if value
end
LOGGER.info("Ready")
[curl.status.split(/\s+/, 2).first.to_i, curl.body]
end
end
Rack::Handler::Puma.run(Server, Port: 3001)
# Run the proxy server:
# $ ruby proxy_api.rb
  1. Run main_api.rb (ruby main_api.rb)
  2. Run proxy_api.rb (ruby proxy_api.rb)
  3. Open browser and go to http://localhost:3001/test
  4. See infinite load :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment