Search in sources :

Example 1 with JsonTransformer

use of com.searchcode.app.util.JsonTransformer in project searchcode-server by boyter.

the class App method main.

public static void main(String[] args) {
    // Database migrations happen before we start
    preStart();
    Singleton.getLogger().info("Starting searchcode server on port " + getServerPort());
    if (getOnlyLocalhost()) {
        Singleton.getLogger().info("Only listening on 127.0.0.1 ");
        Spark.ipAddress("127.0.0.1");
    }
    Spark.port(getServerPort());
    Spark.staticFileLocation("/public");
    Singleton.getJobService().initialJobs();
    ////////////////////////////////////////////////////
    //          Search/Code Routes Below
    ////////////////////////////////////////////////////
    get("/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(codeRouteService.root(request, response));
    });
    get("/html/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(codeRouteService.html(request, response));
    });
    get("/literal/", (request, response) -> {
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.literalSearch(request, response), "index.ftl"));
    });
    get("/file/:codeid/:reponame/*", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getCode(request, response), "coderesult.ftl"));
    });
    get("/repository/overview/:reponame/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getProject(request, response), "repository_overview.ftl"));
    });
    get("/repository/list/", (request, response) -> {
        response.header("Content-Encoding", "gzip");
        CodeRouteService codeRouteService = new CodeRouteService();
        return new FreeMarkerEngine().render(new ModelAndView(codeRouteService.getRepositoryList(request, response), "repository_list.ftl"));
    });
    ////////////////////////////////////////////////////
    //              Page Routes Below
    ////////////////////////////////////////////////////
    get("/documentation/", (request, response) -> {
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new FreeMarkerEngine().render(new ModelAndView(map, "documentation.ftl"));
    });
    get("/404/", (request, response) -> {
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new FreeMarkerEngine().render(new ModelAndView(map, "404.ftl"));
    });
    ////////////////////////////////////////////////////
    //              API Routes Below
    ////////////////////////////////////////////////////
    path("/api", () -> {
        get("/codesearch/", (request, response) -> {
            addJsonHeaders(response);
            SearchRouteService searchRouteService = new SearchRouteService();
            return new JsonTransformer().render(searchRouteService.codeSearch(request, response));
        });
        get("/codesearch/rss/", (request, response) -> {
            addXmlHeaders(response);
            SearchRouteService searchRouteService = new SearchRouteService();
            Map<String, Object> map = new HashMap<>();
            map.put("result", searchRouteService.codeSearch(request, response));
            map.put("hostname", Properties.getProperties().getProperty(Values.HOST_NAME, Values.DEFAULT_HOST_NAME));
            return new FreeMarkerEngine().render(new ModelAndView(map, "codesearchrss.ftl"));
        });
        get("/timecodesearch/", (request, response) -> {
            addJsonHeaders(response);
            TimeSearchRouteService ars = new TimeSearchRouteService();
            return new JsonTransformer().render(ars.getTimeSearch(request, response));
        });
        path("/repo", () -> {
            get("/add/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoAdd(request, response));
            });
            get("/delete/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoDelete(request, response));
            });
            get("/list/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repoList(request, response));
            });
            get("/reindex/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repositoryReindex(request, response));
            });
            ////////////////////////////////////////////////////
            //          Unsecured API Routes Below
            ////////////////////////////////////////////////////
            get("/index/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.repositoryIndex(request, response));
            });
            get("/filecount/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getFileCount(request, response);
            });
            get("/indextime/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getIndexTime(request, response);
            });
            get("/indextimeseconds/", "application/json", (request, response) -> {
                ApiRouteService apiRouteService = new ApiRouteService();
                return apiRouteService.getAverageIndexTimeSeconds(request, response);
            });
            get("/repo/", "application/json", (request, response) -> {
                addJsonHeaders(response);
                ApiRouteService apiRouteService = new ApiRouteService();
                return new JsonTransformer().render(apiRouteService.getRepo(request, response));
            });
        });
    });
    ////////////////////////////////////////////////////
    //              Admin Routes Below
    ////////////////////////////////////////////////////
    get("/login/", (request, response) -> {
        if (getAuthenticatedUser(request) != null) {
            response.redirect("/admin/");
            halt();
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new FreeMarkerEngine().render(new ModelAndView(map, "login.ftl"));
    });
    post("/login/", (request, response) -> {
        if (request.queryParams().contains("password") && request.queryParams("password").equals(com.searchcode.app.util.Properties.getProperties().getProperty("password"))) {
            addAuthenticatedUser(request);
            response.redirect("/admin/");
            halt();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        if (request.queryParams().contains("password")) {
            map.put("passwordInvalid", true);
        }
        return new FreeMarkerEngine().render(new ModelAndView(map, "login.ftl"));
    });
    get("/logout/", (req, res) -> {
        removeAuthenticatedUser(req);
        res.redirect("/");
        return null;
    });
    path("/admin", () -> {
        get("/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminPage(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin.ftl"));
        });
        get("/repo/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminRepo(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
        });
        post("/repo/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            ValidatorResult validatorResult = adminRouteService.postRepo(request, response);
            if (!validatorResult.isValid) {
                Map<String, Object> map = adminRouteService.adminRepo(request, response);
                map.put("validatorResult", validatorResult);
                return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repo.ftl"));
            }
            String[] returns = request.queryParamsValues("return");
            if (returns != null) {
                response.redirect("/admin/repo/");
            } else {
                response.redirect("/admin/repolist/");
            }
            halt();
            return null;
        });
        get("/repolist/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminRepo(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_repolist.ftl"));
        });
        get("/bulk/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = new HashMap<>();
            map.put("logoImage", CommonRouteService.getLogo());
            map.put("isCommunity", ISCOMMUNITY);
            map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
            map.put("repoCount", adminRouteService.getStat("repoCount"));
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_bulk.ftl"));
        });
        post("/bulk/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            List<ValidatorResult> validatorResults = adminRouteService.postBulk(request, response);
            if (!validatorResults.isEmpty()) {
                Map<String, Object> map = new HashMap<>();
                map.put("logoImage", CommonRouteService.getLogo());
                map.put("isCommunity", ISCOMMUNITY);
                map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
                map.put("repoCount", adminRouteService.getStat("repoCount"));
                map.put("validatorResults", validatorResults);
                return new FreeMarkerEngine().render(new ModelAndView(map, "admin_bulk.ftl"));
            }
            response.redirect("/admin/repolist/");
            halt();
            return null;
        });
        get("/settings/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminSettings(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_settings.ftl"));
        });
        get("/logs/", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            Map<String, Object> map = adminRouteService.adminLogs(request, response);
            return new FreeMarkerEngine().render(new ModelAndView(map, "admin_logs.ftl"));
        });
        post("/settings/", (request, response) -> {
            checkLoggedIn(request, response);
            if (ISCOMMUNITY) {
                response.redirect("/admin/settings/");
                halt();
            }
            AdminRouteService adminRouteService = new AdminRouteService();
            adminRouteService.postSettings(request, response);
            response.redirect("/admin/settings/");
            halt();
            return null;
        });
        get("/delete/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            adminRouteService.deleteRepo(request, response);
            return new JsonTransformer().render(true);
        });
        post("/rebuild/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            boolean result = Singleton.getJobService().rebuildAll();
            if (result) {
                Singleton.getJobService().forceEnqueue();
            }
            return new JsonTransformer().render(result);
        });
        post("/forcequeue/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            return Singleton.getJobService().forceEnqueue();
        }, new JsonTransformer());
        post("/togglepause/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            Singleton.getSharedService().setPauseBackgroundJobs(!Singleton.getSharedService().getPauseBackgroundJobs());
            return new JsonTransformer().render(Singleton.getSharedService().getPauseBackgroundJobs());
        });
        post("/clearsearchcount/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            Singleton.getStatsService().clearSearchCount();
            return new JsonTransformer().render(Values.EMPTYSTRING);
        });
        post("/resetspellingcorrector/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            Singleton.getSpellingCorrector().reset();
            return new JsonTransformer().render(Values.EMPTYSTRING);
        });
        get("/checkversion/", "application/json", (request, response) -> {
            checkLoggedIn(request, response);
            AdminRouteService adminRouteService = new AdminRouteService();
            return adminRouteService.checkVersion();
        });
        path("/api", () -> {
            get("/", (request, response) -> {
                checkLoggedIn(request, response);
                AdminRouteService adminRouteService = new AdminRouteService();
                Map<String, Object> map = adminRouteService.adminApi(request, response);
                return new FreeMarkerEngine().render(new ModelAndView(map, "admin_api.ftl"));
            });
            post("/", (request, response) -> {
                checkLoggedIn(request, response);
                Singleton.getApiService().createKeys();
                response.redirect("/admin/api/");
                halt();
                return null;
            });
            get("/delete/", "application/json", (request, response) -> {
                checkLoggedIn(request, response);
                if (getAuthenticatedUser(request) == null || !request.queryParams().contains("publicKey")) {
                    response.redirect("/login/");
                    halt();
                    return new JsonTransformer().render(false);
                }
                String publicKey = request.queryParams("publicKey");
                Singleton.getApiService().deleteKey(publicKey);
                return new JsonTransformer().render(true);
            });
            get("/getstat/", (request, response) -> {
                checkLoggedIn(request, response);
                AdminRouteService adminRouteService = new AdminRouteService();
                return adminRouteService.getStat(request, response);
            });
            get("/getstatjson/", "application/json", (request, response) -> {
                checkLoggedIn(request, response);
                AdminRouteService adminRouteService = new AdminRouteService();
                return new JsonTransformer().render(adminRouteService.getStat(request, response));
            });
            get("/checkindexstatus/", "application/json", (request, response) -> {
                // TODO move this to unsecured routes
                AdminRouteService adminRouteService = new AdminRouteService();
                return adminRouteService.checkIndexStatus(request, response);
            });
        });
    });
}
Also used : FreeMarkerEngine(spark.template.freemarker.FreeMarkerEngine) HashMap(java.util.HashMap) ValidatorResult(com.searchcode.app.model.ValidatorResult) ModelAndView(spark.ModelAndView) JsonTransformer(com.searchcode.app.util.JsonTransformer)

Aggregations

ValidatorResult (com.searchcode.app.model.ValidatorResult)1 JsonTransformer (com.searchcode.app.util.JsonTransformer)1 HashMap (java.util.HashMap)1 ModelAndView (spark.ModelAndView)1 FreeMarkerEngine (spark.template.freemarker.FreeMarkerEngine)1