1use crate::{Config, Context, data::Store, routing::Router};
2use std::sync::Arc;
3
4struct RouterInitContext {
7 config: Config,
8 store: Arc<Store>,
9}
10
11pub struct App {
12 config: Config,
13 router: Arc<Router>,
14 store: Arc<Store>,
15}
16
17impl App {
18 pub fn new(config: Config, mut router: Router, store: Store) -> Self {
19 let store = Arc::new(store);
20 let context = RouterInitContext {
21 config: config.clone(),
22 store: store.clone(),
23 };
24 router.init(&context).expect("failed initializing router");
25 let router = Arc::new(router);
26 Self { config, router, store }
27 }
28}
29
30impl Context for RouterInitContext {
31 fn config(&self) -> &Config {
32 &self.config
33 }
34
35 fn router(&self) -> Arc<Router> {
36 panic!("Router cannot be accessed before it is initialized");
37 }
38
39 fn store(&self) -> Arc<Store> {
40 self.store.clone()
41 }
42}
43
44impl Context for App {
45 fn config(&self) -> &Config {
46 &self.config
47 }
48
49 fn router(&self) -> Arc<Router> {
50 self.router.clone()
51 }
52
53 fn store(&self) -> Arc<Store> {
54 self.store.clone()
55 }
56}