| 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, OwnerSeed, is_claimed, port::GitStorage, seed_owner, sweep_interrupted_imports, |
| 26 | sweep_orphan_directories, |
| 27 | }, |
| 28 | domain::SetupToken, |
| 29 | infrastructure::{ |
| 30 | self, |
| 31 | cat_file::CatFilePool, |
| 32 | git::{DiskGitStorage, GitFeatures}, |
| 33 | password::Argon2Hasher, |
| 34 | repository::{SqliteMembershipRepo, SqliteOrgRepo, SqliteRepoRepo, SqliteUserRepo}, |
| 35 | web::{ |
| 36 | context::SetupState, |
| 37 | csrf::CsrfSecret, |
| 38 | import::ImportQueue, |
| 39 | instance::StartedAt, |
| 40 | rate_limit::{ |
| 41 | ArchiveLimiter, BackupLimiter, GitAuthLimiter, InstanceLimiter, MetricsLimiter, |
| 42 | RateLimiter, ResetLimiter, SearchLimiter, |
| 43 | }, |
| 44 | session_cookie::InsecureCookieTokenStore, |
| 45 | }, |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | pub async fn build( |
| 61 | mut config: AppConfig, |
| 62 | pool: SqlitePool, |
| 63 | assets: AssetBundle, |
| 64 | setup: Option<SetupToken>, |
| 65 | ) -> RouterBuilder { |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | config.setup_token = None; |
| 71 | |
| 72 | let insecure_cookies = config.insecure_cookies; |
| 73 | |
| 74 | let mut builder = Router::builder() |
| 75 | .assets(assets) |
| 76 | .cookies() |
| 77 | .sessions(session_config(insecure_cookies)) |
| 78 | .discover() |
| 79 | .app_context(config) |
| 80 | .app_context(pool) |
| 81 | |
| 82 | |
| 83 | .app_context(RateLimiter::default()) |
| 84 | |
| 85 | |
| 86 | |
| 87 | .app_context(GitAuthLimiter::default()) |
| 88 | |
| 89 | .app_context(ArchiveLimiter::default()) |
| 90 | |
| 91 | |
| 92 | .app_context(InstanceLimiter::default()) |
| 93 | |
| 94 | |
| 95 | |
| 96 | .app_context(StartedAt::now()) |
| 97 | |
| 98 | |
| 99 | |
| 100 | .app_context(MetricsLimiter::default()) |
| 101 | |
| 102 | |
| 103 | .app_context(BackupLimiter::default()) |
| 104 | |
| 105 | |
| 106 | |
| 107 | .app_context(ResetLimiter::default()) |
| 108 | |
| 109 | |
| 110 | .app_context(SearchLimiter::default()) |
| 111 | |
| 112 | |
| 113 | .app_context(ImportQueue::default()) |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | .app_context(std::sync::Arc::new(CatFilePool::new())) |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | .app_context(infrastructure::highlight::cache().clone()) |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | .app_context(std::sync::Arc::new( |
| 130 | infrastructure::merge_cache::MergeCache::new(), |
| 131 | )) |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | .app_context(infrastructure::metrics::metrics().clone()) |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | .app_context(GitFeatures::detect().await) |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | .app_context(CsrfSecret::generate()); |
| 149 | |
| 150 | if let Some(token) = setup { |
| 151 | builder = builder.app_context(SetupState(token)); |
| 152 | } |
| 153 | |
| 154 | builder |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | pub async fn sweep_on_boot(config: &AppConfig, pool: &SqlitePool) { |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | match sweep_interrupted_imports(&SqliteRepoRepo::new(pool.clone())).await { |
| 174 | Ok(0) => {} |
| 175 | Ok(swept) => println!("steid: {swept} interrupted import(s) marked as failed"), |
| 176 | |
| 177 | |
| 178 | Err(error) => eprintln!("steid: interrupted imports could not be swept: {error}"), |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | match DiskGitStorage::new(config.data_dir.clone()) |
| 192 | .enable_reflogs() |
| 193 | .await |
| 194 | { |
| 195 | Ok(0) => {} |
| 196 | Ok(fixed) => println!("steid: reflog enabled on {fixed} bare repository/repositories"), |
| 197 | |
| 198 | |
| 199 | Err(error) => eprintln!("steid: reflogs could not be swept: {error}"), |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | match sweep_orphan_directories( |
| 213 | std::time::SystemTime::now(), |
| 214 | &SqliteOrgRepo::new(pool.clone()), |
| 215 | &SqliteRepoRepo::new(pool.clone()), |
| 216 | &DiskGitStorage::new(config.data_dir.clone()), |
| 217 | ) |
| 218 | .await |
| 219 | { |
| 220 | |
| 221 | |
| 222 | Ok(_) => {} |
| 223 | |
| 224 | |
| 225 | Err(error) => { |
| 226 | eprintln!("steid: orphaned repository directories could not be swept: {error}") |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | pub async fn seed_owner_on_boot( |
| 255 | config: &AppConfig, |
| 256 | pool: &SqlitePool, |
| 257 | ) -> Result<Option<OwnerSeed>, crate::application::Error> { |
| 258 | let Some(seed) = config.owner_seed()? else { |
| 259 | return Ok(None); |
| 260 | }; |
| 261 | |
| 262 | let users = SqliteUserRepo::new(pool.clone()); |
| 263 | if is_claimed(&users).await? { |
| 264 | return Ok(None); |
| 265 | } |
| 266 | |
| 267 | seed_owner( |
| 268 | &seed, |
| 269 | &users, |
| 270 | &SqliteOrgRepo::new(pool.clone()), |
| 271 | &SqliteMembershipRepo::new(pool.clone()), |
| 272 | &Argon2Hasher::new(), |
| 273 | ) |
| 274 | .await?; |
| 275 | |
| 276 | Ok(Some(seed)) |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | fn session_config(insecure_cookies: bool) -> SessionConfig { |
| 289 | if insecure_cookies { |
| 290 | SessionConfig::builder() |
| 291 | .token_store(InsecureCookieTokenStore::new()) |
| 292 | .build() |
| 293 | } else { |
| 294 | SessionConfig::default() |
| 295 | } |
| 296 | } |