2 extern crate diesel_migrations;
7 dev::{Service, ServiceRequest, ServiceResponse},
9 header::{CACHE_CONTROL, CONTENT_TYPE},
15 r2d2::{ConnectionManager, Pool},
18 use lazy_static::lazy_static;
19 use lemmy_api::match_websocket_operation;
20 use lemmy_apub::activity_queue::create_activity_queue;
21 use lemmy_db::get_database_url_from_env;
22 use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
23 use lemmy_server::{code_migrations::run_advanced_migrations, routes::*};
24 use lemmy_structs::blocking;
25 use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX};
26 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
29 use tokio::sync::Mutex;
32 // static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
33 // Test out 1 hour here, this is breaking some things
34 static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60);
40 async fn main() -> Result<(), LemmyError> {
42 let settings = Settings::get();
44 // Set up the r2d2 connection pool
45 let db_url = match get_database_url_from_env() {
47 Err(_) => settings.get_database_url(),
49 let manager = ConnectionManager::<PgConnection>::new(&db_url);
50 let pool = Pool::builder()
51 .max_size(settings.database.pool_size)
53 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
55 // Run the migrations from code
56 blocking(&pool, move |conn| {
57 embedded_migrations::run(conn)?;
58 run_advanced_migrations(conn)?;
59 Ok(()) as Result<(), LemmyError>
63 // Set up the rate limiter
64 let rate_limiter = RateLimit {
65 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
69 "Starting http server at {}:{}",
70 settings.bind, settings.port
73 let activity_queue = create_activity_queue();
74 let chat_server = ChatServer::startup(
77 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
79 activity_queue.clone(),
83 // Create Http server with websocket support
84 HttpServer::new(move || {
85 let context = LemmyContext::create(
87 chat_server.to_owned(),
89 activity_queue.to_owned(),
91 let rate_limiter = rate_limiter.clone();
93 .wrap_fn(add_cache_headers)
94 .wrap(middleware::Logger::default())
97 .configure(|cfg| api::config(cfg, &rate_limiter))
98 .configure(federation::config)
99 .configure(feeds::config)
100 .configure(|cfg| images::config(cfg, &rate_limiter))
101 .configure(nodeinfo::config)
102 .configure(webfinger::config)
103 .service(actix_files::Files::new("/docs", "/app/documentation"))
105 .bind((settings.bind, settings.port))?
112 fn add_cache_headers<S>(
115 ) -> impl Future<Output = Result<ServiceResponse, Error>>
117 S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>,
119 let fut = srv.call(req);
121 let mut res = fut.await?;
122 if let Some(content_type) = res.headers().get(CONTENT_TYPE) {
123 if CACHE_CONTROL_REGEX.is_match(content_type.to_str().unwrap()) {
124 let header_val = HeaderValue::from_static(&CACHE_CONTROL_VALUE);
125 res.headers_mut().insert(CACHE_CONTROL, header_val);