Script: Bind Hugo to external net

Posted in tech; tagged with dev, hugo, tips

Problem: You need to run Hugo in “watch” mode during development but rather than bind to the default localhost:1313 you need to bind Hugo to an externally visible IP address - which for those of us not chained to one work location may change frequently. An externally accessible IP is particularly helpful for virtual machine users that may need to test browsers available on other real or virtual machines. (found in hugo-smorg) will bind Hugo in watch mode to the first available global network interface, whatever it may be called. On many systems that will be eth0; on a VirtualBox guest instance my bridged interface is called enp0s3.

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:97:35:14 brd ff:ff:ff:ff:ff:ff
    inet brd scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe97:3514/64 scope link 
       valid_lft forever preferred_lft forever

# run Hugo in dev mode on an externally accessible interface (not localhost)
# matches first global interface, may not be "eth0" on some systems
IPADDR=`ip a show scope global | grep -m 1 'inet '| awk '{print $2}' | cut -d / -f1`

echo "Serving on: " $IPADDR ":" $PORT
# -v verbose log output
# -D build including drafts 
# -F build including future dated content; 
hugo server -v -D -F --bind $IPADDR -p $PORT -b $IPADDR