Added configuration file support
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@ -8,4 +8,7 @@ app/bin
|
||||
# Ignore Eclipse IDE files and directories
|
||||
.project
|
||||
.classpath
|
||||
.settings
|
||||
.settings
|
||||
|
||||
# Ignore project outputs
|
||||
app/skinner.properties
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
5
app/src/main/resources/skinner.properties
Normal file
5
app/src/main/resources/skinner.properties
Normal file
@ -0,0 +1,5 @@
|
||||
database.address = jdbc:mysql://localhost:3306/skinner
|
||||
database.username = skinner
|
||||
database.password = skinner
|
||||
|
||||
server.port = 8000
|
||||
Reference in New Issue
Block a user