1- use std:: net:: SocketAddr ;
2- use std:: path:: PathBuf ;
3- use std:: str:: FromStr ;
4-
51use axum:: routing:: get;
62use axum:: { http, Router } ;
7- use axum_server:: tls_rustls:: RustlsConfig ;
83use clap:: Args ;
94
105use quinn:: rustls;
@@ -13,7 +8,7 @@ use tower_http::cors::{Any, CorsLayer};
138use tower_http:: decompression:: RequestDecompressionLayer ;
149use tower_http:: trace:: TraceLayer ;
1510
16- use common:: model:: { CommonOptions , P2pOptions } ;
11+ use common:: model:: { CommonHttpOptions , P2pOptions } ;
1712use jupiter:: context:: Context ;
1813use mono:: api:: lfs:: lfs_router;
1914use mono:: api:: MonoApiServiceState ;
@@ -24,65 +19,15 @@ use crate::api::{github_router, MegaApiServiceState};
2419#[ derive( Args , Clone , Debug ) ]
2520pub struct HttpOptions {
2621 #[ clap( flatten) ]
27- pub common : CommonOptions ,
22+ pub common : CommonHttpOptions ,
2823
2924 #[ clap( flatten) ]
3025 pub p2p : P2pOptions ,
31-
32- #[ arg( long, default_value_t = 8000 ) ]
33- pub http_port : u16 ,
34- }
35-
36- #[ derive( Args , Clone , Debug ) ]
37- pub struct HttpsOptions {
38- #[ clap( flatten) ]
39- pub common : CommonOptions ,
40-
41- #[ clap( flatten) ]
42- pub p2p : P2pOptions ,
43-
44- #[ arg( long, default_value_t = 443 ) ]
45- pub https_port : u16 ,
46-
47- #[ arg( long, value_name = "FILE" ) ]
48- pub https_key_path : PathBuf ,
49-
50- #[ arg( long, value_name = "FILE" ) ]
51- pub https_cert_path : PathBuf ,
52- }
53-
54- pub async fn https_server ( context : Context , options : HttpsOptions ) {
55- let HttpsOptions {
56- common : CommonOptions { host, .. } ,
57- https_key_path,
58- https_cert_path,
59- https_port,
60- p2p,
61- } = options. clone ( ) ;
62-
63- rustls:: crypto:: ring:: default_provider ( )
64- . install_default ( )
65- . expect ( "Failed to install rustls crypto provider" ) ;
66-
67- check_run_with_p2p ( context. clone ( ) , options. p2p . clone ( ) ) ;
68-
69- let app = app ( context, host. clone ( ) , https_port, p2p. clone ( ) ) . await ;
70-
71- let server_url = format ! ( "{}:{}" , host, https_port) ;
72- let addr = SocketAddr :: from_str ( & server_url) . unwrap ( ) ;
73- let config = RustlsConfig :: from_pem_file ( https_cert_path. to_owned ( ) , https_key_path. to_owned ( ) )
74- . await
75- . unwrap ( ) ;
76- axum_server:: bind_rustls ( addr, config)
77- . serve ( app. into_make_service ( ) )
78- . await
79- . unwrap ( ) ;
8026}
8127
8228pub async fn http_server ( context : Context , options : HttpOptions ) {
8329 let HttpOptions {
84- common : CommonOptions { host, .. } ,
85- http_port,
30+ common : CommonHttpOptions { host, port, .. } ,
8631 p2p,
8732 } = options. clone ( ) ;
8833
@@ -92,38 +37,33 @@ pub async fn http_server(context: Context, options: HttpOptions) {
9237
9338 check_run_with_p2p ( context. clone ( ) , options. p2p . clone ( ) ) ;
9439
95- let app = app ( context, host. clone ( ) , http_port , p2p. clone ( ) ) . await ;
40+ let app = app ( context, host. clone ( ) , port , p2p. clone ( ) ) . await ;
9641
97- let server_url = format ! ( "{}:{}" , host, http_port ) ;
42+ let server_url = format ! ( "{}:{}" , host, port ) ;
9843
99- let addr = SocketAddr :: from_str ( & server_url) . unwrap ( ) ;
100- let listener = tokio:: net:: TcpListener :: bind ( addr) . await . unwrap ( ) ;
44+ let listener = tokio:: net:: TcpListener :: bind ( server_url) . await . unwrap ( ) ;
10145 axum:: serve ( listener, app. into_make_service ( ) )
10246 . await
10347 . unwrap ( ) ;
10448}
10549
10650pub async fn app ( context : Context , host : String , port : u16 , p2p : P2pOptions ) -> Router {
10751 let state = AppState {
108- host,
52+ host : host . clone ( ) ,
10953 port,
11054 context : context. clone ( ) ,
11155 } ;
11256
113- let mega_api_state = MegaApiServiceState {
114- inner : MonoApiServiceState {
115- context : context. clone ( ) ,
116- oauth_client : None ,
117- store : None ,
118- } ,
119- p2p,
120- port,
121- } ;
122-
12357 let mono_api_state = MonoApiServiceState {
12458 context : context. clone ( ) ,
12559 oauth_client : None ,
12660 store : None ,
61+ listen_addr : format ! ( "http://{}:{}" , host, port) ,
62+ } ;
63+
64+ let mega_api_state = MegaApiServiceState {
65+ inner : mono_api_state. clone ( ) ,
66+ p2p,
12767 } ;
12868
12969 pub fn mega_routers ( ) -> Router < MegaApiServiceState > {
0 commit comments