diff --git a/.gitignore b/.gitignore index 5657f6ea..44911cf4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -vendor \ No newline at end of file +vendor +public/sitemap*.xml* diff --git a/Dockerfile b/Dockerfile index fbd74d06..d699b246 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,8 @@ COPY data /app/data/ RUN echo "** installing deps **" && \ apk --no-cache add redis && \ echo "** installing ruby gems **" && \ + gem install bundler && \ + echo "** updating bundler **" && \ bundle install && \ echo "** starting redis-server **" && \ redis-server --daemonize yes && \ diff --git a/Gemfile.build b/Gemfile.build index 20104143..a39585f9 100644 --- a/Gemfile.build +++ b/Gemfile.build @@ -1,4 +1,6 @@ # Gemfile used in the build docker container source "https://rubygems.org" -gem "redis" \ No newline at end of file +gem "redis" +gem "sitemap_generator" +gem 'to_slug', '~> 1.0', '>= 1.0.8' diff --git a/Gemfile.build.lock b/Gemfile.build.lock index 138c8052..193b15d3 100644 --- a/Gemfile.build.lock +++ b/Gemfile.build.lock @@ -1,13 +1,19 @@ GEM remote: https://rubygems.org/ specs: - redis (4.1.3) + builder (3.2.4) + redis (4.2.5) + sitemap_generator (6.1.2) + builder (~> 3.0) + to_slug (1.0.8) PLATFORMS ruby DEPENDENCIES redis + sitemap_generator + to_slug (~> 1.0, >= 1.0.8) BUNDLED WITH - 2.0.2 + 2.2.7 diff --git a/Gemfile.lock b/Gemfile.lock index 709c1732..a4144903 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -58,4 +58,4 @@ DEPENDENCIES thin BUNDLED WITH - 2.2.6 + 2.2.7 diff --git a/app.rb b/app.rb index 93e4075a..f9e1a3b2 100644 --- a/app.rb +++ b/app.rb @@ -5,6 +5,7 @@ require 'ifsc' require './metrics' require 'sinatra/json' +require "sinatra/multi_route" require 'secure_headers' class IFSCPlus < Razorpay::IFSC::IFSC @@ -80,12 +81,19 @@ def ifsc_data(code) data = settings.redis.hgetall code if !data.empty? + + data['PINCODE'] = nil data['BANK'], data['BANKCODE'] = bank_details(code) data['IFSC'] = code data['RTGS'] = strtobool data['RTGS'] data['NEFT'] = strtobool data['NEFT'] data['IMPS'] = strtobool data['IMPS'] data['UPI'] = strtobool data['UPI'] + + if data['ADDRESS'] =~ /(\d{6})/ + data['PINCODE'] = data['ADDRESS'].match /(\d{6})/ + end + settings.metrics.increment code else data = nil @@ -95,14 +103,33 @@ def ifsc_data(code) end get '/' do - readme = File.read 'README.md' - erb :index, locals: { text: markdown(readme) } + erb :index end get '/metrics' do settings.metrics.format end + +get '/:code.html', %r{/[\w-]+/[\w-]+/(?[A-Z0-9]{11})} do + begin + data = ifsc_data(params['code']) + erb :ifsc, locals: { data: data } + rescue StandardError => e + status 404 + json 'Not Found' + end +end + +get '/banks/:code' do + begin + return json IFSCPlus::Bank.get_details params['code'] + rescue Exception => e + status 404 + json 'Not Found' + end +end + get '/:code' do begin data = ifsc_data(params['code']) @@ -115,6 +142,6 @@ def ifsc_data(code) rescue StandardError => e puts e status 404 - json 'Not Found' + json "Not Found" end end diff --git a/entrypoint.sh b/entrypoint.sh index 7a766ed8..0886bdfe 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,4 @@ #!/usr/bin/dumb-init /bin/sh cd /app redis-server --daemonize yes -bundle exec rackup --host 0.0.0.0 --port 3000 \ No newline at end of file +bundle exec rackup --host 0.0.0.0 --port 3000 diff --git a/init.rb b/init.rb index b39e1cf5..2ff0b927 100644 --- a/init.rb +++ b/init.rb @@ -1,6 +1,8 @@ require 'json' require 'redis' require 'benchmark' +require 'to_slug' +require 'sitemap_generator' redis = Redis.new @@ -8,24 +10,50 @@ def log(msg) puts "[+] (#{Time.now.strftime('%r')}) #{msg}" end +def add_to_sitemap(sitemap, ifsc, data) + url = nil + if data['BANK'] && data['BRANCH'] + url = "/#{data['BANK'].to_s.to_slug}/#{data['BRANCH'].to_s.to_slug}/#{ifsc}" + end + if url + config = { lastmod: Time.now, changefreq: 'monthly', priority: 0.3 } + sitemap.add url, config + end +end + Benchmark.bm(18) do |bm| bm.report('Ingest:') do - Dir.glob('data/*.json') do |file| - log "Processing #{file}" - bank = File.basename file, '.json' - if Regexp.new('[A-Z]{4}').match(bank) - data = JSON.parse File.read file - data.each do |ifsc, d| - # Remove the extra keys from the JSON files - d.delete_if { |key| %w[BANK IFSC].include? key } - redis.hmset ifsc, *d + # CHANGE TO .com before merging + SitemapGenerator::Sitemap.default_host = 'https://ifsc.stage.razorpay.in' + SitemapGenerator::Sitemap.create_index = true + + SitemapGenerator::Sitemap.create do |s| + s.add '/', changefreq: 'daily', priority: 0.9 + Dir.glob('data/*.json') do |file| + log "Processing #{file}" + bank = File.basename file, '.json' + if Regexp.new('[A-Z]{4}').match(bank) + data = JSON.parse File.read file + data.each do |ifsc, d| + begin + add_to_sitemap(s, ifsc, d) + rescue Exception => e + puts e + end + + # Remove the extra keys from the JSON files + d.delete_if { |key| %w[BANK IFSC].include? key } + redis.hmset ifsc, *d + end + log "Processed #{data.size} entries" end - log "Processed #{data.size} entries" end end end + bm.report('Dump:') do redis.save end end + log('Data saved to Redis') diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 00000000..4b3c0d9e --- /dev/null +++ b/public/robots.txt @@ -0,0 +1 @@ +Sitemap: https://ifsc.stage.razorpay.in/sitemap.xml.gz diff --git a/views/ifsc.erb b/views/ifsc.erb new file mode 100644 index 00000000..be549026 --- /dev/null +++ b/views/ifsc.erb @@ -0,0 +1,66 @@ + + + + + Razorpay IFSC Toolkit + + + + + + +
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IFSC<%= data['IFSC']%>
Address<%= data['ADDRESS']%>
Contact<%= data['CONTACT']%>
City<%= data['CITY']%>
District<%= data['DISTRICT']%>
State<%= data['STATE']%>
RTGS<%= data['RTGS'] ? "Yes" : "No" %>
Bank<%= data['BANK']%> (<%=data['BANKCODE']%>)
+
+ +