From ef37379e40532c8082b49022b9925d9dfb1172b3 Mon Sep 17 00:00:00 2001
From: Fredrik Fonn Hansen <fredrfha@stud.ntnu.no>
Date: Mon, 10 Apr 2023 16:27:05 +0200
Subject: [PATCH] Create a common settings file for backed-url

---
 frontend/assets/config.properties             |  8 +++++
 .../src/com/game/tankwars/ConfigReader.java   | 29 +++++++++++++++++++
 .../controller/LeaderboardController.java     |  4 ++-
 .../tankwars/controller/LoginController.java  |  3 +-
 4 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 frontend/assets/config.properties
 create mode 100644 frontend/core/src/com/game/tankwars/ConfigReader.java

diff --git a/frontend/assets/config.properties b/frontend/assets/config.properties
new file mode 100644
index 0000000..0289a24
--- /dev/null
+++ b/frontend/assets/config.properties
@@ -0,0 +1,8 @@
+app.name=TankWars
+app.version=1.0
+backend.host=10.212.26.72
+backend.port=80
+backend.protocol=http
+backend.url=http://10.212.26.72
+
+# local development: change backend-url to http://localhost:80
diff --git a/frontend/core/src/com/game/tankwars/ConfigReader.java b/frontend/core/src/com/game/tankwars/ConfigReader.java
new file mode 100644
index 0000000..bcebe67
--- /dev/null
+++ b/frontend/core/src/com/game/tankwars/ConfigReader.java
@@ -0,0 +1,29 @@
+package com.game.tankwars;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.files.FileHandle;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+public class ConfigReader {
+    private static Properties properties = new Properties();
+
+    static {
+        try {
+            FileHandle fileHandle = Gdx.files.internal("config.properties");
+            InputStream input = fileHandle.read();
+            properties.load(input);
+        } catch (IOException e) {
+            System.err.println("Error loading configuration file.");
+            e.printStackTrace();
+        }
+    }
+
+    public static String getProperty(String key) {
+        return properties.getProperty(key);
+    }
+}
diff --git a/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java b/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java
index 60750d1..0818ad1 100644
--- a/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java
+++ b/frontend/core/src/com/game/tankwars/controller/LeaderboardController.java
@@ -6,6 +6,7 @@ import com.badlogic.gdx.net.HttpRequestBuilder;
 import com.badlogic.gdx.utils.Array;
 import com.badlogic.gdx.utils.Json;
 import com.game.tankwars.Callback;
+import com.game.tankwars.ConfigReader;
 import com.game.tankwars.ReceiverHandler;
 import com.game.tankwars.model.User;
 
@@ -32,7 +33,8 @@ public class LeaderboardController {
             }
         };
         // Define the URL for the HTTP request
-        String url = "http://localhost:3000/highscores";
+        String url = ConfigReader.getProperty("backend.url") + "/highscores";
+
         // Create a new HttpRequest using the HttpRequestBuilder class
         Net.HttpRequest httpRequest = new HttpRequestBuilder()
                 .newRequest()
diff --git a/frontend/core/src/com/game/tankwars/controller/LoginController.java b/frontend/core/src/com/game/tankwars/controller/LoginController.java
index 04847b9..b5ea7dd 100644
--- a/frontend/core/src/com/game/tankwars/controller/LoginController.java
+++ b/frontend/core/src/com/game/tankwars/controller/LoginController.java
@@ -13,6 +13,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextField;
 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 import com.badlogic.gdx.utils.Json;
 import com.game.tankwars.Callback;
+import com.game.tankwars.ConfigReader;
 import com.game.tankwars.ReceiverHandler;
 import com.game.tankwars.TankWarsGame;
 import com.game.tankwars.model.CurrentUser;
@@ -119,7 +120,7 @@ public class LoginController {
             }
         };
 
-        String url = String.format("http://localhost:80/user/create/%s", username);
+        String url = ConfigReader.getProperty("backend.url") + "/user/create/" + username;
         Net.HttpRequest httpRequest = new HttpRequestBuilder()
                 .newRequest()
                 .method(Net.HttpMethods.POST)
-- 
GitLab