-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
129 lines (113 loc) · 3.15 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'sinatra'
require 'sinatra/config_file'
require 'sinatra/assetpack'
require 'sinatra/r18n'
require 'json'
require 'memcached'
require 'yaml'
require './lib/validator'
require './lib/vimrc_creator'
require './lib/vimrc_preview'
require './lib/vimrc_option'
configure do
enable :logging
config_file './config/config.yml'
set :cache, Memcached.new(settings.memcached)
end
before do
session[:locale] = 'ja'
@locale = 'ja'
end
before '/en/*' do
session[:locale] = 'en'
@locale = 'en'
request.path_info = '/' + params[:splat][0]
end
helpers do
def generate_uniqid
loop do
begin
hex = SecureRandom.hex(8)
settings.cache.get(hex)
rescue Memcached::NotFound
return hex
end
end
end
end
assets do
serve '/js', from: 'public/js'
serve '/css', from: 'public/css'
serve '/bower_components', from: 'bower_components'
js :app, [
'/bower_components/jquery/dist/jquery.min.js',
'/bower_components/jquery.blockUI.js/jquery.blockUI.js',
'/bower_components/bootstrap/dist/js/bootstrap.min.js',
'/js/*.js',
'/js/ext/*.js'
]
css :app, '/css/app.css', [
'/css/*.css'
]
css :libs, [
'/bower_components/bootstrap/dist/css/bootstrap.min.css'
]
js_compression :simple
css_compression :simple
end
get '/' do
@top_msg = t.top
@settings_msg = t.settings
@basic_options = YAML.load_file(settings.basic_options_path)
@colorscheme_options = YAML.load_file(settings.colorscheme_options_path)
@plugin_options = YAML.load_file(settings.plugin_options_path)
@programlang_options = YAML.load_file(settings.programlang_options_path)
@initial_options = File.read(settings.vimrc_default_path).split("\n")
@connection_id = generate_uniqid
erb :index
end
# vimrc作成
post '/api/vimrc' do
content_type :json
begin
params = JSON.parse(request.body.read)
# バリデーション
validator = VimFactory::Validator.new(params)
if validator.valid? == false
return [400, { message: validator.error }.to_json]
end
# vimrc作成
container_id = settings.cache.get(params['connection_id'])
vimrc_creator = VimFactory::VimrcCreator.new(
params['vimrc_contents'],
"#{settings.vimrc_dir}/#{container_id}/vimrc",
container_id
)
vimrc_creator.create
rescue JSON::ParserError => e
logger.error(e.message)
return [400, { message: 'Requestbody should be JSON format' }.to_json]
rescue => e
logger.error("msg:#{e.message} trace:#{e.backtrace}")
return [500, { message: 'Unexpected error' }.to_json]
end
logger.info('success')
[201, { vimrc_contents: params['vimrc_contents'] }.to_json]
end
# vimrc取得
get '/api/vimrc/:connection_id' do |id|
content_type :json
begin
vimrc_path = "#{settings.vimrc_dir}/#{settings.cache.get(id)}/vimrc"
vimrc_preview = VimFactory::VimrcPreview.new(vimrc_path)
vimrc = vimrc_preview.get
rescue Memcached::NotFound => e
logger.error(e.message)
return [404, { message: 'Vimrc is not found.' }.to_json]
rescue => e
logger.error(e.message)
return [500, { message: 'Unexpected error' }.to_json]
end
logger.info('success')
[200, { vimrc: vimrc }.to_json]
end