Skip to content

Commit

Permalink
Merge pull request #5 from UTSCCSCC01/feature/DependencyInjection
Browse files Browse the repository at this point in the history
Feature/dependency injection
  • Loading branch information
BillyZ1435 authored Oct 27, 2022
2 parents 92c34be + 4b39975 commit a021086
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public class App

public static void main(String[] args) throws IOException
{
HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
server.createContext("/api/v1/", new ReqHandler());
server.start();
ReqHandlerComponent handlerComponent = DaggerReqHandlerComponent.create();
ServerComponent serverComponent = DaggerServerComponent.create();
Server server = serverComponent.buildServer();
server.createContext("/api/v1/", handlerComponent.buildHandler());
server.startServer();

// TODO Create Your Server Context Here, There Should Only Be One Context
System.out.printf("Server started on port %d\n", port);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import org.neo4j.driver.*;
import org.neo4j.driver.Record;


import java.util.List;

import javax.inject.Inject;

// All your database transactions or queries should
// go in this class
public class Neo4jDAO {
Expand All @@ -16,9 +19,9 @@ public class Neo4jDAO {
private final String username = "neo4j";
private final String password = "123456";


public Neo4jDAO() {
this.driver = GraphDatabase.driver(this.uriDb, AuthTokens.basic(this.username, this.password));
@Inject
public Neo4jDAO(Driver driver) {
this.driver = driver;
this.session = this.driver.session();

}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.OutputStream;
import java.net.URI;

import javax.inject.Inject;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.JSONException;
Expand All @@ -13,10 +15,11 @@ public class ReqHandler implements HttpHandler {

// TODO Complete This Class

public Neo4jDAO dao;
private Neo4jDAO dao;

public ReqHandler() {
this.dao = new Neo4jDAO();
@Inject
public ReqHandler(Neo4jDAO dao) {
this.dao = dao;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@Singleton
// TODO Uncomment The Line Below When You Have Implemented ReqHandlerModule
// @Component(modules = ReqHandlerModule.class)
@Component(modules = ReqHandlerModule.class)
public interface ReqHandlerComponent {

public ReqHandler buildHandler();
}
17 changes: 17 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package ca.utoronto.utm.mcs;

import dagger.Module;
import dagger.Provides;
import org.neo4j.driver.*;


@Module
public class ReqHandlerModule {
// TODO Complete This Module
private final String uriDb = "bolt://localhost:7687";
private final String username = "neo4j";
private final String password = "123456";

@Provides
public Neo4jDAO provideNeo4jDAO(Driver driver) {

return new Neo4jDAO(driver);
}

@Provides
public Driver provideDriver(){
return GraphDatabase.driver(this.uriDb, AuthTokens.basic(this.username, this.password));
}
}
15 changes: 15 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/Server.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package ca.utoronto.utm.mcs;

import com.sun.net.httpserver.HttpServer;

import javax.inject.Inject;

public class Server {
// TODO Complete This Class
private HttpServer httpServer;
@Inject
public Server(HttpServer httpServer){
this.httpServer = httpServer;
}
public void createContext(String path, ReqHandler reqHandler){
this.httpServer.createContext(path, reqHandler);
}
public void startServer(){
this.httpServer.start();
}
}
4 changes: 2 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/ServerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import javax.inject.Singleton;

@Singleton
// TODO Uncomment The Line Below When You Have Implemented ServerModule
// @Component(modules = ServerModule.class)

@Component(modules = ServerModule.class)
public interface ServerComponent {

public Server buildServer();
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ServerModule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
package ca.utoronto.utm.mcs;

import com.sun.net.httpserver.HttpServer;
import dagger.Module;
import dagger.Provides;

import java.io.IOException;
import java.net.InetSocketAddress;

@Module
public class ServerModule {
// TODO Complete This Module
@Provides
public InetSocketAddress provideInetSocketAddress(){
return new InetSocketAddress("0.0.0.0", 8080);
}
@Provides
public HttpServer provideHttpServer(InetSocketAddress inetSocketAddress) {
try {
HttpServer server = HttpServer.create(inetSocketAddress, 0);

return server;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Provides
public Server provideServer(HttpServer httpServer){
return new Server(httpServer);
}
}

0 comments on commit a021086

Please sign in to comment.