Skip to content

Commit

Permalink
Add GitHub workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Dec 2, 2024
1 parent 69eea2e commit a1c6a29
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 2 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<version>3.7.0</version>
<configuration>
<archive>
<manifest>
Expand All @@ -39,11 +39,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
<version>3.13.0</version>
</plugin>
</plugins>
</build>
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/lbry/globe/handler/HTTPHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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;
Expand All @@ -23,17 +25,16 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{

public static final AttributeKey<HttpRequest> ATTR_REQUEST = AttributeKey.newInstance("request");
public static final AttributeKey<List<HttpContent>> 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);
Expand All @@ -47,6 +48,8 @@ private void handleResponse(ChannelHandlerContext ctx){
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());

Expand Down Expand Up @@ -117,8 +120,8 @@ public void channelReadComplete(ChannelHandlerContext ctx){
}

@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();
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/lbry/globe/object/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Node{
private final InetAddress address;
private Double latitude;
private Double longitude;
private List<Service> services = new ArrayList<>();
private final List<Service> services = new ArrayList<>();

public Node(InetAddress address,Double latitude,Double longitude){
this.address = address;
Expand All @@ -25,10 +25,18 @@ public Double getLatitude() {
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<Service> getServices(){
return this.services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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;
Expand All @@ -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();
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/lbry/globe/util/GeoIP.java
Original file line number Diff line number Diff line change
@@ -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<InetAddress,JSONObject> 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){
Expand All @@ -23,14 +24,14 @@ public static JSONObject getCachedGeoIPInformation(InetAddress ip){
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){
Expand Down Expand Up @@ -68,7 +69,7 @@ public static void loadCache(){
}
br.close();
}catch(Exception e){
e.printStackTrace();
GeoIP.LOGGER.log(Level.WARNING,"Failed loading GeoIP cache.",e);
}
}

Expand All @@ -82,7 +83,7 @@ public static void saveCache(){
fos.write(obj.toString().getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
GeoIP.LOGGER.log(Level.WARNING,"Failed saving GeoIP cache.",e);
}
}

Expand Down

0 comments on commit a1c6a29

Please sign in to comment.