Skip to content

Commit

Permalink
Added base controller. (#87)
Browse files Browse the repository at this point in the history
Main author: I just stole it :(
Co-authored-by: Ziad Othman <ziadsadek999@gmail.com>
  • Loading branch information
ziadsadek999 authored May 12, 2024
1 parent b1ab659 commit 6a7370b
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 120 deletions.
2 changes: 2 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Binary file added controller/.mvn/wrapper/maven-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions controller/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
75 changes: 75 additions & 0 deletions controller/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.workup</groupId>
<artifactId>controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>controller</name>
<description>Controller module</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.workup</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.clichemaven</groupId>
<artifactId>cliche</artifactId>
<version>110413</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin</artifactId>
<version>5.3</version>
<executions>
<execution>
<id>validate-code-format</id>
<goals>
<goal>validate-code-format</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.cosium.code</groupId>
<artifactId>google-java-format</artifactId>
<version>5.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
83 changes: 83 additions & 0 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.workup.controller;

import asg.cliche.CLIException;
import asg.cliche.Command;
import com.workup.shared.commands.controller.SetMaxThreadsRequest;
import com.workup.shared.enums.ControllerQueueNames;
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

public class CLIHandler {
@Autowired AmqpTemplate rabbitTemplate;
private static final Map<String, String> appQueueMap = new HashMap<>();

static {
appQueueMap.put("jobs", ControllerQueueNames.JOBS);
appQueueMap.put("users", ControllerQueueNames.USERS);
appQueueMap.put("payments", ControllerQueueNames.PAYMENTS);
appQueueMap.put("contracts", ControllerQueueNames.CONTRACTS);
}

@Command(description = "Set the maximum number of threads for a specific app")
public String maxthreads(String app, int maxThreads) throws CLIException {
app = app.toLowerCase();
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
if (maxThreads > 100 || maxThreads < 1) {
return "Error: Max threads must have a value between 1 and 50";
}
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
"",
SetMaxThreadsRequest.builder().withMaxThreads(maxThreads).build());
return "MaxThreads";
}

@Command(description = "Set the maximum number of DB connections for a specific app")
public String maxdb(String app, int appNum, String maxDBConn) {
return "maxdb";
}

@Command(description = "starts a specific app")
public String start(String app, int appNum) {
return "start";
}

@Command(description = "stops a specific app")
public String freeze(String app, int appNum) {
return "freeze";
}

@Command(description = "stops a specific app")
public String setmq(String app, int appNum) {
return "setmq";
}

@Command(description = "stops a specific app")
public String setErrorReportingLevel(String app, int appNum) {
return "error level";
}

@Command(description = "Creates a new command")
public String addcommand(String app, String commandName, String className) {
return "Add command";
}

@Command(description = "Updates an existing command")
public String updatecommand(String app, String commandName, String className) {
return "Update Command";
}

@Command(description = "Deletes an existing command")
public String deletecommand(String app, String commandName, String className) {
return "Delete command";
}

@Command(description = "stops a specific app")
public String updateClass(String app, int appNum) {
return "update class";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.workup.controller;

import asg.cliche.Shell;
import asg.cliche.ShellFactory;
import java.io.IOException;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ControllerApplication {

public static void main(String[] args) throws IOException {
SpringApplication.run(ControllerApplication.class, args);
}

@Bean
public ApplicationRunner runner(CLIHandler cliHandler) {
return args -> {
Shell shell = ShellFactory.createConsoleShell("", "Controller", cliHandler);
shell.commandLoop();
};
}

@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
public CLIHandler cliHandler() {
return new CLIHandler();
}
}
5 changes: 5 additions & 0 deletions controller/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.application.name=controller
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.workup</groupId>
Expand All @@ -16,6 +16,7 @@

<modules>
<module>shared</module>
<module>controller</module>
<module>services/jobs</module>
<module>services/payments</module>
<module>services/users</module>
Expand Down
Loading

0 comments on commit 6a7370b

Please sign in to comment.