move handler + start reddit session
parent
4b47d907cd
commit
db654ddfdd
@ -1,45 +1,24 @@
|
||||
package sh.adb.RandomRedditMemesAPI;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.github.jreddit.oauth.exception.RedditOAuthException;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
public static void main(String[] args) throws IOException, RedditOAuthException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
||||
server.createContext("", new ErrorHandler());
|
||||
//server.createContext("/api/v1/", new StandardHandler());
|
||||
server.createContext("/api/v1/r/", new CommunityHandler());
|
||||
server.setExecutor(null);
|
||||
server.start();
|
||||
|
||||
RedditAPI client = new RedditAPI();
|
||||
System.out.println("the token is");
|
||||
System.out.println(client.token);
|
||||
}
|
||||
}
|
||||
class CommunityHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange t) throws IOException {
|
||||
InputStream is = t.getRequestBody();
|
||||
//is.read();
|
||||
String response = "This is the response";
|
||||
t.sendResponseHeaders(200, response.length());
|
||||
OutputStream os = t.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
class ErrorHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange t) throws IOException {
|
||||
InputStream is = t.getRequestBody();
|
||||
//is.read();
|
||||
String response = "404";
|
||||
t.sendResponseHeaders(404, response.length());
|
||||
OutputStream os = t.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue