Skip to content

Instantly share code, notes, and snippets.

View mikaelvesavuori's full-sized avatar

Mikael Vesavuori mikaelvesavuori

View GitHub Profile
@mikaelvesavuori
mikaelvesavuori / calculateDistance.mjs
Created February 9, 2019 15:21
Calculate distance between two positions, given as object with LAT and LONG coordinates
// Entirely based on answer given in: https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula
export default function calculateDistance(position1, position2) {
//lon1, lat1, lon2, lat2
const R = 6371; // Radius of the earth in km
const dLat = (position2.latitude - position1.latitude).toRad();
const dLon = (position2.longitude - position1.longitude).toRad();
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(position1.latitude.toRad()) *
@mikaelvesavuori
mikaelvesavuori / renderImagePreviews.mjs
Created February 9, 2019 15:25
Render image previews with FileReader, based on a given array of files (buffers)
// Based on something I found on the internet, don't remember where
export default function renderImagePreviews(files) {
for (let i = 0, f; (f = files[i]); i++) {
if (!f.type.match('image.*')) {
continue;
}
const reader = new FileReader();
@mikaelvesavuori
mikaelvesavuori / indexServer.jsx
Created February 9, 2019 15:26
SSR React base using Unistore (keeping entirely as reminder)
import React from 'react';
import { StaticRouter } from 'react-router';
import { createStore, Provider } from 'unistore/full/react';
import Routes from 'routes/index';
import persistStore from 'unissist';
import localStorageAdapter from 'unissist/integrations/localStorageAdapter';
// Use let rather than const so we can send props in class constructor
let store = createStore({});
const adapter = localStorageAdapter();
@mikaelvesavuori
mikaelvesavuori / countries.js
Created February 9, 2019 15:27
Just an array with all the world's countries in case you need to make a huge <select> list or something
const countries = [
"Afghanistan",
"Åland Islands",
"Albania",
"Algeria",
"American Samoa",
"Andorra",
"Angola",
"Anguilla",
"Antarctica",
@mikaelvesavuori
mikaelvesavuori / countriesEurope.js
Created February 9, 2019 15:28
A list of all the European countries, for your huge-ass <select> dropdowns or what-not
const countriesEurope = [
"Åland Islands",
"Albania",
"Andorra",
"Armenia",
"Austria",
"Azerbaijan",
"Belarus",
"Belgium",
"Bosnia and Herzegovina",
@mikaelvesavuori
mikaelvesavuori / localhost-certificate.sh
Created February 27, 2019 08:34
HTTPS Certificate for Localhost
# From: https://letsencrypt.org/docs/certificates-for-localhost/
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
@mikaelvesavuori
mikaelvesavuori / azure-pipelines.yml
Created November 21, 2019 20:05
Azure DevOps pipelines for building Node app and deploying static site
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
- group: Deployment variables
steps:
@mikaelvesavuori
mikaelvesavuori / azure-login.sh
Created November 22, 2019 07:25
How to actually log into Azure and setting the environment variables
az login
az account set -s ${ACCOUNT_NUMBER}
az ad sp create-for-rbac --name ${PRINCIPAL_NAME}
# Example object returned from create-for-rbac
# {
# "appId": "16c2b0ae-26a6-494c-9afd-257a8eb7b9a1",
# "displayName": "serverless-service-principal",
@mikaelvesavuori
mikaelvesavuori / vs-code-in-google-cloud-shell.sh
Created November 30, 2019 15:48
Visual Studio Code in Google Cloud Shell
# Use Visual Studio Code in Google Cloud Shell
# As posted at: https://medium.com/google-cloud/how-to-run-visual-studio-code-in-google-cloud-shell-354d125d5748
# Export version variable
export VERSION=`curl -s https://api.github.com/repos/cdr/code-server/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
# Download
wget https://github.com/cdr/code-server/releases/download/$VERSION/code-server$VERSION-linux-x64.tar.gz
# Unzip
@mikaelvesavuori
mikaelvesavuori / needed-to-add-assumerole-policy.sh
Last active December 4, 2019 09:26
What is needed to add an AssumeRole policy
#https://github.com/aws/aws-cli/issues/2279#issuecomment-262616353
export AWS_ACCOUNT_ID='123412341234';
export ROLE='SomeAccessRole';
export CLOUDFRONT_DISTRIBUTION_ID='E123123123123';
export SOME_ROLE='SomeUser';
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws