Skip to content

Instantly share code, notes, and snippets.

View danielreiser's full-sized avatar

Daniel Reiser danielreiser

View GitHub Profile
@danielreiser
danielreiser / cf-origin-switch_origin-response.js
Created August 10, 2021 09:29
Origin Response lambda@edge function for setting a cookie
'use strict';
const AWS = require('aws-sdk');
const ssm = new AWS.SSM({ region: 'eu-central-1' });
const SOURCE_COOKIE_NAME = 'myapp-origin';
const LATEST_ORIGIN_GIT_HASH = 'myapp_latest-origin-git-hash';
exports.handler = async event => {
console.group('[INFO] lambda@edge Origin Response')
@danielreiser
danielreiser / cf-origin-switch-cookie-and-header_origin-request.js
Created August 10, 2021 09:26
Origin Request lambda@edge function for cookie and header evaluation
@danielreiser
danielreiser / cf-origin-switch-header_origin-request.js
Last active August 10, 2021 09:25
Origin Request lambda@edge function for header evaluation
'use strict';
const SOURCE_HEADER_NAME = 'x-myapp-origin';
exports.handler = async event => {
console.group('[INFO] lambda@edge Origin Request')
const request = event.Records[0].cf.request;
if (request.headers[SOURCE_HEADER_NAME]) {
const headerValue = request.headers[SOURCE_HEADER_NAME][0].value;
@danielreiser
danielreiser / cf-origin-switch_viewer-request.js
Created August 10, 2021 09:06
Viewer Request lambda@edge function
'use strict';
const AWS = require('aws-sdk');
const ssm = new AWS.SSM({ region: 'eu-central-1' });
const SOURCE_COOKIE_NAME = 'myapp-origin';
const LATEST_ORIGIN_GIT_HASH = 'myapp_latest-origin-git-hash';
exports.handler = async (event, context, callback) => {
console.group('[INFO] lambda@edge Viewer Request')
@danielreiser
danielreiser / levenshtein-redirect-404.guard.ts
Created August 7, 2021 15:00
Angular 2+ Route Guard to correct user typos in urls with the help of the levenshtein distance calculation. Full repo can be found here https://github.com/danielreiser/medium-levenshtein-404-redirect
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'
import { Observable } from 'rxjs'
import { distance } from 'fastest-levenshtein'
import { paths } from '../paths'
@Injectable({
providedIn: 'root'
})
export class Redirect404Guard implements CanActivate {
@danielreiser
danielreiser / leevenshtein-404-approach-b.helper.js
Last active August 7, 2021 14:14
Snippet of the levenshtein distance calculator top correct a misspelled route - Approach B
const { distance } = require('fastest-levenshtein')
// Approach B - Using distance which gives us more control in conjunction with a MAX_DISTANCE value
const routes = {
paymentMethods: 'paymentmethods',
accountData: 'account-data',
profile: 'profile',
privacySettings: 'privacy-settings',
}
@danielreiser
danielreiser / leevenshtein-404-approach-a.helper.js
Last active August 3, 2021 16:35
Snippet of the levenshtein distance calculator top correct a misspelled route - Approach A
const { closest, distance } = require('fastest-levenshtein')
// Approach A - Using closest
const routes = {
paymentMethods: 'paymentmethods',
accountData: 'account-data',
privacy: 'privacy',
privacySettings: 'privacy-settings',
}
@danielreiser
danielreiser / osm-ungzip-vector-tiles.sh
Created July 30, 2021 06:53
Ungzip the extracted vector files from mbutil
# Un-gzip them
gzip -d -r -S .pbf *
# Rename them (this also renames all non-pbf files)
find . -type f -exec mv '{}' '{}'.pbf \;
# As i said, it renames all files so we have to remove the suffix once more
mv metadata.json{.pbf,}
@danielreiser
danielreiser / osm-extract-vector-tiles-from-mbutils.sh
Last active July 30, 2021 06:52
Extract vector tiles from a MBTiles file
cd data/
sudo apt-get install -y python
git clone https://github.com/mapbox/mbutil.git
./mbutil/mb-util --image_format=pbf tiles.mbtiles tiles
cd ../tiles/
@danielreiser
danielreiser / step1_osm-vector-tiles.sh
Last active July 27, 2021 10:01
Install docker and docker-compose on an ubuntu based EC2 instance in AWS
# Update all package lists and update ubuntu
sudo apt-get -y update
sudo apt-get -y dist-upgrade
# Add the docker repository to make it available via apt-get install and update the package lists again
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io