-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0539f40
commit 7b20d07
Showing
7 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
require 'rack' | ||
require 'sqlite3' | ||
require 'erb' | ||
require 'json' | ||
|
||
# Rack application | ||
# | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
end | ||
|
||
require 'rack' | ||
|
||
app = | ||
proc do |_env| | ||
[200, { 'content-type' => 'text/html' }, ['Hello, World!']] | ||
end | ||
|
||
run app | ||
|
||
# curl -i "http://localhost:9292" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
end | ||
|
||
require 'rack' | ||
|
||
class MyApp | ||
def call(env) | ||
request = Rack::Request.new(env) | ||
|
||
case [request.request_method, request.path] | ||
in ['GET', '/'] | ||
[200, { 'content-type' => 'text/plain' }, ['Home page']] | ||
in ['POST', '/books'] | ||
[201, { 'content-type' => 'text/plain' }, ['The book was created']] | ||
else [404, { 'content-type' => 'text/html' }, ['Not Found']] | ||
end | ||
end | ||
end | ||
|
||
run MyApp.new | ||
|
||
# curl -i -X POST http://localhost:9292/books |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
end | ||
|
||
require 'rack' | ||
|
||
class MyApp | ||
def call(env) | ||
request = Rack::Request.new(env) | ||
@response = Rack::Response.new | ||
|
||
case [request.request_method, request.path] | ||
in ['GET', '/'] | ||
@response.status = 200 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('Home page') | ||
|
||
response.finish | ||
in ['POST', '/books'] | ||
@response.status = 201 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('The book was created') | ||
|
||
@response.finish | ||
else not_found | ||
end | ||
end | ||
|
||
def not_found | ||
@response.status = 404 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('Not Found') | ||
|
||
@response.finish | ||
end | ||
end | ||
|
||
run MyApp.new | ||
|
||
# curl -i http://localhost:9292/ | ||
# curl -i -X POST http://localhost:9292/books |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
end | ||
|
||
require 'rack' | ||
require_relative 'gurupi_middleware' | ||
|
||
class MyApp | ||
def call(env) | ||
request = Rack::Request.new(env) | ||
@response = Rack::Response.new | ||
|
||
case [request.request_method, request.path] | ||
in ['GET', '/'] | ||
@response.status = 200 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('Home page') | ||
|
||
response.finish | ||
in ['POST', '/books'] | ||
@response.status = 201 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('The book was created') | ||
|
||
@response.finish | ||
else not_found | ||
end | ||
end | ||
|
||
def not_found | ||
@response.status = 404 | ||
@response['content-type'] = 'text/plain' | ||
@response.write('Not Found') | ||
|
||
@response.finish | ||
end | ||
end | ||
|
||
use GurupiMiddleware | ||
|
||
run MyApp.new | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class GurupiMiddleware | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
status, headers, body = @app.call(env) | ||
|
||
headers['x-gurupi'] = 'Gurupi' | ||
|
||
[status, headers, body] | ||
end | ||
end |