This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.rs
136 lines (122 loc) · 4.76 KB
/
web.rs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::net::IpAddr;
use std::str::FromStr;
use std::time;
use poem::listener::TcpListener;
use poem::middleware::{CatchPanic, CookieJarManager, Tracing};
use poem::{get, Endpoint, EndpointExt, Route, Server};
use views::r#static;
use crate::helpers::redis;
use crate::opts::WebOpts;
use crate::prelude::*;
use crate::wargaming::cache::account::{AccountInfoCache, AccountTanksCache};
use crate::wargaming::WargamingApi;
use crate::web::middleware::timeit::TimeItMiddleware;
use crate::web::middleware::{ErrorMiddleware, SecurityHeadersMiddleware, SentryMiddleware};
use crate::web::tracking_code::TrackingCode;
mod cookies;
mod i18n;
pub mod middleware;
mod partials;
#[cfg(test)]
mod test;
mod tracking_code;
mod views;
/// Run the web app.
pub async fn run(opts: WebOpts) -> Result {
sentry::configure_scope(|scope| scope.set_tag("app", "web"));
info!(host = opts.host.as_str(), port = opts.port, "starting up…");
let app_data = AppData::initialize_from_opts(&opts).await?;
let app = create_app(app_data).await?;
Server::new(TcpListener::bind((IpAddr::from_str(&opts.host)?, opts.port)))
.run_with_graceful_shutdown(
app,
async move {
let _ = tokio::signal::ctrl_c().await;
},
Some(time::Duration::from_secs(3)),
)
.await?;
Ok(())
}
struct AppData {
api: WargamingApi,
mongodb: mongodb::Database,
redis: fred::pool::RedisPool,
tracking_code: TrackingCode,
}
impl AppData {
async fn initialize_from_opts(opts: &WebOpts) -> Result<Self> {
let connections = &opts.connections;
let api = WargamingApi::new(
&connections.application_id,
connections.api_timeout,
connections.max_api_rps,
)?;
let mongodb = crate::database::mongodb::open(&connections.internal.mongodb_uri).await?;
let redis =
redis::connect(&connections.internal.redis_uri, connections.internal.redis_pool_size)
.await?;
let tracking_code = TrackingCode::new(opts)?;
Ok(Self {
api,
mongodb,
redis,
tracking_code,
})
}
}
#[instrument(skip_all)]
async fn create_app(data: AppData) -> Result<impl Endpoint> {
let app = create_standalone_app()
.await?
.data(data.mongodb)
.data(data.tracking_code)
.data(AccountInfoCache::new(data.api.clone(), data.redis.clone()))
.data(AccountTanksCache::new(data.api.clone(), data.redis.clone()))
.data(data.redis)
.data(data.api);
Ok(app)
}
#[instrument(skip_all)]
async fn create_standalone_app() -> Result<impl Endpoint> {
let app = Route::new()
.at("/site.webmanifest", get(r#static::get_site_manifest))
.at("/favicon.ico", get(r#static::get_favicon))
.at("/favicon-16x16.png", get(r#static::get_favicon_16x16))
.at("/favicon-32x32.png", get(r#static::get_favicon_32x32))
.at("/android-chrome-192x192.png", get(r#static::get_android_chrome_192x192))
.at("/android-chrome-512x512.png", get(r#static::get_android_chrome_512x512))
.at("/apple-touch-icon.png", get(r#static::get_apple_touch_icon))
.at("/static/table.js", get(r#static::get_table_js))
.at("/static/navbar.js", get(r#static::get_navbar_js))
.at("/static/theme.css", get(r#static::get_theme_css))
.at("/robots.txt", get(r#static::get_robots_txt))
.at("/static/flags/cn.svg", get(r#static::get_cn_svg))
.at("/static/flags/de.svg", get(r#static::get_de_svg))
.at("/static/flags/eu.svg", get(r#static::get_eu_svg))
.at("/static/flags/fr.svg", get(r#static::get_fr_svg))
.at("/static/flags/gb.svg", get(r#static::get_gb_svg))
.at("/static/flags/jp.svg", get(r#static::get_jp_svg))
.at("/static/flags/su.svg", get(r#static::get_su_svg))
.at("/static/flags/us.svg", get(r#static::get_us_svg))
.at("/static/flags/xx.svg", get(r#static::get_xx_svg))
.at("/", get(views::index::get))
.at("/search", get(views::search::get))
.at("/:realm/:account_id", get(views::player::get).post(views::player::post))
.at("/error", get(views::error::get_error))
.at("/random", get(views::random::get_random))
.at("/sitemaps/:realm/sitemap.txt", get(views::sitemaps::get_sitemap))
.at("/api/health", get(views::api::get_health))
.at(
"/api/:realm/accounts/:since/active-since",
get(views::api::get_active_since).with(TimeItMiddleware),
)
.data(i18n::build_resources()?)
.with(Tracing)
.with(CatchPanic::new())
.with(ErrorMiddleware)
.with(SecurityHeadersMiddleware)
.with(SentryMiddleware)
.with(CookieJarManager::new());
Ok(app)
}