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 / step2_osm-vector-tiles.sh
Created July 27, 2021 10:00
Install tooling and kick of the conversion process
# We need make in order for openmaptiles to work correctly
sudo apt-get install -y make
# get the openmaptiles tooling
git clone https://github.com/openmaptiles/openmaptiles.git
cd ./openmaptiles
# We've decided to only get the mapdata until zoom level 12. It's a good idea to check first which zoom levels you'll need. Eveything higher then 12 take a lot of time and space
sed -i "/MAX_ZOOM=/c\MAX_ZOOM=12" .env
# Prep openmaptiles and download the raw OSM data for europe