8
8
9
9
public class BackendServer {
10
10
11
- static BackendDatabase db = new BackendDatabase ( "./database.json" ) ;
12
- Gson gson = new GsonBuilder (). setLenient (). create ();
11
+ static BackendDatabase db ;
12
+
13
13
14
14
/*private synchronized void handleClientRequest(Socket clientSocket) throws IOException {
15
15
try {
@@ -60,6 +60,15 @@ public synchronized void startServer(int portNumber) {
60
60
61
61
public static void main (String [] args ) {
62
62
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
+ };
63
72
server .startServer (8000 );
64
73
}
65
74
@@ -77,12 +86,12 @@ public static synchronized APIResponse handleRequest(APIRequest req) {
77
86
String finalUser = new Gson ().toJson (user , User .class );
78
87
result .payload = new Gson ().fromJson (finalUser , JsonElement .class );
79
88
result .statusCode = 200 ;
80
- result .message = "ورود موفق " ;
89
+ result .message = "Successfully logged in! " ;
81
90
db .update ();
82
91
}
83
92
else {
84
93
result .statusCode = 401 ;
85
- result .message = "نام کاربری یا رمزعبور اشتباه است. " ;
94
+ result .message = "Wrong credentials " ;
86
95
}
87
96
} else {
88
97
if (req .method .equals ("POST" ) && req .route .equals ("/user/signup/" )) {
@@ -92,11 +101,11 @@ public static synchronized APIResponse handleRequest(APIRequest req) {
92
101
String signUpEmail = signUpPayloadJson .get ("email" ).toString ();
93
102
db .SignUp (signUpEmail , signUpUsername , signUpPass );
94
103
result .statusCode = 200 ;
95
- result .message = "حساب کاربری شما با موفقیت ساخته شد. " ;
104
+ result .message = "Account created successfully! " ;
96
105
db .update ();
97
106
} else {
98
107
result .statusCode = 401 ;
99
- result .message = "لطفا ابتدا وارد حساب کاربری خود شوید. " ;
108
+ result .message = "Please login first! " ;
100
109
}
101
110
}
102
111
else {
0 commit comments