[Snark] Docker Node Bash

Stephen Paul Weber singpolyma at singpolyma.net
Fri Nov 9 04:14:37 UTC 2018


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

Not part of the code, but we're off to a good start with documentation like 
"POSIX bash".

>== docker-compose.yml ==
>
>version: '2'
>services:
> pig:
>   image: node
>   build:
>     context: .
>     dockerfile: ./Dockerfile
>   ports:
>     - "8124:8124"

Because everyone needs a compose file for a single service that isn't 
composed with anything else.

Also, technically there is no docker-compose on Debian stable, but since 
this isn't strictly needed to run the program I allow it.  I tested with and 
without it.

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

Super important to containerise so we don't need to install npm on the host 
system.

I mean, given npm, I almost agree.

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

This is what you would of course do in a real program.  It does remove your 
ability to make the implementation of this part bad, though.

>var http = require('http');

Wait.

>server = http.createServer(function (req, res) {
>	res.write(piglatin(req.url.replace("/","")));
>});

Uhm.  I want to nit about the terrible way of extracting something from the 
input path here.  But instead... why is there an HTTP server?  I mean, a web 
server that translates to pig latin is cool, but why do we need one?  Does 
nodejs not have a print function?

>	server.listen(8124, "0.0.0.0");

Oh good, blindly bind to 0.0.0.0, because nothing can go wrong!

>console.log('Server running at http://127.0.0.1:8124/');

Oh look, it can print things out!  In this case, it prints out a lie.

>#!/bin/bash

This hurts me.

>#Should be configurable for users without a local piglatin service
># set a default hostname
>HOST=localhost
># set a default port
>PORT=8124

Oh good, we needed a shell script to glue our fancy web service to the 
actual problem specification.

># if the .piglatinrc file exists, use this instead of the default
>if [ -f ~/.piglatinrc ]; then
>source ~/.piglatinrc
>fi

`source` bleh.  Also -f instead of -r.  Also WHY DO YOU NEED THIS?

># Since the container will only do one word at a time, parse the words out

Oh good, let's paper over our own service.

>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

Oh, we're really REALLY going to paper over it are we?
Looks like the chosen library DOESN'T EVEN IMPLEMENT THE PROBLEM WE'RE 
SOLVING!

>WORD=`printf "GET /$PIG HTTP/1.1\n\nclose\n\n"|nc $HOST $PORT|dos2unix|tail -n3|head -n1|sed "s/way$/ay/"`

There are no words.

>RETURN=0

Everyone loves a flag.  I think most of our submissions include a flag, it's 
like the universal antipattern!

>if [ "${WORD:0:1}" = "u" ]
>then

Semicolons are hard.

># the container only has one worker so treat it slowly
>sleep 1

Is this... actually needed?  Even with the terrible printf/nc/dos2unix HTTP 
client, I would hope the server knows how to queue connections?

>done | tr "\n" " "|sed "s/ $//"

Fix everything.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://snark.badcode.rocks/pipermail/snark/attachments/20181108/b99cf942/attachment.sig>


More information about the Snark mailing list