Skip to content

Instantly share code, notes, and snippets.

View snadrus's full-sized avatar

Andrew Jackson (Ajax) snadrus

  • Various
  • Tennessee
  • 00:28 (UTC -05:00)
View GitHub Profile
// StartServer serves Handler over HTTPS(443) with a redirect on 80
// using an ACME certificate it creates & is cached in ./certs/
// and has sensible timeouts to avoid nasty clients.
// Note the timeouts don't work for slow transfers,
// but oddly are ok for websockets.
func StartServer(engine http.Handler, ...hostname string)
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(hostname...),
Cache: autocert.DirCache("certs"),
@snadrus
snadrus / Get Latest GoLang URL & compiler
Last active February 8, 2018 21:43
This is an easy way to make sure your Golang builds are always using the latest stable (linux-amd64) compiler in one line of BASH.
Bash:
curl -Ls golang.org/dl | grep -ohm1 htt[^\"]*linux-amd64.tar.gz
Returns just the line of the latest stable linux-amd64 build, like:
https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
Make a ./go/ with the latest compiler ($GOROOT):
# Get the latest stable GoLang.
@snadrus
snadrus / Better than detect.js Browser family
Last active August 29, 2015 14:28
Use detect.js? It's a bit big for just trying to figure out what family your browser is. There's only 4 out there (2 variations of each) and they all have custom prefixed navigator objects. Lets just look for those! Detect.js-like output in 373chars (minified).
window.detect={
parse: function() {
var o = 'orientation' in window, prop, br, pre=['ms', 'moz','webkit'];
return {
isMobile: o,
browser: {
family: (o? "Mobile ":"Desktop ") + (function() {
for (prop in navigator) {
for (br in pre) {
if (!prop.indexOf(br)) { // IE 9 - Edge has "ms", else moz or webkit
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload=function() {
var header = document.querySelector('.header');
isScrolled = false;
window.addEventListener("scroll", function(ev) {
if (!document.body.scrollTop) {
if (isScrolled) {
package main
import "fmt"
func main() {
a := 1
b := 2
max := map[bool]int{true: a, false: b}[a > b]
fmt.Println(max)
}
@snadrus
snadrus / gist:0fc5453d83cb9b39dfd6
Created January 30, 2015 19:18
Data URL to BLOB
function dataUrlToBlob(dUrl) {
return new Blob([new Uint8Array(atob(dUrl.slice(
dUrl.indexOf(',') + 1)).split(/\b|\B/).map(function(c){return c.charCodeAt(0)}))],
{type:dUrl.match(/\:([^;]*)/)[1]});
}
// Example:
var OneByTwoRo_data_url = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QBiRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAYAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAAITAAMAAAABAAEAAAAAAAAAAABIAAAAAQAAAEgAAAAB/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCAwKDAwLCgsLDQ4SEA0OEQ4LCxAWEBETFBUVFQwPFxgWFBgSFBUU/8IACwgAAgABAQERAP/EABQAAQAAAAAAAAAAAAAAAAAAAAj/2gAIAQEAAAABP/8A/8QAFhAAAwAAAAAAAAAAAAAAAAAAAAUW/9oACAEBAAEFAqx4f//EABkQAAEFAAAAAAAAAAAAAAAAAAACBTWU0f/aAAgBAQAGPwKZcLS9P//EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAT8hQv/aAAgBAQAAABB//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPxAV/9k=";
var blob_url = URL.createObjectURL(dataUrlToBlob(OneByTwoRo_data_url));
angular.module('autofocusAngularPolyfill', [])
.directive('autofocus', function() {
var timer;
return function(scope, elm, attr) {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
elm[0].focus();
});
};