diff --git a/src/app_service/common/net_utils.nim b/src/app_service/common/net_utils.nim new file mode 100644 index 0000000000..ba7e65241c --- /dev/null +++ b/src/app_service/common/net_utils.nim @@ -0,0 +1,13 @@ +import net + +# Util function to test if a port is busy +proc isPortBusy*(port: Port): bool = + var sock: Socket + try: + sock = newSocket() + sock.setSockOpt(OptReuseAddr, true) + sock.bindAddr(port) + sock.close() + return false + except OSError: + return true \ No newline at end of file diff --git a/src/app_service/service/accounts/dto/api_config.nim b/src/app_service/service/accounts/dto/api_config.nim new file mode 100644 index 0000000000..80c2c37e0f --- /dev/null +++ b/src/app_service/service/accounts/dto/api_config.nim @@ -0,0 +1,43 @@ +import json + +include ../../../common/net_utils + +type APIConfig* = object + apiModules*: string + connectorEnabled*: bool + httpEnabled*: bool + httpHost*: string + httpPort*: int + wsEnabled*: bool + wsHost*: string + wsPort*: int + +proc checkAndSetPort(port: Port, isEnabled: var bool) = + if isPortBusy(port): + isEnabled = false + +proc defaultAPIConfig*(): APIConfig = + result.apiModules = "connector" + result.connectorEnabled = true + + result.httpEnabled = true + checkAndSetPort(Port(8545), result.httpEnabled) + result.httpHost = "0.0.0.0" + result.httpPort = 8545 + + result.wsEnabled = true + checkAndSetPort(Port(8586), result.wsEnabled) + result.wsHost = "0.0.0.0" + result.wsPort = 8586 + +proc toJson*(self: APIConfig): JsonNode = + return %* { + "apiModules": self.apiModules, + "connectorEnabled": self.connectorEnabled, + "httpEnabled": self.httpEnabled, + "httpHost": self.httpHost, + "httpPort": self.httpPort, + "wsEnabled": self.wsEnabled, + "wsHost": self.wsHost, + "wsPort": self.wsPort + }