Updated config mechanism and added config for the server
Additionally fixed a small bug in EnvoyLog and envoy.server.Startup, fixed Receiver not stopping when the server was stopped and added access token authorization for the server config
This commit is contained in:
@ -1,16 +1,13 @@
|
||||
package envoy.server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.jenkov.nioserver.Server;
|
||||
|
||||
import envoy.data.Config;
|
||||
import envoy.data.ConfigItem;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.data.ServerConfig;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectMessageProcessor;
|
||||
import envoy.server.net.ObjectMessageReader;
|
||||
@ -30,23 +27,9 @@ import envoy.util.EnvoyLog;
|
||||
public class Startup {
|
||||
|
||||
/**
|
||||
* Initializes the logger with a new config instance.
|
||||
*
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
* Stores the configuration used for the whole server
|
||||
*/
|
||||
private static void initLogging() {
|
||||
final var items = new HashMap<String, ConfigItem<?>>();
|
||||
items.put("homeDirectory",
|
||||
new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true));
|
||||
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.WARNING, true));
|
||||
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
|
||||
|
||||
final var config = new Config();
|
||||
config.load(items);
|
||||
|
||||
EnvoyLog.initialize(config);
|
||||
EnvoyLog.attach("envoy");
|
||||
}
|
||||
public static final ServerConfig config = ServerConfig.getInstance();
|
||||
|
||||
/**
|
||||
* Starts the server.
|
||||
@ -57,7 +40,13 @@ public class Startup {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public static void main(String[] args) throws IOException {
|
||||
initLogging();
|
||||
try {
|
||||
config.loadAll(Startup.class, "server.properties", args);
|
||||
EnvoyLog.initialize(config);
|
||||
} catch (final IllegalStateException e) {
|
||||
EnvoyLog.getLogger(Startup.class).log(Level.SEVERE, "Error loading configuration values: ", e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
final var server = new Server(8080, ObjectMessageReader::new,
|
||||
new ObjectMessageProcessor(Set.of(new LoginCredentialProcessor(),
|
||||
@ -84,8 +73,8 @@ public class Startup {
|
||||
server.start();
|
||||
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
|
||||
|
||||
if (args.length == 0 || !args[0].equalsIgnoreCase("no-enter-to-stop")) {
|
||||
System.out.println("Press the return key to stop the server...");
|
||||
if (config.isEnterToStop()) {
|
||||
System.out.println("Press Enter to stop the server...");
|
||||
System.in.read();
|
||||
System.out.println("Stopped");
|
||||
System.exit(0);
|
||||
|
82
server/src/main/java/envoy/server/data/ServerConfig.java
Normal file
82
server/src/main/java/envoy/server/data/ServerConfig.java
Normal file
@ -0,0 +1,82 @@
|
||||
package envoy.server.data;
|
||||
|
||||
import static java.util.function.Function.identity;
|
||||
|
||||
import envoy.data.Config;
|
||||
|
||||
/**
|
||||
* Project: <strong>server</strong><br>
|
||||
* File: <strong>ServerConfig.java</strong><br>
|
||||
* Created: <strong>21.08.2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public class ServerConfig extends Config {
|
||||
|
||||
private static ServerConfig config;
|
||||
|
||||
/**
|
||||
* @return the singleton instance of the server config
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
public static ServerConfig getInstance() { return config == null ? config = new ServerConfig() : config; }
|
||||
|
||||
private ServerConfig() {
|
||||
super(".envoy-server");
|
||||
put("enter-to-stop", "dev-stop", Boolean::parseBoolean, true);
|
||||
// parameters for issue reporting
|
||||
put("issueCreationURL", "i-url", identity(), true);
|
||||
put("issueAuthToken", "i-token", identity());
|
||||
put("userMadeLabel", "l-um", identity(), true);
|
||||
put("bugLabel", "l-b", identity(), true);
|
||||
put("featureLabel", "l-f", identity(), true);
|
||||
// enabling/ disabling several processors
|
||||
put("enableIssueReporting", "e-ir", Boolean::parseBoolean, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this server should be stoppable by pressing enter
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public Boolean isEnterToStop() { return (Boolean) items.get("enter-to-stop").get(); }
|
||||
|
||||
/**
|
||||
* @return {@code true} if issue reporting is enabled
|
||||
* @since Envoy Client v0.3-alpha
|
||||
*/
|
||||
public Boolean isIssueReportingEnabled() { return (Boolean) items.get("enableIssueReporting").get(); }
|
||||
|
||||
/**
|
||||
* @return the URL where issues should be uploaded to
|
||||
* @since Envoy Client v0.1-alpha
|
||||
*/
|
||||
public String getIssueReportingURL() { return (String) items.get("issueCreationURL").get(); }
|
||||
|
||||
/**
|
||||
* @return the String representation for the "{@code user made}" - label
|
||||
* @since Envoy Client v0.1-alpha
|
||||
*/
|
||||
public String getUserMadeLabel() { return (String) items.get("userMadeLabel").get(); }
|
||||
|
||||
/**
|
||||
* @return the String representation for the "{@code user made}" - label
|
||||
* @since Envoy Client v0.1-alpha
|
||||
*/
|
||||
public String getBugLabel() { return (String) items.get("bugLabel").get(); }
|
||||
|
||||
/**
|
||||
* @return the String representation for the "{@code user made}" - label
|
||||
* @since Envoy Client v0.1-alpha
|
||||
*/
|
||||
public String getFeatureLabel() { return (String) items.get("featureLabel").get(); }
|
||||
|
||||
/**
|
||||
* @return the authorization token used to authenticate to
|
||||
* {@link ServerConfig#getIssueReportingURL()}. If null,
|
||||
* authorization is expected to occur via a query_param or via a basic
|
||||
* user-password-combination
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public String getIssueAuthToken() { return (String) items.get("issueAuthToken").get(); }
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package envoy.server.processors;
|
||||
|
||||
import static envoy.server.Startup.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
@ -23,27 +25,32 @@ import envoy.util.EnvoyLog;
|
||||
*/
|
||||
public class IssueProposalProcessor implements ObjectProcessor<IssueProposal> {
|
||||
|
||||
private static boolean issueReportingEnabled = true;
|
||||
private static final Logger logger = EnvoyLog.getLogger(IssueProposalProcessor.class);
|
||||
private static final Logger logger = EnvoyLog.getLogger(IssueProposalProcessor.class);
|
||||
|
||||
@Override
|
||||
public void process(IssueProposal issueProposal, long socketID, ObjectWriteProxy writeProxy) throws IOException {
|
||||
// Do nothing if manually disabled
|
||||
if (!issueReportingEnabled) return;
|
||||
if (!config.isIssueReportingEnabled()) return;
|
||||
try {
|
||||
final var url = new URL(
|
||||
"https://git.kske.dev/api/v1/repos/zdm/envoy/issues?access_token=6d8ec2a72d64cbaf6319434aa2e7caf0130701b3");
|
||||
final var url = new URL(config.getIssueReportingURL());
|
||||
final var connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
// Two types of authorization are currently supported: access token as
|
||||
// query param or access token as authorization header
|
||||
final var authenticationToken = config.getIssueAuthToken();
|
||||
if (authenticationToken != null) {
|
||||
final String auth = "token " + authenticationToken;
|
||||
connection.setRequestProperty("Authorization", auth);
|
||||
}
|
||||
connection.setDoOutput(true);
|
||||
|
||||
final var json = String.format("{\"title\":\"%s\",\"body\":\"%s\",\"labels\":[240, %d]}",
|
||||
final var json = String.format("{\"title\":\"%s\",\"body\":\"%s\",\"labels\":[%s, %s]}",
|
||||
issueProposal.get(),
|
||||
issueProposal.getDescription(),
|
||||
// Label 240 should be user-made, label 117 bug and label 119 feature
|
||||
issueProposal.isBug() ? 117 : 119);
|
||||
config.getUserMadeLabel(),
|
||||
issueProposal.isBug() ? config.getBugLabel() : config.getFeatureLabel());
|
||||
try (final var os = connection.getOutputStream()) {
|
||||
final byte[] input = json.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
@ -51,29 +58,9 @@ public class IssueProposalProcessor implements ObjectProcessor<IssueProposal> {
|
||||
final var status = connection.getResponseCode();
|
||||
if (status == 201) logger.log(Level.INFO, "Successfully created an issue");
|
||||
else logger.log(Level.WARNING,
|
||||
String.format("Tried creating an issue for %s but received status code %d - Request params:title=%s,description=%s,json=%s",
|
||||
url,
|
||||
status,
|
||||
issueProposal.get(),
|
||||
issueProposal.getDescription(),
|
||||
json));
|
||||
String.format("Tried creating an issue for %s but received status code %d - Request params:%s", url, status, json));
|
||||
} catch (final IOException e) {
|
||||
logger.log(Level.WARNING, "An error occurred while creating an issue: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether issue reporting is enabled
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public static boolean isIssueReportingEnabled() { return issueReportingEnabled; }
|
||||
|
||||
/**
|
||||
* @param issueReportingEnabled whether issue reporting should be enabled - true
|
||||
* by default
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public static void setIssueReportingEnabled(boolean issueReportingEnabled) {
|
||||
IssueProposalProcessor.issueReportingEnabled = issueReportingEnabled;
|
||||
}
|
||||
}
|
||||
|
14
server/src/main/resources/server.properties
Normal file
14
server/src/main/resources/server.properties
Normal file
@ -0,0 +1,14 @@
|
||||
enter-to-stop=true
|
||||
enableIssueReporting=true
|
||||
# git.kske.dev config
|
||||
issueCreationURL=https://git.kske.dev/api/v1/repos/zdm/envoy/issues?access_token=6d8ec2a72d64cbaf6319434aa2e7caf0130701b3
|
||||
userMadeLabel=240
|
||||
bugLabel=117
|
||||
featureLabel=119
|
||||
# api.github.com config - supply an additional auth token
|
||||
#issueCreationURL=https://api.github.com/repos/informatik-ag-ngl/envoy/issues
|
||||
#userMadeLabel="user made"
|
||||
#bugLabel="bug"
|
||||
#featureLabel="feature"
|
||||
consoleLevelBarrier=FINEST
|
||||
fileLevelBarrier=WARNING
|
Reference in New Issue
Block a user