-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature.config-routes' into develop
- Loading branch information
Showing
9 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.iml | ||
.idea/ | ||
build/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"routes": [ | ||
{ | ||
"method":"GET", | ||
"route":"/helloworld", | ||
"implementation":"de.tlongo.roscoe.routes.HelloWorldRoute" | ||
}, | ||
{ | ||
"method":"POST", | ||
"route":"/helloworld", | ||
"implementation":"de.tlongo.roscoe.routes.SamplePostRoute" | ||
}, | ||
{ | ||
"method":"PUT", | ||
"route":"/helloworld", | ||
"implementation":"de.tlongo.roscoe.routes.SamplePutRoute" | ||
}, | ||
{ | ||
"method":"DELETE", | ||
"route":"/helloworld", | ||
"implementation":"de.tlongo.roscoe.routes.SampleDeleteRoute" | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package de.tlongo.roscoe.core; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public class ConfigManager { | ||
Logger logger = LoggerFactory.getLogger(ConfigManager.class); | ||
String roscoeRoot; | ||
File configDir; | ||
JsonObject routesConfig; | ||
|
||
public ConfigManager() { | ||
roscoeRoot = System.getProperty("roscoe.root"); | ||
configDir = new File(roscoeRoot + "/conf"); | ||
try { | ||
routesConfig = new JsonParser().parse(new FileReader(configDir + "/routes.json")).getAsJsonObject(); | ||
} catch (FileNotFoundException e) { | ||
logger.error("Could not load routes config {}", configDir + ("/routes.json")); | ||
throw new RuntimeException("Could not load routes config."); | ||
} | ||
} | ||
|
||
public List<RoscoeRoute> getRoutes() { | ||
final List<RoscoeRoute> routes = new ArrayList<>(); | ||
JsonArray jsonRoutesArray = routesConfig.getAsJsonArray("routes"); | ||
|
||
logger.debug("Loading Routes from config..."); | ||
jsonRoutesArray.forEach(route -> { | ||
JsonObject jsonRoute = route.getAsJsonObject(); | ||
|
||
String routeUrl = jsonRoute.get("route").getAsString(); | ||
String routeImplementation = jsonRoute.get("implementation").getAsString(); | ||
String routeMethod = jsonRoute.get("method").getAsString(); | ||
|
||
try { | ||
Class routeClass = Class.forName(routeImplementation); | ||
|
||
RoscoeRoute roscoeRoute = (RoscoeRoute)routeClass.newInstance(); | ||
roscoeRoute.setMethod(routeMethod); | ||
roscoeRoute.setRouteUrl(routeUrl); | ||
|
||
logger.debug("Loaded Route {}", roscoeRoute); | ||
|
||
routes.add(roscoeRoute); | ||
} catch (ClassNotFoundException e) { | ||
handleClassLoadingError(routeUrl, routeImplementation); | ||
} catch (InstantiationException e) { | ||
handleClassLoadingError(routeUrl, routeImplementation); | ||
} catch (IllegalAccessException e) { | ||
handleClassLoadingError(routeUrl, routeImplementation); | ||
} | ||
|
||
}); | ||
logger.debug("Found {} routes in config", routes.size()); | ||
|
||
return routes; | ||
} | ||
|
||
private void handleClassLoadingError(String routeName, String routeImplementation) { | ||
logger.error("Could not load implementation ({}) for Route {}", routeName, routeImplementation); | ||
throw new RuntimeException("Error loading route from config."); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.tlongo.roscoe.core; | ||
|
||
/** | ||
* Created by tomas on 02.01.15. | ||
*/ | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
|
||
import static spark.Spark.*; | ||
public class Roscoe { | ||
Logger logger = LoggerFactory.getLogger(Roscoe.class); | ||
|
||
public static void main(String[] args) { | ||
new Roscoe().go(); | ||
|
||
/** | ||
* Start the web app by reading the config and creating the routes. | ||
*/ | ||
|
||
// 1. Determine the framework route and export to a property | ||
// a. if none is specified in the config, determine it by yourself | ||
// b. create and store paths to folders under the fw root | ||
// - templates | ||
// - views | ||
// - assets | ||
// | ||
// 2. Create the routes | ||
// a. For every route in the config, create a spark-route and register it. | ||
|
||
|
||
|
||
|
||
} | ||
|
||
private void go() { | ||
File file = new File("."); | ||
logger.debug("Roscoe Root at: ", file.toPath().toAbsolutePath().toString()); | ||
System.setProperty("roscoe.root", file.toPath().toAbsolutePath().toString()); | ||
|
||
ConfigManager configManager = new ConfigManager(); | ||
|
||
configManager.getRoutes().forEach(route -> { | ||
if (route.getMethod().equals("GET")) { | ||
get(route.getRouteUrl(), route); | ||
} else if (route.getMethod().equals("POST")) { | ||
post(route.getRouteUrl(), route); | ||
} else if (route.getMethod().equals("PUT")) { | ||
put(route.getRouteUrl(), route); | ||
} if (route.getMethod().equals("DELETE")) { | ||
delete(route.getRouteUrl(), route); | ||
} | ||
}); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package de.tlongo.roscoe.core; | ||
|
||
import spark.Route; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public abstract class RoscoeRoute implements Route { | ||
String routeUrl; | ||
String method; | ||
|
||
public void setRouteUrl(String routeUrl) { | ||
this.routeUrl = routeUrl; | ||
} | ||
|
||
public void setMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
public String getRouteUrl() { | ||
return routeUrl; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RoscoeRoute{" + | ||
"routeUrl='" + routeUrl + '\'' + | ||
", method='" + method + '\'' + | ||
'}'; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/tlongo/roscoe/routes/HelloWorldRoute.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.tlongo.roscoe.routes; | ||
|
||
import de.tlongo.roscoe.core.RoscoeRoute; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public class HelloWorldRoute extends RoscoeRoute { | ||
|
||
@Override | ||
public Object handle(Request request, Response response) { | ||
return "Congratulations. You succesfully set up Roscoe"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/tlongo/roscoe/routes/SampleDeleteRoute.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.tlongo.roscoe.routes; | ||
|
||
import de.tlongo.roscoe.core.RoscoeRoute; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public class SampleDeleteRoute extends RoscoeRoute { | ||
|
||
@Override | ||
public Object handle(Request request, Response response) { | ||
return "This is a sample delete route."; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/tlongo/roscoe/routes/SamplePostRoute.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.tlongo.roscoe.routes; | ||
|
||
import de.tlongo.roscoe.core.RoscoeRoute; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public class SamplePostRoute extends RoscoeRoute { | ||
|
||
@Override | ||
public Object handle(Request request, Response response) { | ||
return "This is a sample post route."; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.tlongo.roscoe.routes; | ||
|
||
import de.tlongo.roscoe.core.RoscoeRoute; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
/** | ||
* Created by tomas on 11.01.15. | ||
*/ | ||
public class SamplePutRoute extends RoscoeRoute { | ||
|
||
@Override | ||
public Object handle(Request request, Response response) { | ||
return "This is a sample put route."; | ||
} | ||
} |