Skip to content

Commit

Permalink
Use nginx to delivery icon image
Browse files Browse the repository at this point in the history
  • Loading branch information
daido1976 committed Feb 25, 2021
1 parent 808fff7 commit 78f9b1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 6 additions & 0 deletions nginx.conf
Expand Up @@ -24,6 +24,12 @@ server {
add_header Cache-Control public;
}

location /icons/ {
root /home/isucon/isubata/webapp/ruby;
expires 24h;
add_header Cache-Control public;
}

location / {
proxy_set_header Host $http_host;
proxy_pass http://app:5000;
Expand Down
30 changes: 14 additions & 16 deletions webapp/ruby/app.rb
Expand Up @@ -272,9 +272,7 @@ def user
end

if !avatar_name.nil? && !avatar_data.nil?
statement = db.prepare('INSERT INTO image (name, data) VALUES (?, ?)')
statement.execute(avatar_name, avatar_data)
statement.close
File.write("icons/#{avatar_name}", avatar_data)
statement = db.prepare('UPDATE user SET avatar_icon = ? WHERE id = ?')
statement.execute(avatar_name, user['id'])
statement.close
Expand All @@ -289,19 +287,19 @@ def user
redirect '/', 303
end

get '/icons/:file_name' do
file_name = params[:file_name]
statement = db.prepare('SELECT * FROM image WHERE name = ?')
row = statement.execute(file_name).first
statement.close
ext = file_name.include?('.') ? File.extname(file_name) : ''
mime = ext2mime(ext)
if !row.nil? && !mime.empty?
content_type mime
return row['data']
end
404
end
# get '/icons/:file_name' do
# file_name = params[:file_name]
# statement = db.prepare('SELECT * FROM image WHERE name = ?')
# row = statement.execute(file_name).first
# statement.close
# ext = file_name.include?('.') ? File.extname(file_name) : ''
# mime = ext2mime(ext)
# if !row.nil? && !mime.empty?
# content_type mime
# return row['data']
# end
# 404
# end

private

Expand Down

0 comments on commit 78f9b1c

Please sign in to comment.