121 lines
2.5 KiB
Bash
Executable File
121 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
# 2022-09-14 Hyperling
|
|
# Ensure dependencies are met and start the webserver.
|
|
|
|
## Setup ##
|
|
|
|
DIR=`dirname $0`
|
|
PROG=`basename $0`
|
|
|
|
## Functions ##
|
|
|
|
function usage {
|
|
cat <<- EOF
|
|
$PROG calls the main Node.js program after ensuring the project can run.
|
|
(PORTS)
|
|
-p : Pass the port numbers that the API/website should listen at.
|
|
Example: $PROG -p 80 -p 443 -p 8080
|
|
(HELP)
|
|
-h : Show this usage output and exit successfully.
|
|
EOF
|
|
exit $1
|
|
}
|
|
|
|
function log {
|
|
message="$1"
|
|
echo -e "`date` - $message"
|
|
}
|
|
|
|
## Parameters ##
|
|
|
|
while getopts ':p:h' opt; do
|
|
case "$opt" in
|
|
p) (( OPTARG < 1024 )) && [[ $LOGNAME != "root" ]] && {
|
|
echo "WARNING: Port $OPTARG is privileged. Will need to be root."
|
|
exit 1
|
|
}
|
|
ports="$ports $OPTARG" ;;
|
|
h) usage 0 ;;
|
|
*) echo "ERROR: Option $OPTARG not recognized." >&2
|
|
usage 1 ;;
|
|
esac
|
|
done
|
|
|
|
if [[ -z $ports ]]; then
|
|
ports=8080
|
|
fi
|
|
|
|
## Build Environment ##
|
|
|
|
# Ensure we are executing from this file's directory.
|
|
cd $DIR
|
|
|
|
sudo=""
|
|
if [[ $LOGNAME != "root" ]]; then
|
|
log "Using sudo since user is '$LOGNAME'."
|
|
sudo="sudo"
|
|
fi
|
|
|
|
log "Check if any system dependencies need installed."
|
|
if [[ ! `which php` ]]; then
|
|
echo "- Installing PHP"
|
|
$sudo apt-get install -y php-cli
|
|
fi
|
|
if [[ ! `which node` ]]; then
|
|
echo "- Installing Node"
|
|
$sudo apt-get install -y nodejs
|
|
fi
|
|
if [[ ! `which npm` ]]; then
|
|
echo "- Installing NPM"
|
|
$sudo apt-get install -y npm
|
|
fi
|
|
|
|
# Directories and allowed page types are executable, others are not.
|
|
log "Fix any strange file permissions."
|
|
find ./pages/ | while read file; do
|
|
if [[ $file == *".php" || $file == *".sh" || -d $file ]]; then
|
|
mode=755
|
|
else
|
|
mode=644
|
|
fi
|
|
chmod -c $mode $file
|
|
done
|
|
|
|
log "Check if any node modules need updated/installed."
|
|
npm install
|
|
|
|
# Reset generated index files.
|
|
log "Removing old index files."
|
|
find files/photos/ -name "*".html -print -delete
|
|
{
|
|
count=1
|
|
http_code=0
|
|
port="${ports%% *}"
|
|
photos_uri=":$port/photos/"
|
|
while [[ $http_code != "200" ]]; do
|
|
log "Sleeping for '$count' while waiting for $photos_uri to come up."
|
|
sleep $count
|
|
log "Checking if $photos_uri is available."
|
|
http_code="`curl --silent --fail --w '\n%{http_code}' localhost$photos_uri | tail -n 1`"
|
|
log "Check for $photos_uri responded with '$http_code'."
|
|
if (( $count >= 10 )); then
|
|
log "Giving up on loading $photos_uri after '$count' attempts."
|
|
break
|
|
else
|
|
count=$(( count + 1 ))
|
|
fi
|
|
done
|
|
log "Finished checking for /photos/."
|
|
} &
|
|
|
|
## Main ##
|
|
|
|
log "Start website API."
|
|
node ./main.js $ports
|
|
status=$?
|
|
|
|
## Finish ##
|
|
|
|
log "Exiting with status '$status'."
|
|
exit $status
|