-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparkServer.java
executable file
·68 lines (54 loc) · 2.21 KB
/
SparkServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package campuspaths;
import campuspaths.utils.CORSFilter;
import com.google.gson.Gson;
import pathfinder.ModelConnector;
import pathfinder.datastructures.Point;
import pathfinder.parser.CampusBuilding;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import pathfinder.datastructures.Path;
public class SparkServer {
private static Gson gson = new Gson();
private static ModelConnector connector = new ModelConnector();
public static void main(String[] args) {
CORSFilter corsFilter = new CORSFilter();
corsFilter.apply();
// The above two lines help set up some settings that allow the
// React application to make requests to the Spark server, even though it
// comes from a different server.
// You should leave these two lines at the very beginning of main().
Spark.get("/getPaths", new Route() {
@Override
public Object handle(Request request, Response response) throws Exception {
String startBuilding = request.queryParams("start");
String destBuilding = request.queryParams("dest");
if(startBuilding == null || destBuilding == null) {
// You can also have a message in "halt" that is displayed in the page.
Spark.halt(400, "must have start and end");
}
Path<Point> path = connector.findShortestPath(startBuilding, destBuilding);
// Let's get a Gson object so we can convert our Path to a JSON String
// before we send it back.
// Gson is Google's library for dealing with JSON. It'll take any Java Object
// and convert it to a JSON string that represents the data inside that object.
String jsonResponse = gson.toJson(path);
return jsonResponse;
}
});
Spark.get("/getBuildings", new Route() {
@Override
public Object handle(Request request, Response response) throws Exception {
// As a first example, let's just return a static string.
Map<String, String> buildings = connector.buildingNames();
String jsonResponse = gson.toJson(buildings);
return jsonResponse;
}
});
}
}