forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Shared application state handed to every router via Topcoat's app context. |
| 2 | |
| 3 | use std::path::PathBuf; |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use forge_core::{Config, Store}; |
| 7 | use forge_git::GitEnv; |
| 8 | use tokio::sync::Notify; |
| 9 | |
| 10 | use crate::throttle::Throttle; |
| 11 | |
| 12 | #[derive(Clone)] |
| 13 | pub struct AppState { |
| 14 | /// The config the process booted with: the file plus every stored override. Live settings |
| 15 | /// can differ from it, so read those through `live()`. |
| 16 | pub config: Arc<Config>, |
| 17 | /// The admin-editable settings layer: file config, boot config and the live view. |
| 18 | pub settings: Arc<forge_core::server_settings::Live>, |
| 19 | pub store: Store, |
| 20 | pub git: Arc<GitEnv>, |
| 21 | /// Instance secret key (data_dir/forge.key). Signs private raw-file URLs. |
| 22 | pub key: Arc<[u8; 32]>, |
| 23 | /// Random per boot. Presented by hook processes on the internal API. |
| 24 | pub internal_token: Arc<str>, |
| 25 | /// Per boot. Presented by the CLI on the `/internal/admin/*` routes. Also written to |
| 26 | /// `data_dir/run/admin.token` with mode 0600, which is how the CLI reads it. |
| 27 | pub admin_token: Arc<str>, |
| 28 | /// The single backup and fsck run slot of this process. |
| 29 | pub jobs: Arc<crate::backup::Jobs>, |
| 30 | /// Absolute path to the internal unix socket (data_dir/internal.sock). |
| 31 | pub internal_socket: PathBuf, |
| 32 | /// Failed-authentication counters for login and git HTTP. Shared by every request. |
| 33 | pub throttle: Arc<Throttle>, |
| 34 | /// Wakes the webhook delivery worker when a delivery is queued. |
| 35 | pub webhook_waker: Arc<Notify>, |
| 36 | /// Wakes the mirror worker on "sync now" and after a push to a repo with push mirrors. |
| 37 | pub mirror_waker: Arc<Notify>, |
| 38 | /// Release asset bytes under `data_dir/blobs`. |
| 39 | pub blobs: crate::blobs::BlobStore, |
| 40 | /// Standby replication queue: repos to push after a ref update, and the reconcile-now flag. |
| 41 | pub replication: Arc<crate::replication::ReplicationQueue>, |
| 42 | /// Wakes long-polling runner claims when a CI job becomes Queued. |
| 43 | pub ci_waker: Arc<Notify>, |
| 44 | /// Outgoing mail transport: SMTP, or the log transport in dev and tests. |
| 45 | pub mailer: Arc<dyn crate::mail::Mailer>, |
| 46 | /// Wakes the mail worker when a message is queued or the mail settings change. |
| 47 | pub mail_waker: Arc<Notify>, |
| 48 | /// Open notification streams per user. A change to an inbox publishes the new unread count. |
| 49 | pub notify_hub: Arc<crate::notify_hub::NotifyHub>, |
| 50 | /// Change counters of CI jobs and runs. Log followers and run pages wait on them. |
| 51 | pub ci_live: Arc<crate::ci::CiLive>, |
| 52 | /// The log each CI runner sent, kept in memory for its admin page. See `ci::runner_log`. |
| 53 | pub runner_logs: Arc<crate::ci::runner_log::RunnerLogs>, |
| 54 | /// The host CPU, memory and network samples of each CI runner, for the graphs on its page. See |
| 55 | /// `ci::runner_metrics`. |
| 56 | pub runner_metrics: Arc<crate::ci::runner_metrics::RunnerMetrics>, |
| 57 | /// Change signals of live pages, keyed by topic. See `live`. |
| 58 | pub live: Arc<crate::live::LiveHub>, |
| 59 | /// Device changes per user. The settings pages showing a user's push devices listen to it. |
| 60 | pub push_live: Arc<crate::web_push::PushLive>, |
| 61 | pub packages_live: Arc<crate::packages_live::PackagesLive>, |
| 62 | } |
| 63 | |
| 64 | /// A settings handle over `config` with no stored overrides and no file keys. For tests. |
| 65 | #[cfg(any(test, feature = "test-support"))] |
| 66 | pub fn settings_for(config: &Arc<Config>) -> Arc<forge_core::server_settings::Live> { |
| 67 | Arc::new(forge_core::server_settings::Live::new( |
| 68 | config.clone(), |
| 69 | Default::default(), |
| 70 | config.clone(), |
| 71 | )) |
| 72 | } |
| 73 | |
| 74 | impl AppState { |
| 75 | /// The config the running process uses now: `config` with the live settings applied. Take it |
| 76 | /// once per request or pass; a later save does not change a value already read. |
| 77 | pub fn live(&self) -> Arc<Config> { |
| 78 | self.settings.current() |
| 79 | } |
| 80 | |
| 81 | /// Replaces the config and resets the settings layer over it. For tests. |
| 82 | #[cfg(any(test, feature = "test-support"))] |
| 83 | pub fn set_config(&mut self, config: Config) { |
| 84 | self.config = Arc::new(config); |
| 85 | self.settings = settings_for(&self.config); |
| 86 | } |
| 87 | } |