This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r := chi.NewRouter() | |
r.Route("/pets", func(r chi.Router) { | |
r.With(petMiddleware).Route("/{pet}", func(r chi.Router) { | |
r.Get("/", getPetHandler) | |
r.Put("/", putPetHandler) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func petMiddleware(next http.Handler) http.Handler { | |
allowedPets := map[string]struct{}{ | |
"cat": struct{}{}, | |
"dog": struct{}{}, | |
} | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
pet := chi.URLParam(r, "pet") | |
if _, ok := allowedPets[pet]; !ok { | |
w.WriteHeader(http.StatusBadRequest) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BAD EXAMPLE, doesn't work! | |
r := chi.NewRouter() | |
r.With(petMiddleware).Route("/pets", func(r chi.Router) { | |
r.Route("/{pet}", func(r chi.Router) { | |
r.Get("/", getPetHandler) | |
r.Put("/", putPetHandler) | |
}) | |
}) | |
http.ListenAndServe(":8000", r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.Put("/info", func(w http.ResponseWriter, r *http.Request) { | |
pet := chi.URLParam(r, "pet") | |
forbiddenPets := []string{"dragon"} | |
for _, forbiddenPet := range forbiddenPets { | |
if pet == forbiddenPet { | |
w.WriteHeader(http.StatusBadRequest) | |
w.Write([]byte(fmt.Sprintf("forbidden pet %s!\n", pet))) | |
return | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.Get("/info", func(w http.ResponseWriter, r *http.Request) { | |
pet := chi.URLParam(r, "pet") | |
forbiddenPets := []string{"dragon"} | |
for _, forbiddenPet := range forbiddenPets { | |
if pet == forbiddenPet { | |
w.WriteHeader(http.StatusBadRequest) | |
w.Write([]byte(fmt.Sprintf("forbidden pet %s!\n", pet))) | |
return | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func serve() { | |
r := chi.NewRouter() | |
r.Route("/pets", func(r chi.Router) { | |
r.Route("/{pet}", func(r chi.Router) { | |
r.Get("/", getPetHandler) | |
r.Put("/", putPetHandler) | |
}) | |
}) | |
http.ListenAndServe(":8000", r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
apt-get install -y curl unzip | |
mkdir -p /var/lib/consul | |
mkdir -p /usr/share/consul | |
mkdir -p /etc/consul/conf.d | |
curl -OL https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip | |
unzip 0.4.1_linux_amd64.zip | |
mv consul /usr/local/bin/consul |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
import boto | |
import config | |
import gevent | |
import gevent.pool | |
import os |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
"""Taken from: | |
http://stackoverflow.com/a/22348885 | |
http://stackoverflow.com/a/2282656""" | |
import signal | |