MCPcopy Create free account
hub / github.com/ElementsProject/lightning / run_rest_server

Function run_rest_server

plugins/rest-plugin/src/main.rs:122–209  ·  view source on GitHub ↗
(
    plugin: Plugin<PluginState>,
    clnrest_options: ClnrestOptions,
    notify_rx: Receiver<serde_json::Value>,
)

Source from the content-addressed store, hash-verified

120}
121
122async fn run_rest_server(
123 plugin: Plugin<PluginState>,
124 clnrest_options: ClnrestOptions,
125 notify_rx: Receiver<serde_json::Value>,
126) -> Result<(), anyhow::Error> {
127 let (socket_layer, socket_io) = SocketIoBuilder::new()
128 .with_state(plugin.clone())
129 .build_layer();
130
131 socket_io.ns("/", socketio_on_connect.with(auth_socket_io_middleware));
132
133 tokio::spawn(notification_background_task(socket_io.clone(), notify_rx));
134
135 let swagger_path = if clnrest_options.swagger.eq("/") {
136 SWAGGER_FALLBACK.to_string()
137 } else {
138 clnrest_options.swagger.clone()
139 };
140
141 let swagger_router =
142 Router::new().merge(SwaggerUi::new(swagger_path).url("/swagger.json", ApiDoc::openapi()));
143
144 let root_router = Router::new()
145 .route("/", get(|| async { "Hello, World!" }))
146 .layer(ServiceBuilder::new().layer(middleware::from_fn(swagger_redirect_middleware)))
147 .layer(Extension(clnrest_options.swagger));
148
149 let rpc_router = Router::new()
150 .route("/v1/list-methods", get(list_methods))
151 .route("/{*path}", any(call_rpc_method))
152 .layer(clnrest_options.cors)
153 .layer(Extension(plugin.clone()))
154 .layer(
155 ServiceBuilder::new().layer(SetResponseHeaderLayer::if_not_present(
156 HeaderName::from_str("Content-Security-Policy")?,
157 HeaderValue::from_str(&clnrest_options.csp)?,
158 )),
159 );
160
161 let app = swagger_router
162 .merge(root_router)
163 .merge(rpc_router)
164 .layer(socket_layer);
165
166 match clnrest_options.protocol {
167 ClnrestProtocol::Https => {
168 let max_retries = 10;
169 let mut retries = 0;
170 while retries < max_retries && !do_certificates_exist(&clnrest_options.certs) {
171 log::debug!("Certificates incomplete. Retrying...");
172 time::sleep(Duration::from_millis(500)).await;
173 retries += 1;
174 }
175
176 if !do_certificates_exist(&clnrest_options.certs) {
177 log::debug!("Certificates still not existing after retries. Generating...");
178 generate_certificates(&clnrest_options.certs, &plugin.option(&OPT_CLNREST_HOST)?)?;
179 log::debug!("Certificates generated.");

Callers

nothing calls this directly

Calls 9

eqMethod · 0.80
urlMethod · 0.80
optionMethod · 0.80
joinMethod · 0.80
do_certificates_existFunction · 0.70
generate_certificatesFunction · 0.70
anyClass · 0.50
to_stringMethod · 0.45

Tested by

no test coverage detected