torrust_index/
lib.rs

1//! Documentation for [Torrust Tracker Index](https://github.com/torrust/torrust-index) API.
2//!
3//! This is the index API for [Torrust Tracker Index](https://github.com/torrust/torrust-index).
4//!
5//! It is written in Rust and uses the [Axum](https://github.com/tokio-rs/axum) framework. It is designed to be
6//! used with by the [Torrust Tracker Index Gui](https://github.com/torrust/torrust-index-gui).
7//!
8//! If you are looking for information on how to use the API, please see the
9//! [API v1](crate::web::api::server::v1) section of the documentation.
10//!
11//! # Table of contents
12//!
13//! - [Features](#features)
14//! - [Services](#services)
15//! - [Installation](#installation)
16//!     - [Minimum requirements](#minimum-requirements)
17//!     - [Prerequisites](#prerequisites)
18//!     - [Install from sources](#install-from-sources)
19//!     - [Run with docker](#run-with-docker)
20//!     - [Development](#development)
21//! - [Configuration](#configuration)
22//! - [Usage](#usage)
23//!     - [API](#api)
24//!     - [Tracker Statistics Importer](#tracker-statistics-importer)
25//!     - [Upgrader](#upgrader)
26//! - [Contributing](#contributing)
27//! - [Documentation](#documentation)
28//!
29//! # Features
30//!
31//! - Torrent categories
32//! - Image proxy cache for torrent descriptions
33//! - User registration and authentication
34//! - DB Support for `SQLite` and `MySQl`
35//!
36//! # Services
37//!
38//! From the end-user perspective the Torrust Tracker exposes three different services.
39//!
40//! - A REST [API](crate::web::api::server::v1)
41//!
42//! From the administrator perspective, the Torrust Index exposes:
43//!
44//! - A console command to update torrents statistics from the associated tracker
45//! - A console command to upgrade the database schema from version `1.0.0` to `2.0.0`
46//!
47//! # Installation
48//!
49//! ## Minimum requirements
50//!
51//! - Rust Stable `1.68`
52//!
53//! ## Prerequisites
54//!
55//! In order the run the index you will need a running torrust tracker. In the
56//! configuration you need to fill the `tracker` section with the following:
57//!
58//! ```toml
59//! [tracker]
60//! url = "udp://localhost:6969"
61//!
62//! api_url = "http://localhost:1212/"
63//! token = "MyAccessToken"
64//! token_valid_seconds = 7257600
65//! ```
66//!
67//! Refer to the [`config::tracker`](crate::config::Tracker) documentation for more information.
68//!
69//! You can follow the tracker installation instructions [here](https://docs.rs/torrust-tracker)
70//! or you can use the docker to run both the tracker and the index. Refer to the
71//! [Run with docker](#run-with-docker) section for more information.
72//!
73//! You will also need to install this dependency:
74//!
75//! ```text
76//! sudo apt-get install libssl-dev
77//! ```
78//!
79//! We needed because we are using native TLS support instead of [rustls](https://github.com/rustls/rustls).
80//!
81//! More info: <https://github.com/torrust/torrust-index/issues/463>.
82//!
83//! If you are using `SQLite3` as database driver, you will need to install the
84//! following dependency:
85//!
86//! ```text
87//! sudo apt-get install libsqlite3-dev
88//! ```
89//!
90//! > **NOTICE**: those are the commands for `Ubuntu`. If you are using a
91//! > different OS, you will need to install the equivalent packages. Please
92//! > refer to the documentation of your OS.
93//!
94//! With the default configuration you will need to create the `storage` directory:
95//!
96//! ```text
97//! storage/
98//! └── database
99//!     └── data.db
100//! ```
101//!
102//! The default configuration expects a directory `./storage/database` to be writable by the app process.
103//!
104//! By default the index uses `SQLite` and the database file name `data.db`.
105//!
106//! ## Install from sources
107//!
108//! ```text
109//! git clone git@github.com:torrust/torrust-index.git \
110//!   && cd torrust-index \
111//!   && cargo build --release \
112//!   && mkdir -p ./storage/database
113//! ```
114//!
115//! Then you can run it with: `./target/release/torrust-index`
116//!
117//! ## Run with docker
118//!
119//! You can run the index with a pre-built docker image:
120//!
121//! ```text
122//! cd /tmp \
123//!   && mkdir torrust-index \
124//!   && cd torrust-index \
125//!   && mkdir -p ./storage/index/lib/database \
126//!   && mkdir -p ./storage/index/log \
127//!   && mkdir -p ./storage/index/etc \
128//!   && sqlite3 "./storage/index/lib/database/sqlite3.db" "VACUUM;" \
129//!   && export USER_ID=1000 \
130//!   && docker run -it \
131//!     --env USER_ID="$USER_ID" \
132//!     --publish 3001:3001/tcp \
133//!     --volume "$(pwd)/storage/index/lib":"/var/lib/torrust/index" \
134//!     --volume "$(pwd)/storage/index/log":"/var/log/torrust/index" \
135//!     --volume "$(pwd)/storage/index/etc":"/etc/torrust/index" \
136//!     torrust/index:develop
137//! ```
138//!
139//! For more information about using docker visit the [tracker docker documentation](https://github.com/torrust/torrust-index/tree/develop/docker).
140//!
141//! ## Development
142//!
143//! We are using the [The Rust SQL Toolkit](https://github.com/launchbadge/sqlx)
144//! [(sqlx)](https://github.com/launchbadge/sqlx) for database migrations.
145//!
146//! You can install it with:
147//!
148//! ```text
149//! cargo install sqlx-cli
150//! ```
151//!
152//! To initialize the database run:
153//!
154//! ```text
155//! echo "DATABASE_URL=sqlite://data.db?mode=rwc" > .env
156//! sqlx db setup
157//! ```
158//!
159//! The `sqlx db setup` command will create the database specified in your
160//! `DATABASE_URL` and run any pending migrations.
161//!
162//! > **WARNING**: The `.env` file is also used by docker-compose.
163//!
164//! > **NOTICE**: Refer to the [sqlx-cli](https://github.com/launchbadge/sqlx/tree/main/sqlx-cli)
165//! > documentation for other commands to create new migrations or run them.
166//!
167//! > **NOTICE**: You can run the index with [tmux](https://github.com/tmux/tmux/wiki) with `tmux new -s torrust-index`.
168//!
169//! # Configuration
170//! In order to run the index you need to provide the configuration. If you run the index without providing the configuration,
171//! the tracker will generate the default configuration the first time you run it. It will generate a `config.toml` file with
172//! in the root directory.
173//!
174//! The default configuration is:
175//!
176//! ```toml
177//! [website]
178//! name = "Torrust"
179//!
180//! [tracker]
181//! api_url = "http://localhost:1212/"
182//! listed = false
183//! private = false
184//! token = "MyAccessToken"
185//! token_valid_seconds = 7257600
186//! url = "udp://localhost:6969"
187//!
188//! [net]
189//! bind_address = "0.0.0.0:3001"
190//!
191//! [auth]
192//! user_claim_token_pepper = "MaxVerstappenWC2021"
193//!
194//! [auth.password_constraints]
195//! min_password_length = 6
196//! max_password_length = 64
197//!
198//! [database]
199//! connect_url = "sqlite://data.db?mode=rwc"
200//!
201//! [mail]
202//! from = "example@email.com"
203//! reply_to = "noreply@email.com"
204//!
205//! [mail.smtp]
206//! port = 25
207//! server = ""
208//!
209//! [mail.smtp.credentials]
210//! password = ""
211//! username = ""
212//!
213//! [image_cache]
214//! max_request_timeout_ms = 1000
215//! capacity = 128000000
216//! entry_size_limit = 4000000
217//! user_quota_period_seconds = 3600
218//! user_quota_bytes = 64000000
219//!
220//! [api]
221//! default_torrent_page_size = 10
222//! max_torrent_page_size = 30
223//!
224//! [tracker_statistics_importer]
225//! torrent_info_update_interval = 3600
226//! port = 3002
227//! ```
228//!
229//! For more information about configuration you can visit the documentation for the [`config`]) module.
230//!
231//! Alternatively to the `config.toml` file you can use one environment variable `TORRUST_INDEX_CONFIG_TOML` to pass the configuration to the tracker:
232//!
233//! ```text
234//! TORRUST_INDEX_CONFIG_TOML=$(cat config.toml)
235//! cargo run
236//! ```
237//!
238//! In the previous example you are just setting the env var with the contents of the `config.toml` file.
239//!
240//! The env var contains the same data as the `config.toml`. It's particularly useful in you are [running the index with docker](https://github.com/torrust/torrust-index/tree/develop/docker).
241//!
242//! > **NOTICE**: The `TORRUST_INDEX_CONFIG_TOML` env var has priority over the `config.toml` file.
243//!
244//! > **NOTICE**: You can also change the location for the configuration file with the `TORRUST_INDEX_CONFIG_PATH` env var.
245//!
246//! # Usage
247//!
248//! ## API
249//!
250//! Running the index with the default configuration will expose the REST API on port 3001: <http://localhost:3001>
251//!
252//! ## Tracker Statistics Importer
253//!
254//! This console command allows you to manually import the tracker statistics.
255//!
256//! For more information about this command you can visit the documentation for
257//! the [`Import tracker statistics`](crate::console::commands::tracker_statistics_importer) module.
258//!
259//! ## Upgrader
260//!
261//! This console command allows you to manually upgrade the application from one
262//! version to another.
263//!
264//! For more information about this command you can visit the documentation for
265//! the [`Upgrade app from version 1.0.0 to 2.0.0`](crate::upgrades::from_v1_0_0_to_v2_0_0::upgrader) module.
266//!
267//! # Contributing
268//!
269//! If you want to contribute to this documentation you can:
270//!
271//! - [Open a new discussion](https://github.com/torrust/torrust-index/discussions)
272//! - [Open a new issue](https://github.com/torrust/torrust-index/issues).
273//! - [Open a new pull request](https://github.com/torrust/torrust-index/pulls).
274//!
275//! # Documentation
276//!
277//! You can find this documentation on [docs.rs](https://docs.rs/torrust-index/).
278//!
279//! If you want to contribute to this documentation you can [open a new pull request](https://github.com/torrust/torrust-index/pulls).
280//!
281//! In addition to the production code documentation you can find a lot of
282//! examples in the [tests](https://github.com/torrust/torrust-index/tree/develop/tests/e2e/contexts) directory.
283pub mod app;
284pub mod bootstrap;
285pub mod cache;
286pub mod common;
287pub mod config;
288pub mod console;
289pub mod databases;
290pub mod errors;
291pub mod mailer;
292pub mod models;
293pub mod services;
294pub mod tracker;
295pub mod ui;
296pub mod upgrades;
297pub mod utils;
298pub mod web;
299
300trait AsCSV {
301    fn as_csv<T>(&self) -> Result<Option<Vec<T>>, ()>
302    where
303        T: std::str::FromStr;
304}
305
306impl<S> AsCSV for Option<S>
307where
308    S: AsRef<str>,
309{
310    fn as_csv<T>(&self) -> Result<Option<Vec<T>>, ()>
311    where
312        T: std::str::FromStr,
313    {
314        match self {
315            Some(ref s) if !s.as_ref().trim().is_empty() => {
316                let mut acc = vec![];
317                for s in s.as_ref().split(',') {
318                    let item = s.trim().parse::<T>().map_err(|_| ())?;
319                    acc.push(item);
320                }
321                if acc.is_empty() {
322                    Ok(None)
323                } else {
324                    Ok(Some(acc))
325                }
326            }
327            _ => Ok(None),
328        }
329    }
330}