return channels for peer

This commit is contained in:
Carsten Otto
2021-11-21 21:59:55 +01:00
parent 6e242c599c
commit e4dfd5911d
10 changed files with 172 additions and 24 deletions

View File

@@ -1,12 +1,15 @@
package de.cotto.lndmanagej.controller;
import com.codahale.metrics.MetricRegistry;
import de.cotto.lndmanagej.controller.dto.ChannelDetailsDto;
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
import de.cotto.lndmanagej.metrics.Metrics;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.NodeService;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/channel/{channelId}")
@Import(ObjectMapperConfiguration.class)
public class ChannelDetailsController {
private final ChannelService channelService;
private final NodeService nodeService;

View File

@@ -1,15 +0,0 @@
package de.cotto.lndmanagej.controller;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
public record ChannelDetailsDto(
@JsonSerialize(using = ToStringSerializer.class) ChannelId channelId,
@JsonSerialize(using = ToStringSerializer.class) Pubkey remotePubkey,
String remoteAlias,
@JsonProperty("private") boolean privateChannel
) {
}

View File

@@ -1,36 +1,68 @@
package de.cotto.lndmanagej.controller;
import com.codahale.metrics.MetricRegistry;
import de.cotto.lndmanagej.controller.dto.ChannelsForNodeDto;
import de.cotto.lndmanagej.controller.dto.NodeDetailsDto;
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
import de.cotto.lndmanagej.metrics.Metrics;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.NodeService;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/node/{pubkey}")
@Import(ObjectMapperConfiguration.class)
public class NodeController {
private final NodeService nodeService;
private final Metrics metrics;
private final ChannelService channelService;
public NodeController(NodeService nodeService, Metrics metrics) {
public NodeController(NodeService nodeService, ChannelService channelService, Metrics metrics) {
this.nodeService = nodeService;
this.metrics = metrics;
this.channelService = channelService;
}
@GetMapping("/alias")
public String getAlias(Pubkey pubkey) {
metrics.mark(MetricRegistry.name(getClass(), "getAlias"));
mark("getAlias");
return nodeService.getAlias(pubkey);
}
@GetMapping("/details")
public NodeDetailsDto getDetails(@PathVariable Pubkey pubkey) {
metrics.mark(MetricRegistry.name(getClass(), "getDetails"));
mark("getDetails");
Node node = nodeService.getNode(pubkey);
return new NodeDetailsDto(pubkey, node.alias(), node.online());
return new NodeDetailsDto(pubkey, node.alias(), getChannelIdsForPubkey(pubkey), node.online());
}
@GetMapping("/open-channels")
public ChannelsForNodeDto getOpenChannelIdsForPubkey(@PathVariable Pubkey pubkey) {
mark("getOpenChannelIdsForPubkey");
List<ChannelId> channels = getChannelIdsForPubkey(pubkey);
return new ChannelsForNodeDto(pubkey, channels);
}
private List<ChannelId> getChannelIdsForPubkey(Pubkey pubkey) {
return channelService.getOpenChannelsWith(pubkey).stream()
.map(Channel::getId)
.sorted()
.collect(Collectors.toList());
}
private void mark(String getDetails) {
metrics.mark(MetricRegistry.name(getClass(), getDetails));
}
}

View File

@@ -0,0 +1,13 @@
package de.cotto.lndmanagej.controller.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
public record ChannelDetailsDto(
ChannelId channelId,
Pubkey remotePubkey,
String remoteAlias,
@JsonProperty("private") boolean privateChannel
) {
}

View File

@@ -0,0 +1,12 @@
package de.cotto.lndmanagej.controller.dto;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import java.util.List;
public record ChannelsForNodeDto(
Pubkey node,
List<ChannelId> channels
) {
}

View File

@@ -1,12 +1,16 @@
package de.cotto.lndmanagej.controller;
package de.cotto.lndmanagej.controller.dto;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import java.util.List;
public record NodeDetailsDto(
@JsonSerialize(using = ToStringSerializer.class) Pubkey pubkey,
@JsonSerialize(using = ToStringSerializer.class) Pubkey node,
String alias,
List<ChannelId> channels,
boolean online
) {
}

View File

@@ -0,0 +1,26 @@
package de.cotto.lndmanagej.controller.dto;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class ObjectMapperConfiguration {
public ObjectMapperConfiguration() {
// default constructor
}
@Bean
@Primary
public ObjectMapper objectMapper() {
SimpleModule module = new SimpleModule("SimpleModule");
module.addSerializer(Pubkey.class, new ToStringSerializer());
module.addSerializer(ChannelId.class, new ToStringSerializer());
return new ObjectMapper().registerModule(module);
}
}