Skip to content

Instantly share code, notes, and snippets.

View moqmar's full-sized avatar

Moritz Marquardt moqmar

View GitHub Profile
@moqmar
moqmar / bootstrap.sh
Last active July 27, 2019 20:16
Set up a Debian/Ubuntu server with basic software and zsh.
#!/bin/bash
# bash -c ". <(curl -s https://gist.githubusercontent.com/moqmar/7b468d995619131e983d59bde253d9a0/raw)"
set -euo pipefail
IFS=$'\n\t'
shopt -s dotglob
auto=
if [ $# -gt 1 ]; then auto=$1; fi
@moqmar
moqmar / Responsive Modals.md
Last active May 8, 2017 19:04
Responsive Modals made purely with HTML & CSS

Responsive Modals made purely with HTML & CSS

…in less than half a kilobyte

All you need is the file modal.min.css and the following code:

<input id="helloworld" type="checkbox" hidden><div class="modal">
  <label for="helloworld"></label><!-- close modal on click -->
  <div><!-- modal contents -->
    <h1>Look at this awesomeness!</h1>
@moqmar
moqmar / select-on-click.js
Last active November 14, 2017 11:23
Select all text in an element
// Modified version of https://stackoverflow.com/a/987376
function selectText(el) {
if (!el) return;
var range, selection;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(el);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
@moqmar
moqmar / Dockerscript
Last active November 14, 2017 11:24
Script for simple management and updating of docker containers. Like docker-compose, but more flexible and with single-command upgrade functionality.
#!/bin/sh
# ./Dockerscript <prefix>
name=helloworld
[ -n "$PREFIX" ] && prefix=-$PREFIX
set -e
start() {
docker network create --driver bridge $name$prefix
docker run -id --restart=always --network=$name$prefix -v "$PWD/files:/data" --name=$name$prefix-main "$@" hello-world
@moqmar
moqmar / build.sh
Last active October 2, 2019 18:08
Caddy build script
#!/bin/bash
#
# Build script for the Git version of https://caddyserver.com
#
# Requirements:
# - Go
# - jq (if using names instead of import paths for plugins)
# Usage:
# curl https://gist.githubusercontent.com/moqmar/ba77bd778e6ebc956eaa36388be3fcdd/raw | bash -s http.realip http.ipfilter http.cors http.expires http.ratelimit #[...]
@moqmar
moqmar / evem.js
Last active March 20, 2018 21:42
Event Emitter
// Minimal Event Handler
// Call makeEmitter(obj) to make obj an event emitter.
// obj.on(event, handler)
// obj.off(event[, handler])
// obj.fire(event[, args...])
// https://gist.github.com/moqmar/1a33e871e9a4b855443d57d40f56d052 (Public Domain/CC0)
function makeEmitter(obj) {
obj.__l = {}; // Listeners - this.on("update", fn) -> { update: [fn] }
obj.on = function(ev, fn) {
if (!this.__l[ev]) this.__l[ev] = [fn];
@moqmar
moqmar / simple-implementations.md
Last active May 4, 2018 11:21
Simple implementations for simple tasks
@moqmar
moqmar / bridge.service
Last active May 28, 2018 17:18
Adding a network bridge with IP address under Linux
[Unit]
Description=Network Bridge
After=syslog.target network.target
[Service]
Type=onehot
ExecStart=/opt/bridge.sh
[Install]
WantedBy=multi-user.target
@moqmar
moqmar / queue.js
Created June 14, 2018 08:10
Completely promise-based queue that works with functions returning promises and normal values.
// Completely promise-based queue that works with functions returning promises and normal values.
/*
function doSomething() {
return new Promise(y => setTimeout(() => y(), 3000));
}
// Using the singleton
for (let i = 0; i < 10; i++) Q(() => doSomething()).then(() => console.log(i));
@moqmar
moqmar / rest-api.js
Last active June 24, 2018 15:22
fetch() wrapper for REST APIs
function RestApi(endpoint, options) {
this.endpoint = (endpoint || "/").replace(/\/$/, "");
this.options = options || {};
// Allow usage in queue, or in other places where the value of "this" is incorrect:
for (let method in ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"].reduce((p,c)=>p[c]=1,{})) {
this[method] = RestApi.prototype[method].bind(this);
}
}
RestApi.fetchResponseHandler = function fetchResponseHandler(hardFail, res) {
return new Promise((resolve, reject) => {