Created
September 10, 2019 08:43
-
-
Save doorbash/06d4afca1f024d9ad375285a163577c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.MongoClient; | |
import com.mongodb.client.MongoCollection; | |
import com.mongodb.client.MongoCursor; | |
import com.mongodb.client.MongoDatabase; | |
import org.bson.Document; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.io.*; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Date; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ThreadPoolExecutor; | |
public class JavaHTTPServer { | |
private static final int PORT = 8081; | |
private static final int NUM_THREADS = 10; | |
private ThreadPoolExecutor threadPoolExecutor; | |
private MongoDatabase database; | |
public JavaHTTPServer() { | |
threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(NUM_THREADS); | |
MongoClient client = null; | |
try { | |
client = new MongoClient("localhost", 27017); | |
database = client.getDatabase("mydb"); | |
ServerSocket serverConnect = new ServerSocket(PORT); | |
System.out.println("Server started.\nListening for connections on port : " + PORT + " ...\n"); | |
while (true) threadPoolExecutor.execute(new Worker(serverConnect.accept())); | |
} catch (IOException e) { | |
System.err.println("Server Connection error : " + e.getMessage()); | |
} finally { | |
if (client != null) client.close(); | |
} | |
} | |
class Worker implements Runnable { | |
Socket socket; | |
public Worker(Socket socket) { | |
this.socket = socket; | |
} | |
@Override | |
public void run() { | |
BufferedReader in = null; | |
PrintWriter out = null; | |
BufferedOutputStream dataOut = null; | |
try { | |
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
out = new PrintWriter(socket.getOutputStream()); | |
dataOut = new BufferedOutputStream(socket.getOutputStream()); | |
String input = in.readLine(); | |
if(input == null) return; | |
String[] parts = input.split(" "); | |
String method = parts[0]; | |
String path = parts[1]; | |
System.out.println("method is " + method); | |
System.out.println("path is " + path); | |
if (method.equals("GET") && path.equals("/user/")) { | |
MongoCollection users = database.getCollection("users"); | |
MongoCursor cursor = users.find().iterator(); | |
JSONArray list = new JSONArray(); | |
while (cursor.hasNext()) { | |
Document document = (Document) cursor.next(); | |
JSONObject user = new JSONObject(); | |
user.put("id", document.getObjectId("_id")); | |
user.put("name", document.getString("name")); | |
user.put("coins", document.getInteger("coins")); | |
JSONArray emojis = new JSONArray(); | |
for (int emoji : document.getList("emojis", Integer.class)) { | |
emojis.put(emoji); | |
} | |
user.put("emojis", emojis); | |
user.put("createdAt", document.getDate("createdAt").getTime()); | |
list.put(user); | |
} | |
String res = list.toString(); | |
out.println("HTTP/1.1 200 OK"); | |
out.println("Server: Java HTTP Server"); | |
out.println("Date: " + new Date()); | |
out.println("Content-type: " + "application/json"); | |
out.println("Content-length: " + res.length()); | |
out.println(); | |
out.flush(); | |
dataOut.write(res.getBytes(), 0, res.length()); | |
dataOut.flush(); | |
} | |
} catch (Exception ioe) { | |
ioe.printStackTrace(); | |
System.err.println("Server error : " + ioe); | |
} finally { | |
try { | |
in.close(); | |
out.close(); | |
dataOut.close(); | |
socket.close(); // we close socket connection | |
} catch (Exception e) { | |
System.err.println("Error closing stream : " + e.getMessage()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment