Skip to content

Instantly share code, notes, and snippets.

View olafkotur's full-sized avatar
💃
Aaaahhhh

Olaf Kotur olafkotur

💃
Aaaahhhh
View GitHub Profile
@olafkotur
olafkotur / http.ts
Created February 3, 2020 15:33
Simplified service for http requests in Typescript using the request library
const request = require('request');
export const HttpService = {
get: async (uri: string): Promise<any> => {
return await new Promise((resolve: any, reject: any) => {
request.get({ uri }, (error: Error, _response: any, body: any) => {
if (error) {
console.error(error);
reject();
}
@olafkotur
olafkotur / Dockerfile
Last active January 29, 2020 20:45
Docker multistaging dockerfile example
# Stage 1 - building
FROM golang:1.13-alpine3.10 as build
WORKDIR /go/src/app
COPY . .
RUN go build
# Stage 2 - optimised container
FROM alpine:3.10
COPY --from=build /go/src/app/main .
EXPOSE 8080
@olafkotur
olafkotur / jwt.go
Created January 29, 2020 20:35
JWT Authentication in Golang
// Return JWT header using HS256 algorithm
func generateHeader() (s string) {
header := `{"alg": "HS256", "typ": "JWT"}`
encoded := b64.StdEncoding.EncodeToString([]byte(header))
return strings.TrimRight(encoded, "=")
}
// Return JWT payload with some some example data e.g. expiration and username
func generatePayload(username string) (s string) {
exp := int(time.Now().Add(expThreshold).Unix())
@olafkotur
olafkotur / http.go
Created January 29, 2020 20:34
HTTP requests in Golang
// GET: Send the request
res, err := http.Get("http://example.com/api/someContext")
if err != nil {
log.Println(err)
}
// Read HTTP response into []byte
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
@olafkotur
olafkotur / mux.go
Created January 29, 2020 20:33
REST Server setup using Gorilla mux in Golang
import "github.com/gorilla/mux"
// Setup and create handlers
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api/example", getExample).Methods("GET")
// Able to set up a function to execute before each request
_ = http.ListenAndServe(":"+port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do something here
router.ServeHTTP(w, r)
@olafkotur
olafkotur / structs.go
Created January 29, 2020 20:31
Structs in Golang
// JSON
type SomeData struct {
Id int `json:"id"`
Status string `json:"status"`
}
var data []SomeData
data = append(data, SomeData{1, "Hello World"})
@olafkotur
olafkotur / arrays.go
Created January 29, 2020 20:31
Array manipulation in Golang
// Removing: If you already know the index
someArray = append(someArray[:i], someArray[i+1:]...)
// Optional to remove multiple based on value
for i, value := range someArray {
if value == someOtherValue {
someArray = append(someArray[:i], someArray[i+1:]...)
}
}
@olafkotur
olafkotur / promises.ts
Created January 29, 2020 20:29
Promises in Typescript
const data: ILiveData[] = await <any>MongoService.findOne('live', {});
findOne: async (collection: string, query: any) => {
return new Promise((resolve: any) => {
database.collection(collection).findOne(query, (error: Error, res: any) => {
if (error) {
throw error;
}
resolve(res);
});
@olafkotur
olafkotur / .zshrc
Created January 29, 2020 20:28
Configuration for zshrc
# Preferences
export ZSH="/Users/olafkotur/.oh-my-zsh"
# Plugins
ZSH_THEME="olafkotur"
plugins=(git tmux)
source $ZSH/oh-my-zsh.sh
# Alias: Directories
alias dev="cd && cd ~/Development"
@olafkotur
olafkotur / emailService.ts
Created January 29, 2020 20:25
Email service for sending out emails via nodemailer
import nodemailer from 'nodemailer';
export const EmailService = {
send: async (subject: string, to: string, body: string) => {
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {