Skip to content

Commit

Permalink
Merge pull request #31 from BorZzzenko/nagel_branch
Browse files Browse the repository at this point in the history
Nagel branch
  • Loading branch information
brzzznko authored Aug 9, 2020
2 parents b67b5fa + 12ddbf5 commit 8b5d7ef
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public ArrayList<Document> getNameApiFunctions() throws ClassNotFoundException {
if(last == -1)
continue;
str = str.substring(0,last);
if(str.charAt(0) != '/'){
str = "/" + str;
}
arrayList.add(new Document("path", rm+str).append("api_version", "v1"));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Collections/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mongodb.collectionName=collections

server.port=8081
server.host=localhost
service.name=CONTENT MANAGEMENT
service.version=0.1.0beta
service.name=Collections
service.version=1.0.0

gateway.host=http://localhost
gateway.port=8085
7 changes: 6 additions & 1 deletion Rating/src/main/java/com/team3/rating/Application.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.team3.rating;

import com.team3.rating.Model.GatewayWork;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
ApplicationContext ctx = SpringApplication.run(Application.class, args);

GatewayWork printThread1 = (GatewayWork) ctx.getBean("gatewayWork");
printThread1.start();
}

}
181 changes: 181 additions & 0 deletions Rating/src/main/java/com/team3/rating/Model/GatewayWork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.team3.rating.Model;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@Component
@Scope("prototype")
public class GatewayWork extends Thread {
@Value("${gateway.host}")
private String gatewayHost; //Host service Gateway

@Value("${gateway.port}")
private String gatewayPort; //Port service Gateway

@Value("${server.host}")
private String host;

@Value("${server.port}")
private String port;

@Value("${service.name}")
private String serviceName;

@Value("${service.version}")
private String serviceVersion;


private String instanceId;
private int pingInterval;

@Autowired
private RestTemplate restTemplate;

@Override
public void run() {

while (true) {
try {
registration(); //To register for the service
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

try {
ready();
} catch (HttpServerErrorException e) {
e.printStackTrace();
}

try {
ping(); //Ping
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void ping() throws InterruptedException {
String url = gatewayHost + ":" + gatewayPort + "/gateway/ping/" + instanceId;

while (true) {
ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);

//If an instance with this ID has not published the API
if (!(result.getStatusCode().equals(HttpStatus.OK)))
return; //Then go to publishing

Thread.sleep(pingInterval);
}
}

public void registration() throws UnsupportedEncodingException, ClassNotFoundException {
//Creating a request address
String url = gatewayHost + ":" + gatewayPort + "/gateway/publish";
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}

//Add address, port, name_service, version_service
Document requestBody = readConfigService();

//Add api
requestBody.append("api", getNameApiFunctions());

//Registering the service
ResponseEntity<Document> result = restTemplate.postForEntity(uri, requestBody, Document.class);
instanceId = result.getBody().getString("instance_id");
pingInterval = (int) result.getBody().getInteger("ping_interval");

}

public void ready() {
String url = gatewayHost + ":" + gatewayPort + "/gateway/ready/" + instanceId;

ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
}

public ArrayList<Document> getNameApiFunctions() throws ClassNotFoundException {
ArrayList<Document> arrayList = new ArrayList<>();

Class clazz = Class.forName("com.team3.rating.Controller.Controller");

String rm = "";
for(Annotation an : clazz.getAnnotations()){
rm = an.toString();
int ix = rm.indexOf("RequestMapping");
if(ix != -1){
rm = rm.substring(ix+"RequestMapping".length());
int first = rm.indexOf("value={") + "value={".length() + 1;
rm = rm.substring(first);
int last = rm.indexOf("\"},");
rm = rm.substring(0,last);
break;
}
}
rm = "/" + rm;

for(Method method :clazz.getMethods()){
for(Annotation annotation :method.getDeclaredAnnotations()){

String str = annotation.toString();
int first = annotation.toString().indexOf("value={") + "value={".length() + 1;
if(first != -1) {
str = str.substring(first);
int last = str.indexOf("\"},");
if(last == -1)
continue;
str = str.substring(0,last);
if(str.charAt(0) != '/'){
str = "/" + str;
}
arrayList.add(new Document("path", rm+str).append("api_version", "v1"));
}
}
}

Set<Document> foo = new HashSet<Document>(arrayList);
ArrayList<Document> mainList = new ArrayList<Document>();
mainList.addAll(foo);

return mainList;
}

public Document readConfigService() {
Document requestBody = new Document();
requestBody.append("address", gatewayHost);
requestBody.append("port", port);
requestBody.append("name_service", serviceName);
requestBody.append("version_service", serviceVersion);
return requestBody;
}

public void setGatewayHost(String gatewayHost) {
this.gatewayHost = gatewayHost;
}

public void setGatewayPort(String gatewayPort) {
this.gatewayPort = gatewayPort;
}
}
8 changes: 8 additions & 0 deletions Rating/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ mongodb.port=27017
mongodb.databaseName=mydb
mongodb.ratingsCollectionName=ratings
mongodb.averageRatingsCollectionName=averageRatings

server.port=8082
server.host=localhost
service.name=Rating
service.version=1.0.0

gateway.host=http://localhost
gateway.port=8085

0 comments on commit 8b5d7ef

Please sign in to comment.