Skip to content

Commit

Permalink
DependencyInjection: created DI for server
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarygdala committed Oct 27, 2022
1 parent 7a9924c commit 4b39975
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class App

public static void main(String[] args) throws IOException
{
ReqHandlerComponent component = DaggerReqHandlerComponent.create();
HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
server.createContext("/api/v1/", component.buildHandler());
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
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 4b39975

Please sign in to comment.