| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use sqlx::SqlitePool; |
| 16 | use topcoat::{ |
| 17 | asset::{AssetBundle, RouterBuilderAssetExt}, |
| 18 | cookie::RouterBuilderCookieExt, |
| 19 | router::{Router, RouterBuilder, RouterBuilderDiscoverExt}, |
| 20 | session::{RouterBuilderSessionExt, SessionConfig}, |
| 21 | }; |
| 22 | |
| 23 | use crate::{ |
| 24 | application::{ |
| 25 | AppConfig, port::GitStorage, sweep_interrupted_imports, sweep_orphan_directories, |
| 26 | }, |
| 27 | domain::SetupToken, |
| 28 | infrastructure::{ |
| 29 | self, |
| 30 | cat_file::CatFilePool, |
| 31 | git::{DiskGitStorage, GitFeatures}, |
| 32 | repository::{SqliteOrgRepo, SqliteRepoRepo}, |
| 33 | web::{ |
| 34 | context::SetupState, |
| 35 | csrf::CsrfSecret, |
| 36 | import::ImportQueue, |
| 37 | instance::StartedAt, |
| 38 | rate_limit::{ |
| 39 | ArchiveLimiter, BackupLimiter, GitAuthLimiter, InstanceLimiter, MetricsLimiter, |
| 40 | RateLimiter, ResetLimiter, SearchLimiter, |
| 41 | }, |
| 42 | session_cookie::InsecureCookieTokenStore, |
| 43 | }, |
| 44 | }, |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub async fn build( |
| 59 | mut config: AppConfig, |
| 60 | pool: SqlitePool, |
| 61 | assets: AssetBundle, |
| 62 | setup: Option<SetupToken>, |
| 63 | ) -> RouterBuilder { |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | config.setup_token = None; |
| 69 | |
| 70 | let insecure_cookies = config.insecure_cookies; |
| 71 | |
| 72 | let mut builder = Router::builder() |
| 73 | .assets(assets) |
| 74 | .cookies() |
| 75 | .sessions(session_config(insecure_cookies)) |
| 76 | .discover() |
| 77 | .app_context(config) |
| 78 | .app_context(pool) |
| 79 | |
| 80 | |
| 81 | .app_context(RateLimiter::default()) |
| 82 | |
| 83 | |
| 84 | |
| 85 | .app_context(GitAuthLimiter::default()) |
| 86 | |
| 87 | .app_context(ArchiveLimiter::default()) |
| 88 | |
| 89 | |
| 90 | .app_context(InstanceLimiter::default()) |
| 91 | |
| 92 | |
| 93 | |
| 94 | .app_context(StartedAt::now()) |
| 95 | |
| 96 | |
| 97 | |
| 98 | .app_context(MetricsLimiter::default()) |
| 99 | |
| 100 | |
| 101 | .app_context(BackupLimiter::default()) |
| 102 | |
| 103 | |
| 104 | |
| 105 | .app_context(ResetLimiter::default()) |
| 106 | |
| 107 | |
| 108 | .app_context(SearchLimiter::default()) |
| 109 | |
| 110 | |
| 111 | .app_context(ImportQueue::default()) |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | .app_context(std::sync::Arc::new(CatFilePool::new())) |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | .app_context(infrastructure::highlight::cache().clone()) |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | .app_context(std::sync::Arc::new( |
| 128 | infrastructure::merge_cache::MergeCache::new(), |
| 129 | )) |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | .app_context(infrastructure::metrics::metrics().clone()) |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | .app_context(GitFeatures::detect().await) |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | .app_context(CsrfSecret::generate()); |
| 147 | |
| 148 | if let Some(token) = setup { |
| 149 | builder = builder.app_context(SetupState(token)); |
| 150 | } |
| 151 | |
| 152 | builder |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | pub async fn sweep_on_boot(config: &AppConfig, pool: &SqlitePool) { |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | match sweep_interrupted_imports(&SqliteRepoRepo::new(pool.clone())).await { |
| 172 | Ok(0) => {} |
| 173 | Ok(swept) => println!("steid: {swept} interrupted import(s) marked as failed"), |
| 174 | |
| 175 | |
| 176 | Err(error) => eprintln!("steid: interrupted imports could not be swept: {error}"), |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | match DiskGitStorage::new(config.data_dir.clone()) |
| 190 | .enable_reflogs() |
| 191 | .await |
| 192 | { |
| 193 | Ok(0) => {} |
| 194 | Ok(fixed) => println!("steid: reflog enabled on {fixed} bare repository/repositories"), |
| 195 | |
| 196 | |
| 197 | Err(error) => eprintln!("steid: reflogs could not be swept: {error}"), |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | match sweep_orphan_directories( |
| 211 | std::time::SystemTime::now(), |
| 212 | &SqliteOrgRepo::new(pool.clone()), |
| 213 | &SqliteRepoRepo::new(pool.clone()), |
| 214 | &DiskGitStorage::new(config.data_dir.clone()), |
| 215 | ) |
| 216 | .await |
| 217 | { |
| 218 | |
| 219 | |
| 220 | Ok(_) => {} |
| 221 | |
| 222 | |
| 223 | Err(error) => { |
| 224 | eprintln!("steid: orphaned repository directories could not be swept: {error}") |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | fn session_config(insecure_cookies: bool) -> SessionConfig { |
| 239 | if insecure_cookies { |
| 240 | SessionConfig::builder() |
| 241 | .token_store(InsecureCookieTokenStore::new()) |
| 242 | .build() |
| 243 | } else { |
| 244 | SessionConfig::default() |
| 245 | } |
| 246 | } |