From a1c6a29eea318198bea6888f2ff0cee779bb43eb Mon Sep 17 00:00:00 2001 From: Ben van Hartingsveldt Date: Mon, 2 Dec 2024 11:20:42 +0100 Subject: [PATCH] Add GitHub workflow --- .github/workflows/docker-image.yml | 16 ++++++++++++++++ pom.xml | 8 ++------ .../com/lbry/globe/handler/HTTPHandler.java | 15 +++++++++------ src/main/java/com/lbry/globe/object/Node.java | 10 +++++++++- .../thread/BlockchainNodeFinderThread.java | 4 ++-- src/main/java/com/lbry/globe/util/GeoIP.java | 17 +++++++++-------- 6 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..ab3b50a --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,16 @@ +name: Docker Image CI + +on: + push: + branches: ["**"] + pull_request: + branches: ["**"] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3ee4f8b..0ec6fa1 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ org.apache.maven.plugins maven-assembly-plugin - 3.3.0 + 3.7.0 @@ -39,11 +39,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 - - 8 - 8 - + 3.13.0 diff --git a/src/main/java/com/lbry/globe/handler/HTTPHandler.java b/src/main/java/com/lbry/globe/handler/HTTPHandler.java index 063c497..9e719db 100644 --- a/src/main/java/com/lbry/globe/handler/HTTPHandler.java +++ b/src/main/java/com/lbry/globe/handler/HTTPHandler.java @@ -14,6 +14,8 @@ import java.io.InputStream; import java.io.IOException; import java.net.URI; import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; import org.json.JSONArray; @@ -23,17 +25,16 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{ public static final AttributeKey ATTR_REQUEST = AttributeKey.newInstance("request"); public static final AttributeKey> ATTR_CONTENT = AttributeKey.newInstance("content"); + private static final Logger LOGGER = Logger.getLogger("Handler"); @Override public void channelRead(ChannelHandlerContext ctx,Object msg){ if(msg instanceof HttpRequest){ - HttpRequest request = (HttpRequest) msg; - ctx.channel().attr(HTTPHandler.ATTR_REQUEST).set(request); + ctx.channel().attr(HTTPHandler.ATTR_REQUEST).set((HttpRequest) msg); } if(msg instanceof HttpContent){ - HttpContent content = (HttpContent) msg; ctx.channel().attr(HTTPHandler.ATTR_CONTENT).setIfAbsent(new ArrayList<>()); - ctx.channel().attr(HTTPHandler.ATTR_CONTENT).get().add(content); + ctx.channel().attr(HTTPHandler.ATTR_CONTENT).get().add((HttpContent) msg); if(msg instanceof LastHttpContent){ this.handleResponse(ctx); @@ -47,6 +48,8 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{ ctx.channel().attr(HTTPHandler.ATTR_REQUEST).set(null); ctx.channel().attr(HTTPHandler.ATTR_CONTENT).set(null); + assert content!=null; + if(request.method().equals(HttpMethod.GET)){ URI uri = URI.create(request.uri()); @@ -117,8 +120,8 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{ } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - cause.printStackTrace(); //TODO + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){ + HTTPHandler.LOGGER.log(Level.WARNING,"Exception during HTTP handling",cause); ctx.close(); } diff --git a/src/main/java/com/lbry/globe/object/Node.java b/src/main/java/com/lbry/globe/object/Node.java index ceff014..fd6967c 100644 --- a/src/main/java/com/lbry/globe/object/Node.java +++ b/src/main/java/com/lbry/globe/object/Node.java @@ -9,7 +9,7 @@ public class Node{ private final InetAddress address; private Double latitude; private Double longitude; - private List services = new ArrayList<>(); + private final List services = new ArrayList<>(); public Node(InetAddress address,Double latitude,Double longitude){ this.address = address; @@ -25,10 +25,18 @@ public class Node{ return this.latitude; } + public void setLatitude(Double latitude) { + this.latitude = latitude; + } + public Double getLongitude() { return this.longitude; } + public void setLongitude(Double longitude) { + this.longitude = longitude; + } + public List getServices(){ return this.services; } diff --git a/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java b/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java index 1cb0a08..4716416 100644 --- a/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java +++ b/src/main/java/com/lbry/globe/thread/BlockchainNodeFinderThread.java @@ -10,7 +10,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; -import java.net.URL; +import java.net.URI; import java.util.*; import org.json.JSONArray; @@ -22,7 +22,7 @@ public class BlockchainNodeFinderThread implements Runnable{ public void run() { while(true){ try{ - HttpURLConnection conn = (HttpURLConnection) new URL(System.getenv("BLOCKCHAIN_RPC_URL")).openConnection(); + HttpURLConnection conn = (HttpURLConnection) new URI(System.getenv("BLOCKCHAIN_RPC_URL")).toURL().openConnection(); conn.setDoOutput(true); conn.addRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((System.getenv("BLOCKCHAIN_USERNAME")+":"+System.getenv("BLOCKCHAIN_PASSWORD")).getBytes())); conn.connect(); diff --git a/src/main/java/com/lbry/globe/util/GeoIP.java b/src/main/java/com/lbry/globe/util/GeoIP.java index 9e29a3b..c512f19 100644 --- a/src/main/java/com/lbry/globe/util/GeoIP.java +++ b/src/main/java/com/lbry/globe/util/GeoIP.java @@ -1,18 +1,19 @@ package com.lbry.globe.util; import java.io.*; -import java.net.HttpURLConnection; -import java.net.InetAddress; -import java.net.URL; +import java.net.*; import java.util.Comparator; import java.util.Map; import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; import org.json.JSONObject; public class GeoIP{ private static final Map CACHE = new TreeMap<>(Comparator.comparing(InetAddress::getHostAddress)); + private static final Logger LOGGER = Logger.getLogger("GeoIP"); private static final String TOKEN = System.getenv("IPINFO_TOKEN"); public static JSONObject getCachedGeoIPInformation(InetAddress ip){ @@ -23,14 +24,14 @@ public class GeoIP{ GeoIP.CACHE.put(ip,result); GeoIP.saveCache(); }catch(Exception e){ - e.printStackTrace(); + GeoIP.LOGGER.log(Level.WARNING,"Failed getting cached GeoIP information.",e); } } return result; } - public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException{ - HttpURLConnection conn = (HttpURLConnection) new URL("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).openConnection(); + public static JSONObject getGeoIPInformation(InetAddress ip) throws IOException,URISyntaxException{ + HttpURLConnection conn = (HttpURLConnection) new URI("https://ipinfo.io/"+ip.getHostAddress()+"?token="+GeoIP.TOKEN).toURL().openConnection(); conn.connect(); InputStream in = conn.getInputStream(); if(in==null){ @@ -68,7 +69,7 @@ public class GeoIP{ } br.close(); }catch(Exception e){ - e.printStackTrace(); + GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e); } } @@ -82,7 +83,7 @@ public class GeoIP{ fos.write(obj.toString().getBytes()); fos.close(); }catch(Exception e){ - e.printStackTrace(); + GeoIP.LOGGER.log(Level.WARNING,"Failed saving GeoIP cache.",e); } }