Added configuration file support

This commit is contained in:
2025-06-12 18:09:49 +02:00
parent ed46f93ba3
commit ce53580040
3 changed files with 33 additions and 4 deletions

5
.gitignore vendored
View File

@ -8,4 +8,7 @@ app/bin
# Ignore Eclipse IDE files and directories
.project
.classpath
.settings
.settings
# Ignore project outputs
app/skinner.properties

View File

@ -2,17 +2,38 @@ package org.skinner;
import java.io.*;
import java.net.*;
import java.util.Properties;
import com.sun.net.httpserver.*;
public class WebApp {
public static void main(String[] args) throws IOException {
// Load properties
File propertiesFile = new File("skinner.properties");
if (!propertiesFile.exists()) {
OutputStream os = new FileOutputStream(propertiesFile);
InputStream is = WebApp.class.getResourceAsStream("/skinner.properties");
os.write(is.readAllBytes());
os.close();
is.close();
propertiesFile = new File("skinner.properties");
}
InputStream propertiesIs = new FileInputStream(propertiesFile);
Properties properties = new Properties();
properties.load(propertiesIs);
propertiesIs.close();
// Initialise database
Database.init("jdbc:mysql://localhost:3306/skinner", "skinner", "skinner");
Database.init(
properties.getProperty("database.address"),
properties.getProperty("database.username"),
properties.getProperty("database.password")
);
// Create an HttpServer instance
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
int serverPort = Integer.parseInt(properties.getProperty("server.port"));
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
// Create a context for a specific path and set the handler
server.createContext("/api/", new RestAPI("/api/"));
@ -24,7 +45,7 @@ public class WebApp {
server.setExecutor(null); // Use the default executor
server.start();
System.out.println("Server is running on port 8000");
System.out.printf("Server is running on :%s\n", serverPort);
}
}

View File

@ -0,0 +1,5 @@
database.address = jdbc:mysql://localhost:3306/skinner
database.username = skinner
database.password = skinner
server.port = 8000