1
- use std:: net:: SocketAddr ;
2
- use std:: path:: PathBuf ;
3
- use std:: str:: FromStr ;
4
-
5
1
use axum:: routing:: get;
6
2
use axum:: { http, Router } ;
7
- use axum_server:: tls_rustls:: RustlsConfig ;
8
3
use clap:: Args ;
9
4
10
5
use quinn:: rustls;
@@ -13,7 +8,7 @@ use tower_http::cors::{Any, CorsLayer};
13
8
use tower_http:: decompression:: RequestDecompressionLayer ;
14
9
use tower_http:: trace:: TraceLayer ;
15
10
16
- use common:: model:: { CommonOptions , P2pOptions } ;
11
+ use common:: model:: { CommonHttpOptions , P2pOptions } ;
17
12
use jupiter:: context:: Context ;
18
13
use mono:: api:: lfs:: lfs_router;
19
14
use mono:: api:: MonoApiServiceState ;
@@ -24,65 +19,15 @@ use crate::api::{github_router, MegaApiServiceState};
24
19
#[ derive( Args , Clone , Debug ) ]
25
20
pub struct HttpOptions {
26
21
#[ clap( flatten) ]
27
- pub common : CommonOptions ,
22
+ pub common : CommonHttpOptions ,
28
23
29
24
#[ clap( flatten) ]
30
25
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 ( ) ;
80
26
}
81
27
82
28
pub async fn http_server ( context : Context , options : HttpOptions ) {
83
29
let HttpOptions {
84
- common : CommonOptions { host, .. } ,
85
- http_port,
30
+ common : CommonHttpOptions { host, port, .. } ,
86
31
p2p,
87
32
} = options. clone ( ) ;
88
33
@@ -92,38 +37,33 @@ pub async fn http_server(context: Context, options: HttpOptions) {
92
37
93
38
check_run_with_p2p ( context. clone ( ) , options. p2p . clone ( ) ) ;
94
39
95
- let app = app ( context, host. clone ( ) , http_port , p2p. clone ( ) ) . await ;
40
+ let app = app ( context, host. clone ( ) , port , p2p. clone ( ) ) . await ;
96
41
97
- let server_url = format ! ( "{}:{}" , host, http_port ) ;
42
+ let server_url = format ! ( "{}:{}" , host, port ) ;
98
43
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 ( ) ;
101
45
axum:: serve ( listener, app. into_make_service ( ) )
102
46
. await
103
47
. unwrap ( ) ;
104
48
}
105
49
106
50
pub async fn app ( context : Context , host : String , port : u16 , p2p : P2pOptions ) -> Router {
107
51
let state = AppState {
108
- host,
52
+ host : host . clone ( ) ,
109
53
port,
110
54
context : context. clone ( ) ,
111
55
} ;
112
56
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
-
123
57
let mono_api_state = MonoApiServiceState {
124
58
context : context. clone ( ) ,
125
59
oauth_client : None ,
126
60
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,
127
67
} ;
128
68
129
69
pub fn mega_routers ( ) -> Router < MegaApiServiceState > {
0 commit comments