Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add REST API command line interface 'rest-cli'. #539

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

MusclePr
Copy link
Contributor

@MusclePr MusclePr commented Apr 23, 2024

Context

Like rcon-cli, it supports a REST API command line interface.

Choices

Since rcon-cli is convenient, I also tried imitating rest-cli.

Test instructions

  1. download test script.
    test-rest-cli.zip

  2. extract to project root.

unzip test-rest-cli.zip
chmod +x test-rest-cli.sh
  1. build docker image with debug tag.
docker build -t thijsvanloef/palworld-server-docker:debug .
  1. run test-rest-cli.sh and connect server from client manualy.
$ ./test-rest-cli.sh 
[+] Running 1/2
 ⠸ Network palworld-server-docker_default  Created
  ✔ Container palworld-server               Started 
wait healthy...... done.
TEST ... docker exec -i palworld-server rest-cli
OK
TEST ... docker exec -i palworld-server rest-cli -h
OK
TEST ... docker exec -i palworld-server rest-cli announce -h
OK
TEST ... docker exec -i palworld-server rest-cli ban -h
OK
TEST ... docker exec -i palworld-server rest-cli unban -h
OK
TEST ... docker exec -i palworld-server rest-cli kick -h
OK
TEST ... docker exec -i palworld-server rest-cli shutdown -h
OK
TEST ... docker exec -i palworld-server rest-cli unknown foo bar
OK
TEST ... docker exec -i palworld-server rest-cli info
OK
TEST ... docker exec -i palworld-server rest-cli settings
OK
TEST ... docker exec -i palworld-server rest-cli metrics
OK
SERVER ADDRESS: 172.21.57.49:8211
TEST ... docker exec -i palworld-server rest-cli players
wait login..OK
TEST ... docker exec -i palworld-server rest-cli announce "Hello PalWorld!"
OK
TEST ... docker exec -i palworld-server rest-cli announce '{"message":"Logout to continue tests."}'
OK
TEST ... cat json-file | docker exec -i palworld-server rest-cli announce -
OK
TEST ... docker exec -i palworld-server rest-cli save
OK
TEST ... docker exec -i palworld-server rest-cli shutdown 300
OK
wait shutdown or logout......logout to stop now
TEST ... docker exec -i palworld-server rest-cli stop
OK
... done.
TEST ... docker exec -i palworld-server rest-cli info
OK
[+] Running 2/2
 ✔ Container palworld-server               Removed
 ✔ Network palworld-server-docker_default  Removed

Checklist before requesting a review

  • I have performed a self-review/test of my code
  • I've added documentation about this change to the docs.
  • I've not introduced breaking changes.
  • My changes do not violate linting rules.

@@ -155,7 +155,8 @@ COPY ./scripts /home/steam/server/
RUN chmod +x /home/steam/server/*.sh && \
mv /home/steam/server/backup.sh /usr/local/bin/backup && \
mv /home/steam/server/update.sh /usr/local/bin/update && \
mv /home/steam/server/restore.sh /usr/local/bin/restore
mv /home/steam/server/restore.sh /usr/local/bin/restore && \
ln -sf /home/steam/server/rest_api.sh /usr/local/bin/rest-cli
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbolic links are just a preference.

@@ -176,7 +176,8 @@ REST_API(){
local URL="http://localhost:${REST_API_PORT}/v1/api/${1}"
local ACCEPT="Accept: application/json"
local USERPASS="admin:${ADMIN_PASSWORD}"
if [ "${DATA}" = "" ]; then
local post_api="save|stop"
if [ "${DATA}" = "" ] && [[ ! ${1} =~ ${post_api} ]]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"save" and "stop" require the method to be POST.

@MusclePr
Copy link
Contributor Author

Due to an increase in minor fixes, I will temporarily organize the commits.

@MusclePr MusclePr force-pushed the feature/rest-cli branch 2 times, most recently from c35387c to 1628749 Compare April 24, 2024 01:47
ORGPATH=$(readlink -fn "${0}")
ORGDIR=$(dirname "${ORGPATH}")
#shellcheck source=scripts/helper_functions.sh
source "${ORGDIR}/helper_functions.sh"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The symbolic link /usr/local/bin/rest-cli refers to helper_functions.sh in the same location as the original rest_api.sh.

Comment on lines +46 to +88
elif [[ ! ${json} =~ ^\{ ]]; then
usage="Usage: ${SCRIPT} ${api}"
case ${api} in
"announce")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <message>"
exit 1
fi
json="{\"message\":\"${2}\"}"
;;
"ban")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000> [message]"
exit 1
fi
msg=${3:-You are banned.}
json="{\"userid\":\"${2}\",\"message\":\"${msg}\"}"
;;
"kick")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000> [message]"
exit 1
fi
msg=${3:-You are kicked.}
json="{\"userid\":\"${2}\",\"message\":\"${msg}\"}"
;;
"shutdown")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <sec> [message]"
exit 1
fi
sec=${2}
msg=${3:-Server will shutdown in ${sec} sec.}
json="{\"waittime\":${sec},\"message\":\"${msg}\"}"
;;
"unban")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000>"
exit 1
fi
json="{\"userid\":\"${2}\"}"
;;
esac
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are just converting the CLI parameters to JSON when the parameters are not JSON.

fi
fi

REST_API "${api}" "${json}" && echo ""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a line break because the output JSON ends with "}".

@thijsvanloef
Copy link
Owner

Hi @MusclePr ! Thanks again for the REST API PR's! Will take a look at it tonight

@MusclePr
Copy link
Contributor Author

Fixed a part of README.md.

@thijsvanloef
Copy link
Owner

Sorry I took so long, got sidetracked, merging!

@thijsvanloef thijsvanloef merged commit d1e3bb1 into thijsvanloef:main Apr 29, 2024
10 checks passed
@MusclePr MusclePr deleted the feature/rest-cli branch May 7, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants