forked from isucon/isucon9-qualify
-
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
Showing
7 changed files
with
188 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.bundle/ | ||
/vendor/bundle/ |
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,10 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'puma' | ||
gem 'sinatra' | ||
gem 'mysql2' | ||
gem 'mysql2-cs-bind' | ||
|
||
group :development do | ||
gem 'sinatra-contrib' | ||
end |
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,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 |
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,6 @@ | ||
lib = File.expand_path('../lib', __FILE__) | ||
$:.unshift(lib) unless $:.include?(lib) | ||
|
||
require 'isucari/web' | ||
|
||
run Isucari::Web |
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,2 @@ | ||
module Isucari | ||
end |
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,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 |
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 @@ | ||
../public |