Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active October 2, 2018 06:31
Show Gist options
  • Save nicholastay/a55da34873bdb7524234b3f3bac2bab5 to your computer and use it in GitHub Desktop.
Save nicholastay/a55da34873bdb7524234b3f3bac2bab5 to your computer and use it in GitHub Desktop.
Firebase short URL GAE proxy prototype
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
libraries:
- name: flask
version: 0.12
import logging
import json
from flask import Flask, request, redirect
from google.appengine.api import urlfetch
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
@app.route('/<shortcode>')
def code_redir(shortcode):
# !! Customise / add domains here if this gae is to be used for redirecting as well
if 'nexerq.cf' in request.url_root:
return redirect('https://nexerq.page.link/' + shortcode)
return 'invalid shortcode'
@app.route('/shorten')
def shorten():
if not ('k' in request.args and 'n' in request.args and 'u' in request.args and 'd' in request.args):
return 'Invalid request', 400
if request.args['n'] != 'code_thing': # !! Add secret code here as well if want or remove - don't want random people using my proxy for their own firebase
return 'Invalid auth', 401
payload = {
'dynamicLinkInfo': {
'dynamicLinkDomain': request.args['d'],
'link': request.args['u'],
},
'suffix': {
'option': 'SHORT'
}
}
try:
result = urlfetch.fetch(
url='https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=' + request.args['k'],
method=urlfetch.POST,
validate_certificate=True, # https
headers={ 'Content-Type': 'application/json' },
payload=json.dumps(payload))
if result.status_code != 200:
return result.content, result.status_code
data = json.loads(result.content)
return data['shortLink']
except urlfetch.Error:
logging.exception('Fetch or parse error')
return 'ISE Fetch error', 500
@app.errorhandler(500)
def server_error(e):
logging.exception('Error')
return '500 ISE', 500

To shorten: GET /shorten with args.

  • k = Firebase API key
  • n = Secret code (if desired) to use the GAE API
  • d = Shortlink domain
  • u = URL to be shortened

Returns full shortened URL in plaintext with given domain.

Can replace the domain (https://.../) with custom domain if wanted (GAE domain if configured).

{
"Name": "GAE Firebase DL proxy test",
"DestinationType": "URLShortener",
"RequestType": "GET",
"RequestURL": "https://<appspot_url>/shorten",
"Arguments": {
"k": "<firebase_api_key>",
"n": "<private_api_key>",
"d": "<shortlink_domain>",
"u": "$input$"
},
"RegexList": [
"([^\/]+$)"
],
"URL": "https://<custom_proxy_domain>/$1,1$"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment