5.3 KiB
Photos API
Small and simple API server for saving photos and videos.
Dependencies
- Python 3.8+ (3.9+ recommended)
- MongoDB
- exiftool
- jpegoptim
- optipng
Installation
First you need to have a Python interpreter, MongoDB and optionally git. You can also ignore git and simply download source code, should also work fine. After that you're ready to go.
In this README I assume that you're using default python in your system and your system's PATH contains it. If your default python is
python3
or for example/home/user/.local/bin/python3.9
- use it instead.
-
Install Mongo:
Please follow official installation manual for that.
-
Download Photos API:
git clone https://git.end-play.xyz/profitroll/PhotosAPI.git
(if you're using git)cd PhotosAPI
-
Create virtual environment [Optional yet recommended]:
- Install virtualenv module:
pip install virtualenv
- Create venv:
python -m venv .venv
- Activate it using
source .venv/bin/activate
on Linux,.venv\Scripts\activate.bat
in CMD or.venv\Scripts\Activate.ps1
in PowerShell.
- Install virtualenv module:
-
Install project's dependencies:
python -m pip install -r requirements.txt
-
Configure your API:
- Copy file
config_example.json
toconfig.json
- Open
config.json
using your favorite text editor. For examplenano config.json
- Change
"database"
keys to match your MongoDB setup - Set the key
"secret"
to your JWT secret. You can type in anything, but long secrets are recommended. You can also set environment variablePHOTOSAPI_SECRET
as an alternative - Change
"external_address"
to the ip/http address you may get in responses. By default it's"localhost"
. This is extremely useful when running behind reverse-proxy.
After configuring everything listed above your API will be able to boot, however further configuration can be done. You can read about it in repository's wiki. There's no need to focus on that now, it makes more sense to configure it afterwards.
- Copy file
-
Start your API:
You can run your API by the following command:
uvicorn photos_api:app --host 127.0.0.1 --port 8054
Learn more about available uvicorn arguments using
uvicorn --help
Upgrading
When a new version comes out, sometimes you want to upgrade your instance right away. Here's a checklist what to do:
- Carefully read the patch notes of the version you want to update to and all the versions that came out between the release of your version and the one you want to upgrade to. Breaking changes will be marked so and config updates will also be described in the patch notes
- Make a backup of your currently working instance. This includes both the PhotosAPI and the database
- Download the latest version using git (
git pull
if you cloned the repo in the past) or from the releases - Reconfigure the config if needed and apply the changes from the patch notes
- Upgrade the dependencies in your virtual environment using
pip install -r requirements.txt
- Start the migration using
python photos_api.py --migrate
from your virtual environment - Test if everything works and troubleshoot/rollback if not
Using as a service
It's a good practice to use your API as a systemd service on Linux. Here's a quick overview how that can be done.
-
Create user and move your API
You don't always need to do so, but that's a cleaner way to deploy a service.
- Create service user
photosapi
usingsudo useradd -r -U photosapi
- Assuming you are still in directory
PhotosAPI
, usecd ..
to go up a level and then move your API to the distinguished folder. For example,/opt/
:sudo mv ./PhotosAPI /opt/
- Make your user and its group own their directory using
sudo chown -R photosapi:photosapi /opt/PhotosAPI
- Create service user
-
Configure service
Here's an example service file for PhotosAPI that is using virtual environment:
[Unit] Description=Photos API After=network.target mongod.service Wants=network-online.target mongod.service [Service] Restart=always Type=simple ExecStart=/bin/bash -c 'source .venv/bin/activate && .venv/bin/uvicorn photos_api:app --port 8054' WorkingDirectory=/opt/PhotosAPI User=photosapi Group=photosapi [Install] WantedBy=multi-user.target
- Create a service by pasting code above into
/etc/systemd/system/photos-api.service
- Enable your service to start on system boot using
sudo systemctl enable photos-api.service
- Start your service now using
sudo systemctl start photos-api.service
- Check if it's running using
sudo systemctl status photos-api.service
- If something goes wrong - check API's logs using
sudo journalctl -u photos-api.service
- Create a service by pasting code above into