1 extern crate lemmy_server;
3 extern crate diesel_migrations;
5 pub extern crate lazy_static;
7 pub type DbPool = Pool<ConnectionManager<PgConnection>>;
9 use crate::lemmy_server::actix_web::dev::Service;
10 use actix::prelude::*;
14 dev::{ServiceRequest, ServiceResponse},
16 header::{CACHE_CONTROL, CONTENT_TYPE},
22 r2d2::{ConnectionManager, Pool},
25 use lemmy_db::get_database_url_from_env;
28 code_migrations::run_advanced_migrations,
29 rate_limit::{rate_limiter::RateLimiter, RateLimit},
34 use lemmy_utils::{settings::Settings, CACHE_CONTROL_REGEX};
36 use tokio::sync::Mutex;
39 // static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
40 // Test out 1 hour here, this is breaking some things
41 static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60);
47 async fn main() -> Result<(), LemmyError> {
49 let settings = Settings::get();
51 // Set up the r2d2 connection pool
52 let db_url = match get_database_url_from_env() {
54 Err(_) => settings.get_database_url(),
56 let manager = ConnectionManager::<PgConnection>::new(&db_url);
57 let pool = Pool::builder()
58 .max_size(settings.database.pool_size)
60 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
62 // Run the migrations from code
63 blocking(&pool, move |conn| {
64 embedded_migrations::run(conn)?;
65 run_advanced_migrations(conn)?;
66 Ok(()) as Result<(), LemmyError>
70 // Set up the rate limiter
71 let rate_limiter = RateLimit {
72 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
75 // Set up websocket server
76 let server = ChatServer::startup(pool.clone(), rate_limiter.clone(), Client::default()).start();
79 "Starting http server at {}:{}",
80 settings.bind, settings.port
83 // Create Http server with websocket support
84 HttpServer::new(move || {
85 let settings = Settings::get();
86 let rate_limiter = rate_limiter.clone();
88 .wrap_fn(add_cache_headers)
89 .wrap(middleware::Logger::default())
92 .data(Client::default())
94 .configure(|cfg| api::config(cfg, &rate_limiter))
95 .configure(federation::config)
96 .configure(feeds::config)
97 .configure(|cfg| images::config(cfg, &rate_limiter))
98 .configure(index::config)
99 .configure(nodeinfo::config)
100 .configure(webfinger::config)
102 .service(actix_files::Files::new(
104 settings.front_end_dir.to_owned(),
106 .service(actix_files::Files::new(
108 settings.front_end_dir + "/documentation",
111 .bind((settings.bind, settings.port))?
118 fn add_cache_headers<S>(
121 ) -> impl Future<Output = Result<ServiceResponse, Error>>
123 S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>,
125 let fut = srv.call(req);
127 let mut res = fut.await?;
128 if let Some(content_type) = res.headers().get(CONTENT_TYPE) {
129 if CACHE_CONTROL_REGEX.is_match(content_type.to_str().unwrap()) {
130 let header_val = HeaderValue::from_static(&CACHE_CONTROL_VALUE);
131 res.headers_mut().insert(CACHE_CONTROL, header_val);