Skip to content

Instantly share code, notes, and snippets.

View allanlei's full-sized avatar
:shipit:
What's up?

Allan Lei allanlei

:shipit:
What's up?
View GitHub Profile
@allanlei
allanlei / utils.py
Created June 24, 2016 06:53
Configuration helpers for flask-cache
from six.moves import urllib
def config(uri):
parsed = urllib.parse.urlparse(uri)
options = dict(urllib.parse.parse_qsl(parsed.query))
cache_type = parsed.scheme.lower()
if cache_type not in ['null', 'simple', 'file', 'memcached', 'saslmemcached', 'redis']:
raise Exception('Misconfiguration')
// "use strict";
// var minimatch = require('minimatch');
// var replaceall = require('replaceall');
//
//
// function Authorizer(acl) {
// this.acl = acl || [];
// }
// module.exports = Authorizer;
//
@allanlei
allanlei / elb-acm-ssl.config
Last active March 14, 2016 03:16
Configures AWS Elastic Beanstalk with ACM SSL. Add to .ebextensions/
option_settings:
- namespace: aws:elb:listener:443
option_name: ListenerProtocol
value: HTTPS
- namespace: aws:elb:listener:443
option_name: SSLCertificateId
value: YOUR-ARN-HERE
- namespace: aws:elb:listener:443
option_name: InstancePort
value: 80
@allanlei
allanlei / elb-acm-ssl.config
Created March 14, 2016 03:16
Configures AWS Elastic Beanstalk with ACM SSL.
option_settings:
- namespace: aws:elb:listener:443
option_name: ListenerProtocol
value: HTTPS
- namespace: aws:elb:listener:443
option_name: SSLCertificateId
value: YOUR-ARN-HERE
- namespace: aws:elb:listener:443
option_name: InstancePort
value: 80
@allanlei
allanlei / publish.py
Created February 26, 2016 05:09
MQTT Example
import paho.mqtt.client as mqtt
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.connect('iot.eclipse.org', port=1883, keepalive=60)
client.publish('update', 'hello world')
@allanlei
allanlei / decorators.py
Created January 15, 2016 15:39
Vary decorator for Flask
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from flask import after_this_request
import decorator
def vary(*headers):
def _vary(f, *args, **kwargs):
@allanlei
allanlei / resolvers.py
Created January 7, 2016 08:26
Reverse url_for for Flask
# REF: http://stackoverflow.com/a/19637175/1198679
from flask.globals import _app_ctx_stack, _request_ctx_stack
from werkzeug.urls import url_parse
def route_from(url, method = None):
appctx = _app_ctx_stack.top
reqctx = _request_ctx_stack.top
if appctx is None:
raise RuntimeError('Attempted to match a URL without the '
'application context being pushed. This has to be '
@allanlei
allanlei / static.py
Last active November 3, 2015 08:09
Unicode code support for filenames using twisted.web.static.File
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from urllib import quote, unquote
from twisted.web import static
from twisted.web.static import getTypeAndEncoding, formatFileSize
from twisted.python import filepath
from twisted.python.compat import escape
@allanlei
allanlei / vhost.py
Last active October 31, 2015 15:12
Twisted HTTP server example with vhosts and HTTPS
# -*- coding: utf-8 -*-
"""
Usage: twistd -ny vhost.py
"""
from __future__ import absolute_import, division, unicode_literals, print_function
import os
from twisted.application import internet, service
@allanlei
allanlei / notifySlack.js
Created August 31, 2015 03:14
AWS Lambda Slack notification via SNS events
const https = require('https');
const url = require('url');
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration
const slack_url = 'https://hooks.slack.com/services/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';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {'Content-Type': 'application/json'};
exports.handler = function(event, context) {
(event.Records || []).forEach(function (rec) {