Skip to content

Commit

Permalink
feat(ruby): add Ruby implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Aug 29, 2019
1 parent 7b9cee9 commit 52eea76
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
2 changes: 2 additions & 0 deletions webapp/ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.bundle/
/vendor/bundle/
10 changes: 10 additions & 0 deletions webapp/ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'https://rubygems.org'

gem 'puma'
gem 'sinatra'
gem 'mysql2'
gem 'mysql2-cs-bind'

group :development do
gem 'sinatra-contrib'
end
41 changes: 41 additions & 0 deletions webapp/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
GEM
remote: https://rubygems.org/
specs:
backports (3.15.0)
multi_json (1.13.1)
mustermann (1.0.3)
mysql2 (0.5.2)
mysql2-cs-bind (0.0.7)
mysql2
nio4r (2.5.1)
puma (4.1.0)
nio4r (~> 2.0)
rack (2.0.7)
rack-protection (2.0.7)
rack
sinatra (2.0.7)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.7)
tilt (~> 2.0)
sinatra-contrib (2.0.7)
backports (>= 2.8.2)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.0.7)
sinatra (= 2.0.7)
tilt (~> 2.0)
tilt (2.0.9)

PLATFORMS
ruby

DEPENDENCIES
mysql2
mysql2-cs-bind
puma
sinatra
sinatra-contrib

BUNDLED WITH
2.0.1
6 changes: 6 additions & 0 deletions webapp/ruby/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lib = File.expand_path('../lib', __FILE__)
$:.unshift(lib) unless $:.include?(lib)

require 'isucari/web'

run Isucari::Web
2 changes: 2 additions & 0 deletions webapp/ruby/lib/isucari.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Isucari
end
126 changes: 126 additions & 0 deletions webapp/ruby/lib/isucari/web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
require 'json'
require 'sinatra/base'
require 'mysql2'
require 'mysql2-cs-bind'

module Isucari
class Web < Sinatra::Base
class InternalServerError < StandardError; end

configure :development do
require 'sinatra/reloader'
register Sinatra::Reloader
end

set :add_charset, ['application/json']
set :public_folder, File.join(__dir__, '..', '..', 'public')
set :root, File.join(__dir__, '..', '..')

helpers do
def db
Thread.current[:db] ||= Mysql2::Client.new(
host: ENV['MYSQL_HOST'] || '127.0.0.1',
port: ENV['MYSQL_PORT'] || '3306',
database: ENV['MYSQL_DBNAME'] || 'isucari',
username: ENV['MYSQL_USER'] || 'isucari',
password: ENV['MYSQL_PASS'] || 'isucari',
charset: 'utf8mb4',
database_timezone: :local,
cast_booleans: true,
reconnect: true,
)
end

def body_params
@body_params ||= JSON.parse(request.body.tap(&:rewind).read)
end

def halt_with_error(status = 500, error = 'unknown')
halt status, { error: error }.to_json
end
end

# API

# postInitialize
post '/initialize' do
unless system "#{settings.root}/../sql/init.sh"
halt_with_error 500, 'exec init.sh error'
end

['payment_service_url', 'shipment_service_url'].each do |name|
value = body_params[name]

db.xquery('INSERT INTO `configs` (name, val) VALUES (?, ?) ON DUPLICATE KEY UPDATE `val` = VALUES(`val`)', name, value)
end

# Campaign 実施時は true にする
is_campaign = false

content_type :json
{ is_campaign: is_campaign }.to_json
rescue JSON::ParserError
halt_with_error 500, 'json decode error'
rescue Mysql2::Error
halt_with_error 500, 'db error'
end

# Frontend

def get_index
send_file File.join(settings.public_folder, 'index.html')
end

get '/' do
get_index
end

get '/login' do
get_index
end

get '/register' do
get_index
end

get '/timeline' do
get_index
end

get '/categories/:category_id/items' do
get_index
end

get '/sell' do
get_index
end

get '/items/:item_id' do
get_index
end

get '/items/:item_id/edit' do
get_index
end

get '/items/:item_id/buy' do
get_index
end

get '/buy/complete' do
get_index
end

get '/transactions/:transaction_id' do
get_index
end

get '/users/:user_id' do
get_index
end

get '/users/setting' do
get_index
end
end
end
1 change: 1 addition & 0 deletions webapp/ruby/public

0 comments on commit 52eea76

Please sign in to comment.