API server for the Centaur scorekeeping software.
Requirements:
- MySQL server
- .NET Core 8 Runtime
Just install .NET core 8 on your server.
Create a user:
CREATE DATABASE CentaurScores;
CREATE USER 'csuser'@'{host}' IDENTIFIED WITH mysql_native_password BY '{superSecretPassword!123}';
GRANT ALL PRIVILEGES ON CentaurScores.* TO 'csuser'@'%';
FLUSH PRIVILEGES;
You can now set-up a connection string in appsettings.json
on your deployed instance like this:
{
...,
"ConnectionStrings": {
"CentaurScoresDatabase": "server={server-ip};database=CentaurScores;user=csuser;password={superSecretPassword!123}"
}
}
The first time you use the software, the initial database schema will automatically be created.
Start with creating a user account that is used to run this software. This user needs no special privileges on the system.
sudo adduser --system csuser
Copy over the software to a temporary folder in the system. Then create a folder to host from. Make sure this is not owned by the system user you just created, make sure they can access it though.
sudo mkdir -p /var/www/centaurscoresapi
sudo cp -R /tmp/release /var/www/centaurscoresapi
sudo chown -R root:root /var/www/centaurscoresapi
sudo chmod -R a+rX /var/www/centaurscoresapi
If you don't have the file yet, copy /var/www/centaurscoresapi/appsettings.json to /var/www/centaurscoresapi/appsettings.Production.json and fill in the missing information. The Secret can just be some random string, the LoginSecret is an API password for some restricted functionality. this is currently notr used so set it to some default.
Create a service definition file for your service, based on deployment-files/centaurscoresapi.service
. Note that you will need to adjust this for the location of your dotnet binary.
Store the updated file in /etc/systemd/system/centaurscoresapi.service
then enable it.
sudo systemctl enable centaurscoresapi.service
Then start it and check if it all worked.
sudo systemctl start centaurscoresapi
sudo systemctl status centaurscoresapi
Congratulations, you should now have your API service running on http://<server-ip>:8062/
Stop the service:
sudo systemctl stop centaurscoresapi
Copy the new release into the runtime folder:
sudo rm -f /tmp/release/appsettings.Production.json
sudo cp -R /tmp/release /var/www/centaurscoresapi
sudo chown -R root:root /var/www/centaurscoresapi
sudo chmod -R a+rX /var/www/centaurscoresapi
If database updates are required, apply them now to your MySQL database.
Start the software:
sudo systemctl start centaurscoresapi
#!/bin/bash
systemctl stop centaurscoresapi
sleep 5
DOWNLOAD_URL=$(curl -s https://api.github.com/repos/pbijdens/centaur-scores-api/releases/latest | grep browser_download_url | cut -d '"' -f4)
DB_SERVER="127.0.0.1"
DB_USER="csuser"
DB_NAME="CentaurScores"
DB_PASSWORD="YOUR-DATABASE-PASSWORD"
useradd --system --home-dir /var/www --no-create-home csuser --shell /usr/sbin/nologin
mkdir -p /var/www/centaurscoresapi
rm -rf /tmp/release
mkdir /tmp/release
pushd /tmp/release
curl -sL $DOWNLOAD_URL | tar xvfz -
popd
rm -rf /var/www/centaurscoresapi/*
cp -R /tmp/release/* /var/www/centaurscoresapi
chown -R root:root /var/www/centaurscoresapi
chmod -R a+rX /var/www/centaurscoresapi
cat <<EOT > /etc/systemd/system/centaurscoresapi.service
[Unit]
Description=CentaurScores API Service (kestrel)
[Service]
WorkingDirectory=/var/www/centaurscoresapi
ExecStart=/usr/bin/dotnet /var/www/centaurscoresapi/CentaurScores.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=centaurscoresapi
User=csuser
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://*:8062
[Install]
WantedBy=multi-user.target
EOT
cat <<EOT > /var/www/centaurscoresapi/appsettings.Production.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"CentaurScoresDatabase": "server=$DB_SERVER;database=$DB_NAME;user=$DB_USER;password=$DB_PASSWORD"
}
}
EOT
chown -R root:root /var/www/centaurscoresapi
chmod -R a+rX /var/www/centaurscoresapi
systemctl enable centaurscoresapi.service
systemctl start centaurscoresapi
sleep 5
systemctl status centaurscoresapi