diff --git a/.gitignore b/.gitignore index a7229e9..526aabb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ app/bin # Ignore Eclipse IDE files and directories .project .classpath -.settings \ No newline at end of file +.settings + +# Ignore project outputs +app/skinner.properties \ No newline at end of file diff --git a/app/src/main/java/org/skinner/WebApp.java b/app/src/main/java/org/skinner/WebApp.java index e71368f..bfe051e 100644 --- a/app/src/main/java/org/skinner/WebApp.java +++ b/app/src/main/java/org/skinner/WebApp.java @@ -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); } } diff --git a/app/src/main/resources/skinner.properties b/app/src/main/resources/skinner.properties new file mode 100644 index 0000000..fa69ad0 --- /dev/null +++ b/app/src/main/resources/skinner.properties @@ -0,0 +1,5 @@ +database.address = jdbc:mysql://localhost:3306/skinner +database.username = skinner +database.password = skinner + +server.port = 8000 \ No newline at end of file