1- use  std:: sync:: Arc ; 
21use  std:: str:: FromStr ; 
2+ use  std:: sync:: Arc ; 
33use  std:: time:: Duration ; 
44
5+ use  axum:: extract:: { Path ,  State } ; 
6+ use  axum:: routing:: { delete,  post} ; 
57use  axum:: { Json ,  Router } ; 
6- use  axum:: routing:: post; 
7- use  axum:: extract:: State ; 
88use  color_eyre:: eyre:: Result ; 
99use  hyper:: server:: accept:: Accept ; 
1010use  serde:: { Deserialize ,  Deserializer ,  Serialize } ; 
1111use  tokio:: io:: { AsyncRead ,  AsyncWrite } ; 
1212
13- use  crate :: meta:: Store ; 
1413use  crate :: allocation:: config:: { AllocConfig ,  DbConfig } ; 
14+ use  crate :: linc:: bus:: Bus ; 
1515use  crate :: linc:: NodeId ; 
16+ use  crate :: manager:: Manager ; 
17+ use  crate :: meta:: DatabaseId ; 
1618
1719pub  struct  Config  { 
18-     pub  meta_store :  Arc < Store > , 
20+     pub  bus :  Arc < Bus < Arc < Manager > > > , 
1921} 
2022
2123struct  AdminServerState  { 
22-     meta_store :  Arc < Store > , 
24+     bus :  Arc < Bus < Arc < Manager > > > , 
2325} 
2426
2527pub  async  fn  run_admin_api < I > ( config :  Config ,  listener :  I )  -> Result < ( ) > 
2628where 
2729    I :  Accept < Error  = std:: io:: Error > , 
2830    I :: Conn :  AsyncRead  + AsyncWrite  + Send  + Unpin  + ' static , 
2931{ 
30-     let  state = AdminServerState  { 
31-         meta_store :  config. meta_store , 
32-     } ; 
32+     let  state = AdminServerState  {  bus :  config. bus  } ; 
3333
3434    let  app = Router :: new ( ) 
3535        . route ( "/manage/allocation" ,  post ( allocate) . get ( list_allocs) ) 
36+         . route ( "/manage/allocation/:db_name" ,  delete ( deallocate) ) 
3637        . with_state ( Arc :: new ( state) ) ; 
3738    axum:: Server :: builder ( listener) 
3839        . serve ( app. into_make_service ( ) ) 
@@ -49,7 +50,7 @@ struct AllocateResp {}
4950
5051#[ derive( Deserialize ,  Debug ) ]  
5152struct  AllocateReq  { 
52-     alloc_id :  String , 
53+     database_name :  String , 
5354    max_conccurent_connection :  Option < u32 > , 
5455    config :  DbConfigReq , 
5556} 
@@ -60,7 +61,10 @@ pub enum DbConfigReq {
6061    Primary  { } , 
6162    Replica  { 
6263        primary_node_id :  NodeId , 
63-         #[ serde( deserialize_with = "deserialize_duration" ,  default  = "default_proxy_timeout" ) ]  
64+         #[ serde(  
65+             deserialize_with = "deserialize_duration" ,  
66+             default  = "default_proxy_timeout"  
67+         ) ]  
6468        proxy_request_timeout_duration :  Duration , 
6569    } , 
6670} 
7882        type  Value  = Duration ; 
7983
8084        fn  visit_str < E > ( self ,  v :  & str )  -> std:: result:: Result < Self :: Value ,  E > 
81-              where 
82-                  E :  serde:: de:: Error ,   
85+         where 
86+             E :  serde:: de:: Error , 
8387        { 
8488            match  humantime:: Duration :: from_str ( v)  { 
8589                Ok ( d)  => Ok ( * d) , 
9094        fn  expecting ( & self ,  f :  & mut  std:: fmt:: Formatter )  -> std:: fmt:: Result  { 
9195            f. write_str ( "a duration, in a string format" ) 
9296        } 
93- 
9497    } 
9598
9699    deserializer. deserialize_str ( Visitor ) 
@@ -102,7 +105,7 @@ async fn allocate(
102105)  -> Result < Json < AllocateResp > ,  Json < ErrorResponse > >  { 
103106    let  config = AllocConfig  { 
104107        max_conccurent_connection :  req. max_conccurent_connection . unwrap_or ( 16 ) , 
105-         db_name :  req. alloc_id . clone ( ) , 
108+         db_name :  req. database_name . clone ( ) , 
106109        db_config :  match  req. config  { 
107110            DbConfigReq :: Primary  { }  => DbConfig :: Primary  { } , 
108111            DbConfigReq :: Replica  { 
@@ -114,7 +117,20 @@ async fn allocate(
114117            } , 
115118        } , 
116119    } ; 
117-     state. meta_store . allocate ( & req. alloc_id ,  & config) . await ; 
120+ 
121+     let  dispatcher = state. bus . clone ( ) ; 
122+     let  id = DatabaseId :: from_name ( & req. database_name ) ; 
123+     state. bus . handler ( ) . allocate ( id,  & config,  dispatcher) . await ; 
124+ 
125+     Ok ( Json ( AllocateResp  { } ) ) 
126+ } 
127+ 
128+ async  fn  deallocate ( 
129+     State ( state) :  State < Arc < AdminServerState > > , 
130+     Path ( database_name) :  Path < String > , 
131+ )  -> Result < Json < AllocateResp > ,  Json < ErrorResponse > >  { 
132+     let  id = DatabaseId :: from_name ( & database_name) ; 
133+     state. bus . handler ( ) . deallocate ( id) . await ; 
118134
119135    Ok ( Json ( AllocateResp  { } ) ) 
120136} 
@@ -133,7 +149,9 @@ async fn list_allocs(
133149    State ( state) :  State < Arc < AdminServerState > > , 
134150)  -> Result < Json < ListAllocResp > ,  Json < ErrorResponse > >  { 
135151    let  allocs = state
136-         . meta_store 
152+         . bus 
153+         . handler ( ) 
154+         . store ( ) 
137155        . list_allocs ( ) 
138156        . await 
139157        . into_iter ( ) 
0 commit comments