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_structs::blocking;
20 use lemmy_db::get_database_url_from_env;
21 use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
23 apub::activity_queue::create_activity_queue,
24 code_migrations::run_advanced_migrations,
26 websocket::chat_server::ChatServer,
29 use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX};
32 use tokio::sync::Mutex;
35 // static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
36 // Test out 1 hour here, this is breaking some things
37 static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60);
43 async fn main() -> Result<(), LemmyError> {
45 let settings = Settings::get();
47 // Set up the r2d2 connection pool
48 let db_url = match get_database_url_from_env() {
50 Err(_) => settings.get_database_url(),
52 let manager = ConnectionManager::<PgConnection>::new(&db_url);
53 let pool = Pool::builder()
54 .max_size(settings.database.pool_size)
56 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
58 // Run the migrations from code
59 blocking(&pool, move |conn| {
60 embedded_migrations::run(conn)?;
61 run_advanced_migrations(conn)?;
62 Ok(()) as Result<(), LemmyError>
66 // Set up the rate limiter
67 let rate_limiter = RateLimit {
68 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
72 "Starting http server at {}:{}",
73 settings.bind, settings.port
76 let activity_queue = create_activity_queue();
77 let chat_server = ChatServer::startup(
81 activity_queue.clone(),
85 // Create Http server with websocket support
86 HttpServer::new(move || {
87 let context = LemmyContext::create(
89 chat_server.to_owned(),
91 activity_queue.to_owned(),
93 let settings = Settings::get();
94 let rate_limiter = rate_limiter.clone();
96 .wrap_fn(add_cache_headers)
97 .wrap(middleware::Logger::default())
100 .configure(|cfg| api::config(cfg, &rate_limiter))
101 .configure(federation::config)
102 .configure(feeds::config)
103 .configure(|cfg| images::config(cfg, &rate_limiter))
104 .configure(index::config)
105 .configure(nodeinfo::config)
106 .configure(webfinger::config)
108 .service(actix_files::Files::new(
110 settings.front_end_dir.to_owned(),
112 .service(actix_files::Files::new(
114 settings.front_end_dir + "/documentation",
117 .bind((settings.bind, settings.port))?
124 fn add_cache_headers<S>(
127 ) -> impl Future<Output = Result<ServiceResponse, Error>>
129 S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>,
131 let fut = srv.call(req);
133 let mut res = fut.await?;
134 if let Some(content_type) = res.headers().get(CONTENT_TYPE) {
135 if CACHE_CONTROL_REGEX.is_match(content_type.to_str().unwrap()) {
136 let header_val = HeaderValue::from_static(&CACHE_CONTROL_VALUE);
137 res.headers_mut().insert(CACHE_CONTROL, header_val);