This is a backend designed for Button Game.
URL | METHOD | DESCRIPTION | PARAMETERS |
---|---|---|---|
/api/ | GET | Root of api future documentation | NONE |
/api/v1.0/players/ | GET | Get one player if username exists else all players | username |
/api/v1.0/click/ | POST | Send click to server with 'username' in body | NONE |
/api/v1.0/needed-clicks/ | GET | Get next goal and amount of clicks needed | NONE |
NOTE: Most of these commands need to be run with root
or sudo
.
Install needed programs:
apt-get install mariadb-server nginx
Install needed libraries for python:
pip3 install flask flask-cors pymysql uwsgi
Installer can be downloaded by clicking this link.
or with:
wget https://dl.meckl.in/button-game-api/install.py
But first you need to create a directory and a database with a user for this.
Access the mysql with your root and its password (set in the installation of mariadb)
mysql -u root -p
Create database and a user for it.
CREATE DATABASE button_game;
GRANT ALL PRIVILEGES ON button_game.* TO 'api-user'@'localhost' IDENTIFIED BY 'very nice password';
FLUSH PRIVILEGES;
Now create a directory that will contain the api software itself.
mkdir /var/www/button-game-api
Now you should copy your just downloaded installer with wget:
cp install.py /var/www/button-game-api/install.py
or download it now:
cd /var/www/button-game-api
wget https://dl.meckl.in/button-game-api/install.py
Now you are ready to install!
Run the installer:
python3 install.py
And give the installer all the needed stuff that you just set up.
If you are using something else than Ubuntu, you should add this line in your config/uwsgi.ini
file below module = wsgi
:
plugins = logfile, python36
Replace python36 with python35 if you are using python3.5 and so on.
Create a new service to run the API:
nano /etc/systemd/system/button-game-api.uwsgi.service
Paste this inside with your own directories.
[Unit]
Description=uWSGI Backend for Button Game
After=syslog.target
[Service]
WorkingDirectory=/var/www/button-game-api
# Can require modifying, based on uwsgi installation directory
ExecStart=/usr/bin/uwsgi --ini config/uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Enable the service:
systemctl daemon-reload
Start the service:
service button-game-api.uwsgi start
If everything went right (without errors) you can now start to setup Nginx.
nano /etc/nginx/uwsgi_params
And paste parameters from this page .
Create configuration for Nginx:
nano /etc/nginx/sites-available/button-game-api.conf
And fill it with this:
server {
listen 80;
server_name api.example-game.com www.api.example-game.com;
root /var/www/button-game-api/;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/button-game-api/config/uwsgi.sock;
}
}
Enable your configuration:
ln -s /etc/nginx/sites-available/button-game-api.conf /etc/nginx/sites-enabled/button-game-api.conf
Test that your configuration works:
nginx -t
If it works:
service nginx reload
Or:
service nginx restart
Now setup your domain to point to your servers IP-address.
To fix permission issues (caused byt using root or sudo):
cd ..
chown www-data:www-data button-game-api/ -R
Download certbot (if you are using something other than Ubuntu 14.04 - 18.04, installation guide can be found here):
apt-get update
apt-get install software-properties-common
add-apt-repository universe
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install certbot python-certbot-nginx
Get SSL certificates from Let's Encrypt:
certbot --nginx
Now select your newly created site.