Last active
August 29, 2015 14:14
-
-
Save vdel26/bf1c65e524f494faa384 to your computer and use it in GitHub Desktop.
Inject extra fields to API backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| upstream threescale_admin_api { | |
| # 3scale adminstration API | |
| server MYCOMPANY-admin.3scale.net:443 max_fails=5 fail_timeout=30; | |
| } | |
| location = /threescale_appfind { | |
| internal; | |
| set $provider_key "PROVIDERKEY"; | |
| proxy_pass "https://threescale_admin_api/admin/api/applications/find.xml?provider_key=$provider_key&app_id=$app_id"; | |
| proxy_set_header Host "MYCOMPANY-admin.3scale.net"; | |
| } | |
| location / { | |
| # below the other variable initializations | |
| set $app_id null; | |
| set $organizationId null; | |
| set $brandId null; | |
| ## add these lines below the other "proxy_set_header" lines | |
| proxy_set_header X-Org-Id $organizationId; | |
| proxy_set_header X-Brand-Id $brandId; | |
| ## | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function set_app_info(app_id) | |
| local metadata = ngx.shared.metadata | |
| local app_data = metadata:get(app_id) | |
| local brandId, organizationId | |
| if app_data then | |
| local idx = app_data:find(":") | |
| if idx then | |
| brandId = app_data:sub(1, idx-1) | |
| organizationId = app_data:sub(idx+1) | |
| end | |
| else | |
| local res = ngx.location.capture("/threescale_appfind", { share_all_vars = true }) | |
| local m1, err1 = ngx.re.match(res.body, [[<brandId>(\S+)<\/brandId>]]) | |
| local m2, err2 = ngx.re.match(res.body, [[<organizationId>(\S+)<\/organizationId>]]) | |
| if m1 then brandId = m1[1] end | |
| if m2 then organizationId = m2[1] end | |
| brandId = brandId or "none" | |
| organizationId = organizationId or "none" | |
| -- the last parameter is the expiration time in seconds (0=no expiration) | |
| -- it is recommended to set an expiration time to sync any metadata changes | |
| metadata:set(app_id, brandId .. ":" .. organizationId, 0) | |
| end | |
| ngx.var.brandId = brandId | |
| ngx.var.organizationId = organizationId | |
| end | |
| -- call set_app_info(ngx.var.app_id) at the end of the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment