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

Function run_rest_server

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

Source from the content-addressed store, hash-verified

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