Skip to content

Commit

Permalink
Ready for env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
alamorre committed Dec 13, 2022
1 parent edeb7f1 commit bf6a523
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 27 deletions.
2 changes: 1 addition & 1 deletion requests.rest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ POST http://localhost:3001/signup
Content-Type: application/json

{
"username": "zack",
"username": "zack1",
"secret": "pass1234",
"email": "[email protected]",
"first_name": "Zack",
Expand Down
12 changes: 12 additions & 0 deletions server-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,107 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@RestController
public class UserController {
// private static String CHAT_ENGINE_PROJECT_ID =
// "5d498a31-cd23-42b7-b367-4fcc9463bd2f";
private static String CHAT_ENGINE_PROJECT_ID = "5d498a31-cd23-42b7-b367-4fcc9463bd2f";
private static String CHAT_ENGINE_PRIVATE_KEY = "49a46286-91c3-4f9c-92bf-284ae51b7628";

@RequestMapping(path = "/login", method = RequestMethod.GET)
public String getLogin() {
return "user allowed";
@RequestMapping(path = "/login", method = RequestMethod.POST)
public Map<String, Object> getLogin(@RequestBody HashMap<String, String> request) {
HttpURLConnection con = null;
try {
// Create connection
URL url = new URL("https://api.chatengine.io/users/me");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
// Set headers
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Project-ID", CHAT_ENGINE_PROJECT_ID);
con.setRequestProperty("User-Name", request.get("username"));
con.setRequestProperty("User-Secret", request.get("secret"));
// Generate response
StringBuilder responseStr = new StringBuilder();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
responseStr.append(responseLine.trim());
}
}
// Jsonify and return response
Map<String, Object> response = new Gson().fromJson(
responseStr.toString(), new TypeToken<HashMap<String, Object>>() {
}.getType());
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (con != null) {
con.disconnect();
}
}
}

@RequestMapping(path = "/signup", method = RequestMethod.GET)
public String newSignup() {
HttpURLConnection connection = null;
@RequestMapping(path = "/signup", method = RequestMethod.POST)
public Map<String, Object> newSignup(@RequestBody HashMap<String, String> request) {
HttpURLConnection con = null;
try {
// Create connection
URL url = new URL("https://api.chatengine.io/users");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// Headers
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Private-Key", CHAT_ENGINE_PRIVATE_KEY);
// Add body
connection.setDoOutput(true);
String jsonInputString = "{\"username\": \"adam1\", \"secret\": \"pass1234\", \"first_name\": \"adam\", \"last_name\": \"lamorre\", \"email\": \"[email protected]\"}";
try (OutputStream os = connection.getOutputStream()) {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
// Set headers
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Private-Key", CHAT_ENGINE_PRIVATE_KEY);
// Add request body
con.setDoOutput(true);
Map<String, String> body = new HashMap<String, String>();
body.put("username", request.get("username"));
body.put("secret", request.get("secret"));
body.put("email", request.get("email"));
body.put("first_name", request.get("first_name"));
body.put("last_name", request.get("last_name"));
String jsonInputString = new JSONObject(body).toString();
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
// Generate and return response
// Generate response
StringBuilder responseStr = new StringBuilder();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
new InputStreamReader(con.getInputStream(), "utf-8"))) {
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
responseStr.append(responseLine.trim());
}
return response.toString();
}

// Jsonify and return response
Map<String, Object> response = new Gson().fromJson(
responseStr.toString(), new TypeToken<HashMap<String, Object>>() {
}.getType());
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
if (con != null) {
con.disconnect();
}
}
}
Expand Down

0 comments on commit bf6a523

Please sign in to comment.