Skip to content

Commit

Permalink
refactor: extract program-start logic from Server into a Main class
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanGerlek committed Nov 14, 2024
1 parent 476dd40 commit b8897f2
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 96 deletions.
103 changes: 103 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.server.endpointprovider.EndpointProvider;
import edu.byu.cs.server.endpointprovider.EndpointProviderImpl;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.properties.ApplicationProperties;
import edu.byu.cs.server.Server;
import edu.byu.cs.service.SubmissionService;
import edu.byu.cs.util.ResourceUtils;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class Main {
private static Logger LOGGER = LoggerFactory.getLogger(Main.class);

private static EndpointProvider endpointProvider = new EndpointProviderImpl();

public static void main(String[] args) {
ResourceUtils.copyResourceFiles("phases", new File(""));
setupProperties(args);

try {
DaoService.initializeSqlDAOs();
} catch (DataAccessException e) {
LOGGER.error("Error setting up database", e);
throw new RuntimeException(e);
}

new Server(endpointProvider).start(8080);

try {
SubmissionService.reRunSubmissionsInQueue();
} catch (IOException | DataAccessException | GradingException e) {
LOGGER.error("Error rerunning submissions already in queue", e);
}
}

private static void setupProperties(String[] args) {
Options options = getOptions();

Properties properties = new Properties();

CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("db-host")) {
properties.setProperty("db-host", cmd.getOptionValue("db-host"));
}
if (cmd.hasOption("db-port")) {
properties.setProperty("db-port", cmd.getOptionValue("db-port"));
}
if (cmd.hasOption("db-name")) {
properties.setProperty("db-name", cmd.getOptionValue("db-name"));
}
if (cmd.hasOption("db-user")) {
properties.setProperty("db-user", cmd.getOptionValue("db-user"));
}
if (cmd.hasOption("db-pass")) {
properties.setProperty("db-pass", cmd.getOptionValue("db-pass"));
}
if (cmd.hasOption("frontend-url")) {
properties.setProperty("frontend-url", cmd.getOptionValue("frontend-url"));
}
if (cmd.hasOption("cas-callback-url")) {
properties.setProperty("cas-callback-url", cmd.getOptionValue("cas-callback-url"));
}
if (cmd.hasOption("canvas-token")) {
properties.setProperty("canvas-token", cmd.getOptionValue("canvas-token"));
}
if (cmd.hasOption("use-canvas")) {
properties.setProperty("use-canvas", cmd.getOptionValue("use-canvas"));
}
if (cmd.hasOption("disable-compilation")) {
properties.setProperty("run-compilation", "false");
}
} catch (ParseException e) {
throw new RuntimeException("Error parsing command line arguments", e);
}

ApplicationProperties.loadProperties(properties);
}

private static Options getOptions() {
Options options = new Options();
options.addOption(null, "db-host", true, "Database Host");
options.addOption(null, "db-port", true, "Database Port");
options.addOption(null, "db-name", true, "Database Name");
options.addOption(null, "db-user", true, "Database User");
options.addOption(null, "db-pass", true, "Database Password");
options.addOption(null, "frontend-url", true, "Frontend URL");
options.addOption(null, "cas-callback-url", true, "CAS Callback URL");
options.addOption(null, "canvas-token", true, "Canvas Token");
options.addOption(null, "use-canvas", true, "Using Canvas");
options.addOption(null, "disable-compilation", false, "Turn off student code compilation");
return options;
}

}
102 changes: 6 additions & 96 deletions src/main/java/edu/byu/cs/server/Server.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
package edu.byu.cs.server;

import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.controller.WebSocketController;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.properties.ApplicationProperties;
import edu.byu.cs.server.endpointprovider.EndpointProvider;
import edu.byu.cs.server.endpointprovider.EndpointProviderImpl;
import edu.byu.cs.service.SubmissionService;
import edu.byu.cs.util.ResourceUtils;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import static spark.Spark.*;

public class Server {
Expand All @@ -28,6 +17,12 @@ public Server(EndpointProvider endpointProvider) {
this.provider = endpointProvider;
}

public int start(int desiredPort) {
int chosenPort = setupEndpoints(desiredPort);
LOGGER.info("Server started on port {}", chosenPort);
return chosenPort;
}

public int setupEndpoints(int port) {
port(port);

Expand Down Expand Up @@ -133,89 +128,4 @@ public int setupEndpoints(int port) {

return port();
}

private static void setupProperties(String[] args) {
Options options = getOptions();

Properties properties = new Properties();

CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("db-host")) {
properties.setProperty("db-host", cmd.getOptionValue("db-host"));
}
if (cmd.hasOption("db-port")) {
properties.setProperty("db-port", cmd.getOptionValue("db-port"));
}
if (cmd.hasOption("db-name")) {
properties.setProperty("db-name", cmd.getOptionValue("db-name"));
}
if (cmd.hasOption("db-user")) {
properties.setProperty("db-user", cmd.getOptionValue("db-user"));
}
if (cmd.hasOption("db-pass")) {
properties.setProperty("db-pass", cmd.getOptionValue("db-pass"));
}
if (cmd.hasOption("frontend-url")) {
properties.setProperty("frontend-url", cmd.getOptionValue("frontend-url"));
}
if (cmd.hasOption("cas-callback-url")) {
properties.setProperty("cas-callback-url", cmd.getOptionValue("cas-callback-url"));
}
if (cmd.hasOption("canvas-token")) {
properties.setProperty("canvas-token", cmd.getOptionValue("canvas-token"));
}
if (cmd.hasOption("use-canvas")) {
properties.setProperty("use-canvas", cmd.getOptionValue("use-canvas"));
}
if (cmd.hasOption("disable-compilation")) {
properties.setProperty("run-compilation", "false");
}
} catch (ParseException e) {
throw new RuntimeException("Error parsing command line arguments", e);
}

ApplicationProperties.loadProperties(properties);
}

private static Options getOptions() {
Options options = new Options();
options.addOption(null, "db-host", true, "Database Host");
options.addOption(null, "db-port", true, "Database Port");
options.addOption(null, "db-name", true, "Database Name");
options.addOption(null, "db-user", true, "Database User");
options.addOption(null, "db-pass", true, "Database Password");
options.addOption(null, "frontend-url", true, "Frontend URL");
options.addOption(null, "cas-callback-url", true, "CAS Callback URL");
options.addOption(null, "canvas-token", true, "Canvas Token");
options.addOption(null, "use-canvas", true, "Using Canvas");
options.addOption(null, "disable-compilation", false, "Turn off student code compilation");
return options;
}


public static void main(String[] args) {
ResourceUtils.copyResourceFiles("phases", new File(""));
setupProperties(args);

try {
DaoService.initializeSqlDAOs();
} catch (DataAccessException e) {
LOGGER.error("Error setting up database", e);
throw new RuntimeException(e);
}

EndpointProvider endpointProvider = new EndpointProviderImpl();
int port = new Server(endpointProvider).setupEndpoints(8080);

LOGGER.info("Server started on port {}", port);

try {
SubmissionService.reRunSubmissionsInQueue();
} catch (IOException | DataAccessException | GradingException e) {
LOGGER.error("Error rerunning submissions already in queue", e);
}
}

}

0 comments on commit b8897f2

Please sign in to comment.