-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.rb
64 lines (53 loc) · 1.5 KB
/
server.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
# frozen_string_literal: true
require 'sinatra'
require 'site-inspector'
require 'json'
require 'rack-cache'
require 'tilt/erb'
require 'cgi'
require 'urlscan'
require 'dotenv/load'
require 'rack/ecg'
GLOBAL_CACHE_TIMEOUT = 30
module SiteInspectorServer
class App < Sinatra::Base
configure :production do
require 'rack-ssl-enforcer'
use Rack::SslEnforcer
end
use Rack::ECG, checks: [
[:static, { name: 'environment', value: Sinatra::Application.environment }],
]
helpers SiteInspector::Formatter
helpers do
def slugify(word)
word.to_s.downcase.tr(' ', '-')
end
end
def render_template(template, locals = {})
halt erb template, layout: :layout, locals: locals
end
def urlscan_client
@urlscan_client ||= UrlScan::API.new
end
def urlscan(domain)
urlscan_client.submit(domain.canonical_endpoint, visibility: 'private')
rescue UrlScan::ProcessingError, UrlScan::RateLimited
nil
end
get '/' do
render_template :index
end
get '/domains/:domain.json' do
cache_control :public, max_age: GLOBAL_CACHE_TIMEOUT
content_type :json
domain = SiteInspector.inspect params[:domain]
domain.to_h.to_json
end
get '/domains/:domain' do
cache_control :public, max_age: GLOBAL_CACHE_TIMEOUT
domain = SiteInspector.inspect params[:domain]
render_template :domain, domain: domain, endpoint: domain.canonical_endpoint, urlscan: urlscan(domain)
end
end
end