Skip to content

Commit

Permalink
Stable handler dockerized integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Conor Flynn committed Feb 14, 2023
1 parent 888e515 commit 484df2d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions DeFi-Data-Engine/Api-Handler/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM maven:3.8.6-eclipse-temurin-17-alpine

ENV APP_NAME api-handler
ENV PORT 8080
ENV OUTPUT /Documents/Handler-Output

EXPOSE ${PORT}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.application.apihandler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -12,11 +19,76 @@
@ComponentScan
public class ApiHandlerApplication {

public static void main(String[] args) {
SpringApplication.run(ApiHandlerApplication.class, args);
// define default output
// System.getProperty("user.home") +
private static final String DEFAULT_OUTPUT = "/data";//String.format("/Users/%s/Documents/DeFi-Data", System.getProperty("user.name"));

private static final HashMap<String, BufferedWriter> file_lock = new HashMap<String, BufferedWriter>();

private static final void initialize() {
// retrieve home directory and path to default output
File dir = new File(DEFAULT_OUTPUT);
// validate that it is a directory and if not then create
if(!dir.exists() || !dir.isDirectory())
dir.mkdir();
}

public final static void lock(String name) {
if(!file_lock.containsKey(name)) {
try {
String file_name = DEFAULT_OUTPUT + "/" + name + ".csv";
File file = new File(file_name);
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
if(!file.exists())
file.createNewFile();
else {
file.delete();
file.createNewFile();
}
System.out.println("writing to: " + file.getAbsolutePath());
file_lock.put(name, new BufferedWriter(new FileWriter(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}

public final static void output(String data) {
System.out.println(data);
public final static void unlock(String name) {
if(file_lock.containsKey(name)) {
try {
file_lock.get(name).close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

file_lock.remove(name);
}

public final static void output(String name, String data) {
if(!file_lock.containsKey(name)) {
lock(name);
}

try {
file_lock.get(name).write(data);
file_lock.get(name).write("\n");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

public static void main(String[] args) {
// load in output directory
initialize();

SpringApplication.run(ApiHandlerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public ResponseEntity<String> handleRequest(
}

// submit request
ApiHandlerApplication.lock(name);
String response = request.request(path_map, properties_map, headers_map);
ApiHandlerApplication.unlock(name);
if(response != null)
return new ResponseEntity<String>(response, HttpStatus.SERVICE_UNAVAILABLE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ else if(obj.has(path[i])) {
// extract and print data
for(int i = 0; i < data.length(); i++) {
StringBuilder point = parse(data.getJSONObject(i));
ApiHandlerApplication.output(point.toString());
ApiHandlerApplication.output(this.getName(), point.toString());
}

// initiate recursive call
Expand Down

0 comments on commit 484df2d

Please sign in to comment.