Skip to content

Instantly share code, notes, and snippets.

Karl Gustav karl-gustav

Block or report user

Report or block karl-gustav

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View safe_script.bash
#######COPY THIS#######
set -euo pipefail
#######################
# Exit immediately when a command fails.
set -e
# Fail if any step in pipe fails, not just the last one
set -o pipefail
@karl-gustav
karl-gustav / json_decoding_web_request.go
Last active Sep 23, 2019
Decode directly from request in golang webserver
View json_decoding_web_request.go
var subscription Subscription
err := json.NewDecoder(r.Body).Decode(&subscription)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
defer r.Body.Close()
@karl-gustav
karl-gustav / json_encoding_web_response.go
Last active Sep 22, 2019
Encode directly into golang http response
View json_encoding_web_response.go
r.Get("/subscription/{phoneNumber}", func(w http.ResponseWriter, r *http.Request) {
phoneNumber := chi.URLParam(r, "phoneNumber")
subscription, err := dbGet(phoneNumber)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
}
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
err = encoder.Encode(subscription)
if err != nil {
@karl-gustav
karl-gustav / Makefile
Last active Aug 29, 2019
Makefile for deploying with password environment variables on remote ssh server (and 8080->80, 4443->443 port forwarding)
View Makefile
SSH_SERVER=45.46.47.48
PROJECT_NAME=some-power
EXECUTABLE=some_power
ENV_FILE=.env_file
build:
GOOS=linux GOARCH=amd64 go build -o $(EXECUTABLE) .
deploy: build
$(eval IS_SUITE_PASSWORD := $(shell gopass api/some_more_power/password))
$(eval SMS_PASSWORD := $(shell gopass api/sms_service/password))
@karl-gustav
karl-gustav / Makefile
Created Aug 29, 2019
Makefile for deploying a go executable as a service to an ssh machine
View Makefile
SSH_SERVER=34.32.31.30
PROJECT_NAME=some-power
EXECUTABLE=some_power
build:
GOOS=linux GOARCH=amd64 go build -o $(EXECUTABLE) .
deploy: build
@tar czf - $(EXECUTABLE) | ssh $(SSH_SERVER) 'echo ≫ Backing up old executable...\
&& test -f /srv/$(PROJECT_NAME)/$(EXECUTABLE)\
&& mv /srv/$(PROJECT_NAME)/$(EXECUTABLE){,.old}\
View install_micro.md
@karl-gustav
karl-gustav / Dockerfile
Last active Jan 9, 2020
Dockerfile for building golang scratch container
View Dockerfile
# Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.13-alpine as builder
RUN apk update
# Add required certificates to be able to call HTTPS endpoints.
RUN apk add --no-cache ca-certificates git
# Add git for installing go dependencies
RUN apk add --no-cache git
@karl-gustav
karl-gustav / main.go
Created May 24, 2019
Super simple golang reverse proxy
View main.go
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
@karl-gustav
karl-gustav / Makefile
Last active Dec 25, 2019
Good example of Makefile for using google run containers with gopass
View Makefile
GPC_PROJECT_ID=hkraft-iot
SERVICE_NAME=defibrilator-registry
CONTAINER_NAME=eu.gcr.io/$(GPC_PROJECT_ID)/$(SERVICE_NAME)
run: build
docker run -p 8080:8080\
-e CLIENT_ID=$$(gopass hjertestarterregisteret.no/client-id)\
-e CLIENT_SECRET=$$(gopass hjertestarterregisteret.no/client-secret)\
$(CONTAINER_NAME)
build:
@karl-gustav
karl-gustav / lillesveiven.space
Created Apr 24, 2019
/etc/nginx/sites-available/lillesveiven.space
View lillesveiven.space
# Based on https://gist.github.com/nrollr/9a39bb636a820fb97eec2ed85e473d38:
# =========================================================================
# UPDATED 17 February 2019
# Redirect all HTTP traffic to HTTPS
server {
listen 80;
listen [::]:80;
server_name lillesveiven.space;
return 301 https://lillesveiven.space$request_uri;
}
You can’t perform that action at this time.