University-of-Washington-Navigator / src / main / java / campuspaths / SparkServer.java
SparkServer.java
Raw
package campuspaths;

import campuspaths.utils.CORSFilter;
import pathfinder.ModelConnector;
import pathfinder.datastructures.Path;
import pathfinder.datastructures.Point;
import spark.*;
import com.google.gson.*;

public class SparkServer {

  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().

    Gson gson = new Gson();
    ModelConnector model = new ModelConnector();
    Spark.get("/buildings", new Route() {
      @Override
      public Object handle(Request request, Response response) throws Exception {
        System.out.println("Buildings called");
        return gson.toJson(model.buildingNames().keySet());
      }
    });
    Spark.get("/findPath", new Route() {
      @Override
      public Object handle(Request request, Response response) throws Exception {
        String start = request.queryParams("start");
        String end = request.queryParams("end");
        System.out.println("Path called on " + start + " to " + end);
        Path<Point> path = model.findShortestPath(start, end);
        System.out.println("Path found:");
        for (Path.Segment p : path) {
          System.out.println(p);
        }
        return gson.toJson(path);
      }
    });
  }

}