Skip to content

Instantly share code, notes, and snippets.

@andrewstucki
Created March 17, 2023 01:51
Show Gist options
  • Save andrewstucki/fce7d28e6544a727970a173fca37ecd3 to your computer and use it in GitHub Desktop.
Save andrewstucki/fce7d28e6544a727970a173fca37ecd3 to your computer and use it in GitHub Desktop.
#!/bin/bash
./consul agent -dev > /dev/null 2>&1 &
sleep 2
cat << EOF | ./consul config write -
Kind = "proxy-defaults"
Name = "global"
Config {
protocol = "http"
}
EOF
cat << EOF | ./consul config write -
kind = "api-gateway"
name = "gateway"
listeners = [
{
port = 8081
protocol = "http"
name = "listener"
}
]
EOF
cat << EOF | ./consul config write -
kind = "http-route"
name = "router"
rules = [
{
services = [{
name = "service"
}]
}
]
parents = [{
name = "gateway"
}]
EOF
cat << EOF > /tmp/service.hcl
service {
name = "service"
id = "service"
port = 9002
connect {
sidecar_service {}
}
}
EOF
./consul services register /tmp/service.hcl
cat << EOF > /tmp/service.html
HTTP/1.1 200 GET
Content-Type: text/html; charset=UTF-8
<!doctype html><html><body>service</body></html>
EOF
ncat -e "/bin/cat /tmp/service.html" -k -l 9002 > /dev/null 2>&1 &
./consul connect envoy -sidecar-for service -admin-bind 127.0.0.1:9092 > /dev/null 2>&1 &
./consul connect envoy -gateway api -register -service gateway > /dev/null 2>&1 &
checkPort() {
ret=1
until [ "$ret" == "0" ]
do
echo "gateway not listening yet"
lsof -i :8081 > /dev/null
ret=$?
done
echo "gateway listening"
}
time checkPort
exec 2> /dev/null
killall consul envoy ncat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment