Skip to content

Instantly share code, notes, and snippets.

View vdel26's full-sized avatar

Victor Delgado vdel26

View GitHub Profile
@vdel26
vdel26 / heroku-blog-post.md
Last active August 29, 2015 14:02
API Management with 3scale as an Heroku add-on for your application

API Management with 3scale as an Heroku add-on for your application

Hosting your API in a platform as a service like Heroku is a popular option nowadays since it frees you from all the infrastructure administration tasks and allows you to focus on development. It is also a really good way to launch your API when you are not sure what the usage will be like, as Heroku will handle scaling your application server for you.

If that is your case, now you can add 3scale to secure and manage your API right from your Heroku dashboard, by using 3scale Heroku add-on.

Provisioning the add-on for your Heroku app is as simple as doing a simple call from the command line:

heroku addons:add threescale
@vdel26
vdel26 / nginx.conf
Last active August 29, 2015 14:04
Error 404 and report if app plan doesn't match
location = /threescale_report {
internal;
set $provider_key "PROVIDERKEY";
proxy_pass http://threescale_backend/transactions.xml;
proxy_set_header Host "su1.3scale.net";
}
@vdel26
vdel26 / nginx.lua
Last active August 29, 2015 14:04
Use prebuilt cache in case 3scale cannot be reached
local whitelist = require "whitelist"
function authrep(params, service)
ngx.var.cached_key = ngx.var.cached_key .. ":" .. ngx.var.usage
local api_keys = ngx.shared.api_keys
local is_known = api_keys:get(ngx.var.cached_key)
if is_known ~= 200 then
local res = ngx.location.capture("/threescale_authrep", { share_all_vars = true })
---
THEME_ACCT: vdel26
COMMON_GIT_MIRROR: 'github.com'
##
# deploying from an edx fork
#
# GIT_ACCT: vdel26
# edx_platform_repo: https://{{ COMMON_GIT_MIRROR }}/{{ GIT_ACCT }}/edx-platform.git
# edx_platform_version: release
@vdel26
vdel26 / nginx.conf
Last active August 29, 2015 14:05
Bypass 3scale depending on incoming IP
upstream backend_api {
# service name:API ;
server api.domain.com:80 max_fails=5 fail_timeout=30;
}
#### add this anywhere inside your http block
geo $match {
default 0;
## set IPs or IP groups that will be sent directly to your API
@vdel26
vdel26 / nginx.lua
Created September 8, 2014 15:29
detect 409 from 3scale
function error_authorization_failed(service)
ngx.status = 409
ngx.header.content_type = service.auth_failed_headers
ngx.print("Limit exceeded")
ngx.exit(ngx.HTTP_OK)
end
function authrep(params, service)
ngx.var.cached_key = ngx.var.cached_key .. ":" .. ngx.var.usage
local api_keys = ngx.shared.api_keys
@vdel26
vdel26 / nginx.conf
Last active August 29, 2015 14:10
Send Application plan as a header named X-3scale-Plan
...
location / {
set $provider_key null;
set $cached_key null;
set $credentials null;
set $usage null;
set $service_id YOURSERVICEID;
set $proxy_pass null;
set $secret_token null;
set $plan_header null; # ADD THIS LINE
@vdel26
vdel26 / index.html
Last active August 29, 2015 14:11
Update button workaround
<section>
<!-- BUTTON -->
<!-- add this where you would put the tag {{ 'Regenerate' | update_button: application.update_user_key_url }} -->
<form action="{{ application.update_user_key_url }}" class="button_to js-regenerateButton" method="post">
<div>
<input name="_method" type="hidden" value="put">
<input class="btn btn-danger btn-xs" type="submit" value="Regenerate">
</div>
</form>
<!-- ENDBUTTON -->
@vdel26
vdel26 / nginx-ip.conf
Last active February 26, 2021 18:57
Referrer filter detection in 3scale API proxy
# ENABLING REFERRER FILTERING IN NGINX – by IP address
# Using this snippet, 3scale will check if the origin of the API call
# matches the one introduce for that application by the developer
# Nginx will report to 3scale the origin IP address.
# replace the following location block in your nginx.conf file
location = /threescale_authrep {
internal;
set $provider_key "YOUR_PROVIDER_KEY";
@vdel26
vdel26 / Editable-table.markdown
Last active August 29, 2015 14:13
Editable table