Class: Mints::PublicAPIController

Inherits:
ActionController::API
  • Object
show all
Includes:
ReverseProxy::Controller
Defined in:
lib/mints/controllers/public_api_controller.rb

Direct Known Subclasses

Api::V1::MintsPublicController

Instance Method Summary collapse

Instance Method Details

#indexObject

[View source]

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
# File 'lib/mints/controllers/public_api_controller.rb', line 9

def index
    headers = {
        'host' => "#{@host.gsub('http://', '').gsub('https://', '')}",
        'ApiKey' => "#{@api_key}", 
        'Content-Type'=> 'application/json', 
        'Accept'=> 'application/json' 
    }
    
    url_need_cache = false
    result_from_cache = false
    time = 0
    full_url = request.original_url

    if request.method == "GET"
        if @use_cache
            @redis_config['groups'].each do |group|
                group['urls'].each do |url|
                    if full_url.match url
                        time = group['time']
                        url_need_cache = true
                        break
                    end
                end
                break if url_need_cache  
            end
        end                
    end

    if url_need_cache
        if @redis_server.get(full_url)
            response = @redis_server.get(full_url)
            result_from_cache = true
            render json: response
        else 
            reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
                # Request succeded!
                config.on_response do |code, response|
                    @redis_server.setex(full_url,time,response.body)
                end
                # Request failed!
                config.on_missing do |code, response|
                    # We got a 404!
                    if code == 404
                        raise ActionController::RoutingError.new('Not Found')
                    end
                end
            end
        end
    else
        reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
            # Request failed!
            config.on_missing do |code, response|
                # We got a 404!
                if code == 404
                    raise ActionController::RoutingError.new('Not Found')
                end
            end
        end
    end
    
end