[Snark] Docker Node Bash

Sean Howard submissions at badcode.rocks
Mon Oct 29 02:41:17 UTC 2018


piglatin.sh exists to support the piglatin docker container service.

It requires an external pig latin service to be running, but a docker container is provided to support local development.

.piglatinrc MUST be in the form

`HOST=hostname
PORT=portname`

If you must either of these then the defaults will be used. Other imports are ignored.

Requirements:
	This requires a standard POSIX bash shell with dos2unix installed to support removing CRLFs from webserver

== License ==

All work done by Sean Howard. Licensed under CC0.

== docker-compose.yml ==

version: '2'
services:
  pig:
    image: node
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - "8124:8124"

== Dockerfile ==

FROM node
MAINTAINER Sean
RUN npm install pig-latin
ADD piglatin.js piglatin.js
CMD ["node", "piglatin.js"]

== piglatin.js ==

var piglatin = require('pig-latin');
var http = require('http');

server = http.createServer(function (req, res) {
	res.write(piglatin(req.url.replace("/","")));
});
	server.listen(8124, "0.0.0.0");
console.log('Server running at http://127.0.0.1:8124/');

== piglatin.sh ==

#!/bin/bash
#Should be configurable for users without a local piglatin service
# set a default hostname
HOST=localhost
# set a default port
PORT=8124
# if the .piglatinrc file exists, use this instead of the default
if [ -f ~/.piglatinrc ]; then
source ~/.piglatinrc
fi
# Since the container will only do one word at a time, parse the words out
for PIG in $@
do
if [ ${PIG:0:2} = "yt" -o ${PIG:0:2} = "xr" ]
then
	export DEMANGLE="${PIG:0:2}"
	PIG=`echo $PIG| sed -e "s/^yt/e/" -e "s/^xr/e/"`
fi
#this is the easiest way to get the correct line from the container
WORD=`printf "GET /$PIG HTTP/1.1\n\nclose\n\n"|nc $HOST $PORT|dos2unix|tail -n3|head -n1|sed "s/way$/ay/"`
RETURN=0
if [ "${WORD:0:1}" = "u" ]
then
	if [ "${WORD: -3}" = "qay" ]
	then
		echo $WORD | sed -e "s/qay/quay/" -e "s/^u//"
		RETURN=1
	fi
fi
if [ $RETURN = 0 ]
then
	if [ -n "$DEMANGLE" ]
	then
		WORD=`echo $WORD | sed -e "s/^./$DEMANGLE/"`
	fi
	echo $WORD
fi
# the container only has one worker so treat it slowly
sleep 1
# merge script output
done | tr "\n" " "|sed "s/ $//"
echo


More information about the Snark mailing list