- Run
main_api.rb
(ruby main_api.rb
) - Run
proxy_api.rb
(ruby proxy_api.rb
) - Open browser and go to
http://localhost:3001/test
- See infinite load :(
Last active
May 6, 2020 15:31
-
-
Save nbulaj/bd89ba366290033bf71c0f9b60927709 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment