1- alias Cache.Registry , as: Cache
2-
31defmodule MuCachePlug do
2+ alias Cache.Registry , as: Cache
3+
44 @ moduledoc """
55 Router for receiving cache requests.
66 """
@@ -10,6 +10,17 @@ defmodule MuCachePlug do
1010 plug ( :match )
1111 plug ( :dispatch )
1212
13+ @ request_manipulators [ ]
14+ @ response_manipulators [
15+ Manipulators.CacheKeyLogger ,
16+ Manipulators.StoreResponse ,
17+ Manipulators.RemoveCacheRelatedKeys
18+ ]
19+ @ manipulators ProxyManipulatorSettings . make_settings (
20+ @ request_manipulators ,
21+ @ response_manipulators
22+ )
23+
1324 get "/cachecanhasresponse" do
1425 send_resp ( conn , 200 , "debugging is a go" )
1526 end
@@ -20,21 +31,22 @@ defmodule MuCachePlug do
2031 |> Enum . map ( & Poison . decode! / 1 )
2132 # remove nil values
2233 |> Enum . filter ( & & 1 )
23- |> Enum . map ( & maybe_log_clear_keys / 1 )
34+ |> Enum . map ( & maybe_log_delta_clear_keys / 1 )
2435 |> Enum . map ( & Cache . clear_keys / 1 )
2536
2637 Plug.Conn . send_resp ( conn , 204 , "" )
2738 end
2839
2940 match "/*path" do
30- full_path = Enum . reduce ( path , "" , fn a , b -> b <> "/" <> a end )
41+ full_path = conn . request_path
3142 known_allowed_groups = get_string_header ( conn . req_headers , "mu-auth-allowed-groups" )
3243 conn = Plug.Conn . fetch_query_params ( conn )
3344
3445 cond do
3546 known_allowed_groups == nil ->
3647 # without allowed groups, we don't know the access rights
37- calculate_response_from_backend ( full_path , conn )
48+ # calculate_response_from_backend(full_path, conn)
49+ ConnectionForwarder . forward ( conn , path , "http://backend/" , @ manipulators )
3850
3951 cached_value =
4052 Cache . find_cache ( { conn . method , full_path , conn . query_string , known_allowed_groups } ) ->
@@ -43,93 +55,11 @@ defmodule MuCachePlug do
4355
4456 true ->
4557 # without a cache, we should consult the backend
46- IO . puts ( "Cache miss" )
47- calculate_response_from_backend ( full_path , conn )
48- end
49- end
50-
51- defp maybe_log_clear_keys ( clear_keys ) do
52- if Application . get_env ( :mu_cache , :log_clear_keys ) do
53- # credo:disable-for-next-line Credo.Check.Warning.IoInspect
54- IO . inspect ( clear_keys , label: "Clear keys" )
55- end
56-
57- clear_keys
58- end
58+ # IO.inspect(
59+ # {conn.method, full_path, conn.query_string, known_allowed_groups}, label: "Cache miss for signature")
5960
60- defp maybe_log_cache_keys ( cache_keys ) do
61- if Application . get_env ( :mu_cache , :log_cache_keys ) do
62- # credo:disable-for-next-line Credo.Check.Warning.IoInspect
63- IO . inspect ( cache_keys , label: "Cache keys" )
61+ ConnectionForwarder . forward ( conn , path , "http://backend/" , @ manipulators )
6462 end
65-
66- cache_keys
67- end
68-
69- @ spec calculate_response_from_backend ( String . t ( ) , Plug.Conn . t ( ) ) :: Plug.Conn . t ( )
70- defp calculate_response_from_backend ( full_path , conn ) do
71- # Full path starts with /
72- url = "http://backend" <> full_path
73-
74- url =
75- case conn . query_string do
76- "" -> url
77- query -> url <> "?" <> query
78- end
79-
80- opts = PlugProxy . init ( url: url )
81-
82- processors = % {
83- header_processor: fn headers , _conn , state ->
84- headers = downcase_headers ( headers )
85-
86- { headers , cache_keys } = extract_json_header ( headers , "cache-keys" )
87- { headers , clear_keys } = extract_json_header ( headers , "clear-keys" )
88-
89- maybe_log_cache_keys ( cache_keys )
90- maybe_log_clear_keys ( clear_keys )
91-
92- { headers ,
93- % {
94- state
95- | headers: headers ,
96- allowed_groups: get_string_header ( headers , "mu-auth-allowed-groups" ) ,
97- cache_keys: cache_keys ,
98- clear_keys: clear_keys
99- } }
100- end ,
101- chunk_processor: fn chunk , state ->
102- # IO.puts "Received chunk:"
103- # IO.inspect chunk
104- { chunk , % { state | body: state . body <> chunk } }
105- end ,
106- body_processor: fn body , state ->
107- # IO.puts "Received body:"
108- # IO.inspect body
109- { body , % { state | body: state . body <> body } }
110- end ,
111- finish_hook: fn state ->
112- # IO.puts "Fully received body"
113- # IO.puts state.body
114- # IO.puts "Current state:"
115- # IO.inspect state
116- Cache . store ( { conn . method , full_path , conn . query_string , state . allowed_groups } , state )
117- { true , state }
118- end ,
119- state: % {
120- is_processor_state: true ,
121- body: "" ,
122- headers: % { } ,
123- status_code: 200 ,
124- cache_keys: [ ] ,
125- clear_keys: [ ] ,
126- allowed_groups: nil
127- }
128- }
129-
130- conn
131- |> Map . put ( :processors , processors )
132- |> PlugProxy . call ( opts )
13363 end
13464
13565 defp respond_with_cache ( conn , cached_value ) do
@@ -138,30 +68,16 @@ defmodule MuCachePlug do
13868 |> send_resp ( 200 , cached_value . body )
13969 end
14070
141- defp extract_json_header ( headers , header_name ) do
142- case List . keyfind ( headers , header_name , 0 ) do
143- { ^ header_name , keys } ->
144- new_headers = List . keydelete ( headers , header_name , 0 )
145- { new_headers , Poison . decode! ( keys ) }
146-
147- _ ->
148- { headers , [ ] }
71+ defp maybe_log_delta_clear_keys ( clear_keys ) do
72+ if Application . get_env ( :mu_cache , :log_clear_keys ) do
73+ # credo:disable-for-next-line Credo.Check.Warning.IoInspect
74+ IO . inspect ( clear_keys , label: "Clear keys" )
14975 end
15076 end
15177
15278 defp get_string_header ( headers , header_name ) do
153- case List . keyfind ( headers , header_name , 0 ) do
154- { ^ header_name , string } ->
155- string
156-
157- _ ->
158- nil
159- end
160- end
161-
162- defp downcase_headers ( headers ) do
163- Enum . map ( headers , fn { header , content } ->
164- { String . downcase ( header ) , content }
165- end )
79+ headers
80+ |> List . keyfind ( header_name , 0 , { nil , nil } )
81+ |> elem ( 1 )
16682 end
16783end
0 commit comments