Skip to content

Instantly share code, notes, and snippets.

@learncodeacademy
learncodeacademy / async.md
Last active August 29, 2015 14:05
Async Generator Tests

###Doing Express.js Async With Generators###

Using Co library, run generators as responses by wrapping Co and adding an extra function to handle errors

  • Because Co always runs the last argument as a callback...so it would always runs next() by default
  • This function only runs next if there's an error
//assign this to the Co module somewhere in application bootstrapping, 
//so you can do var ce = require('co').coExpress in each router file
co.coExpress = (generator) => {
  return (...args) => {
@learncodeacademy
learncodeacademy / keymap.c
Created October 10, 2016 14:39
Ergodox EZ Keymap - layer 2: mouse - layer3: arrows
#include "ergodox_ez.h"
#include "debug.h"
#include "action_layer.h"
#define BASE 0 // default layer
#define MDIA 1 // mouse/media keys
#define ARROW 2 // arrow keys
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
/* Keymap 0: Basic layer
@learncodeacademy
learncodeacademy / Dockerfile
Created March 27, 2015 02:20
Node Dockerfile
FROM node:0.12.0
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /tmp/package.json
RUN cd /tmp && mkdir data && npm install --production --unsafe-perm
RUN mkdir -p /app && cp -a /tmp/node_modules /app/ && cp -a /tmp/data /app/
# From here we load our application's code in, therefore the previous docker
# "layer" thats been cached will be used if possible

##The Good, The Bad, & The Ugly Ways of handling Async Operations With Javascript## #####Callbacks < Promises < Generators#####


###An Example: 5 in-sequence Async Operations### (also see parallel-sequence example: https://gist.github.com/willrstern/af3a3308fc5864cf48f8)
###The Ugly Way: Callbacks### After each function takes place, handle any errors & do the next thing - It's easy to walk through the code and understand what's going on...but it's ugly as sin
@learncodeacademy
learncodeacademy / .drone.yml
Created June 28, 2017 16:28
Rancher Drone CD Pipeline Setup
pipeline:
slack:
image: plugins/slack
webhook: <your slack webhook url>
channel: deployments
username: drone
template: "<{{build.link}}|Deployment #{{build.number}} started> on <http://github.com/{{repo.owner}}/{{repo.name}}/tree/{{build.branch}}|{{repo.name}}:{{build.branch}}> by {{build.author}}"
when:
branch: [ master, staging ]
build:
@learncodeacademy
learncodeacademy / README.md
Last active June 17, 2021 15:43
Running a High Availability Service on CoreOS using Docker, Fleet, Flannel, Etcd, Confd & Nginx

Running a High Availability Service on CoreOS using Docker, Fleet, Flannel, Etcd, Confd & Nginx

Tools used:

  • coreos: server machine clustering via a shared cloud-config.yml
  • etcd: key value store for service registration and discovery
  • fleet: scheduling/failover of docker containers across coreos cluster
  • flannel: Gives each docker container a unique ip that allows you to access the internal port (i.e. port 80 not 32679)
  • confd: watch etcd for nodes arriving/leaving - template nginx configuration files / reload nginx on change
@learncodeacademy
learncodeacademy / gist:8acf7e3a2c4c33100f04c6715c662a01
Created June 11, 2018 14:23
Training a Neural Network - Enhanced Console Output From Brain.js
Start Training!...Here's the data, we'll do 2 iterations:
[ { input: [ 0, 1 ], output: [ 1, 0 ] },
{ input: [ 1, 1 ], output: [ 1, 1 ] } ]
======== TRAINING ITERATION 1 =========
--------- Run input set 0: 0,1 ----------
-> Layer 2 has 3 nodes
START NODE: 0
-> bias for node 0: 0.13861538469791412
-> weights for node 0:
-> input value: 0, weight: -0.03485306352376938

##Example Task: Make 2 parallel (simultaneous) async calls, then one async call when they've resolved##

##The Good Way: Promises##

//with bluebird library
var parallelCalls = [
  getUrl('/api/profile'),
  getUrl('/api/accounts')
];
//spread is like .then(), except it apply()s the array of responses as individual arguments
@learncodeacademy
learncodeacademy / node-deploy-as-upstart-service.md
Last active August 16, 2022 17:35
Deploy Node.js app on Ubuntu as Upstart Service - instead of using Forever

Deploying a node app with Forever is great...until your server restarts unexpectedly. Then your app stops running and you have to re-deploy.

To get around this, we're going to run our node app as an Upstart service. Upstart services are great, because, once started, the system auto-restarts them if they fail, or if the server restarts.

###Step 1: Create a service for your node app

  • ssh in as root ssh root@youripaddress
  • Create a node-app.conf file in /etc/init
    IMPORTANT: whatever filename you pick is what you will use to start|stop|restart your service i.e. service node-app start
@learncodeacademy
learncodeacademy / flightplan-deploy.md
Last active January 7, 2024 11:58
Deploy Node.js Apps with Flightplan

##Setup your server (this would ideally be done with automated provisioning)

  • add a deploy user with password-less ssh see this gist
  • install forever npm install -g forever

##Install flightplan

  • npm install -g flightplan
  • in your project folder npm install flightplan --save-dev
  • create a flightplan.js file