1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use actix_web::web;
use anyhow::{Context, Result};
use sqlx::{Pool, Sqlite};

use self::db::Games;

mod db;
mod endpoints;

#[derive(Clone)]
pub struct GamesService {
    games: Games,
}

impl GamesService {
    /// Setup games DB and endpoints.
    ///
    /// This should be called after [`crate::auth::Auth`].
    pub async fn setup(pool: &'static Pool<Sqlite>) -> Result<Self> {
        Ok(Self {
            games: db::Games::init(pool)
                .await
                .context("Failed to initialize games")?,
        })
    }

    /// Configure actix-web application.
    pub fn configure(&self, cfg: &mut web::ServiceConfig) {
        cfg.app_data(web::Data::new(self.games.clone()));
        endpoints::configure(cfg);
    }
}