Skip to content

Commit 651b6f9

Browse files
committed
DB will be initialized once there is a database.json
1 parent 2d6c7ae commit 651b6f9

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

BackendServer.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
public class BackendServer {
1010

11-
static BackendDatabase db = new BackendDatabase("./database.json");
12-
Gson gson = new GsonBuilder().setLenient().create();
11+
static BackendDatabase db;
12+
1313

1414
/*private synchronized void handleClientRequest(Socket clientSocket) throws IOException {
1515
try {
@@ -60,6 +60,15 @@ public synchronized void startServer(int portNumber) {
6060

6161
public static void main(String[] args) {
6262
BackendServer server = new BackendServer();
63+
Gson gson = new GsonBuilder().setLenient().create();
64+
try {
65+
BackendDatabase tmp = gson.fromJson(new FileReader("./database.json"), BackendDatabase.class);
66+
BackendServer.db = tmp;
67+
System.out.println("Loaded initial db from: "+db.baseAddress);
68+
} catch (IOException io) {
69+
System.out.println("No initial DB. Creating a new one...");
70+
BackendServer.db =new BackendDatabase("./database.json");
71+
};
6372
server.startServer(8000);
6473
}
6574

@@ -77,12 +86,12 @@ public static synchronized APIResponse handleRequest(APIRequest req) {
7786
String finalUser = new Gson().toJson(user, User.class);
7887
result.payload = new Gson().fromJson(finalUser, JsonElement.class);
7988
result.statusCode = 200;
80-
result.message = "ورود موفق";
89+
result.message = "Successfully logged in!";
8190
db.update();
8291
}
8392
else {
8493
result.statusCode = 401;
85-
result.message = "نام کاربری یا رمزعبور اشتباه است.";
94+
result.message = "Wrong credentials";
8695
}
8796
} else {
8897
if (req.method.equals("POST") && req.route.equals("/user/signup/")) {
@@ -92,11 +101,11 @@ public static synchronized APIResponse handleRequest(APIRequest req) {
92101
String signUpEmail = signUpPayloadJson.get("email").toString();
93102
db.SignUp(signUpEmail, signUpUsername, signUpPass);
94103
result.statusCode = 200;
95-
result.message = "حساب کاربری شما با موفقیت ساخته شد.";
104+
result.message = "Account created successfully!";
96105
db.update();
97106
} else {
98107
result.statusCode = 401;
99-
result.message = "لطفا ابتدا وارد حساب کاربری خود شوید.";
108+
result.message = "Please login first!";
100109
}
101110
}
102111
else {

ExampleClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void main (String[] args){
2323

2424

2525

26-
Thread.sleep(2000);
26+
Thread.sleep(500);
2727
dout.flush();
2828

2929
String line;

RequestFormat.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"method": "POST",
2+
"method": "GET",
33
"username": "mohsen",
4-
"route": "/user/login/",
4+
"route": "/user/profile/",
55
"payload": {
66
"password": "1234",
77
"email": "[email protected]",

0 commit comments

Comments
 (0)