示例#1
0
  protected void initWebServer(ServiceProvider serviceProvider, IdeArguments ideArguments) {
    port(ideArguments.getPort());
    staticFileLocation("/public");
    externalStaticFileLocation(staticFolderForSpark);
    System.out.println("Reports are in: " + reportFolder);

    new DeviceController(serviceProvider.deviceService());
    new DomSnapshotController(serviceProvider.domSnapshotService());
    new FileBrowserController(serviceProvider.fileBrowserService());
    new SettingsController(serviceProvider.settingsService());
    new ProfilesController(serviceProvider.profilesService(), serviceProvider.settingsService());
    new TaskResultController(serviceProvider.taskResultService());
    new TesterController(serviceProvider.testerService());
    new HelpController();

    scheduledExecutorService.scheduleAtFixedRate(
        new TaskResultsStorageCleanupJob(
            taskResultsStorage,
            ideArguments.getKeepLastResults(),
            ideArguments.getZombieResultsTimeout(),
            reportFolder),
        ideArguments.getCleanupPeriodInMinutes(),
        ideArguments.getCleanupPeriodInMinutes(),
        TimeUnit.MINUTES);

    if (ideArguments.getProfile() != null) {
      serviceProvider.profilesService().loadProfile(ideArguments.getProfile());
    }
  }
  public static void main(String[] args) throws SQLException {
    // open database
    Connection conn = DriverManager.getConnection("jdbc:h2:./main");
    createTables(conn);

    // serve external files
    Spark.externalStaticFileLocation("public");
    Spark.init();

    // insert test data
    if (selectCountries(conn).size() == 0) {
      insertCountry(conn, "United States", "US");
      insertCountry(conn, "Canada", "CA");
      insertCountry(conn, "Mexico", "MX");
    }

    // create routes for AJAX
    Spark.get(
        "/get-countries",
        ((request, response) -> {
          JsonSerializer serializer = new JsonSerializer();
          String json = serializer.serialize(selectCountries(conn));
          return json;
        }));
    Spark.get(
        "/get-country",
        ((request, response) -> {
          String id = request.queryParams("id");
          try {
            int idNum = Integer.valueOf(id);
            JsonSerializer serializer = new JsonSerializer();
            String json = serializer.serialize(selectCountry(conn, idNum));
            return json;
          } catch (Exception e) {

          }
          return "";
        }));
    Spark.post(
        "/add-country",
        ((request, response) -> {
          String name = request.queryParams("name");
          String abbrev = request.queryParams("abbrev");
          if (name == null || abbrev == null) {
            Spark.halt(403);
          }
          insertCountry(conn, name, abbrev);
          return "";
        }));
  }