Skip to content

Instantly share code, notes, and snippets.

@vfeskov
vfeskov / handler.rb
Last active February 1, 2018 23:38
Event machine
# frozen_string_literal: true
require 'redis'
class Handler
DB = Redis.new
def initialize(headers, client)
@headers = headers
@client = client
end
@vfeskov
vfeskov / handler.rb
Last active February 1, 2018 23:36
New thread on each request
# frozen_string_literal: true
require 'redis'
class Handler
DB = Redis.new
def initialize(client, request, db)
@client = client
@request = request
end
@vfeskov
vfeskov / index.html
Last active February 15, 2022 21:43
Minimal Vue + Webpack project
<!doctype html>
<html>
<body>
<a id="link" v-on:click="clickEvent" href="#">A</a>
<script src="bundle.js"/>
</body>
</html>
@vfeskov
vfeskov / server.js
Last active January 3, 2018 19:48
Redirecting HTTP to HTTPS in Node.js
// *.pem files were generated with:
// openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -nodes -days 365
const http = require('http')
const https = require('https')
const fs = require('fs')
http.createServer((req, res) => {
res.writeHead(301, { Location: `https://localhost${req.url}` })
res.end()
/**
* Calculates weighted average with latter values weighing more.
*
* Weights for each value are calculated using https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Exponentially_decreasing_weights
*
* The average itself is calculated by summing up each value multiplied by its weight: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Convex_combination_example
*
* @param {number|Array<number>} values Values, of which to calculate average
* @param {number} delta Delta, 0 < delta < 1, basically, how much weight the latest value will have when calculating average
* @return The input multiplied by 2.