Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.config;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import org.neo4j.graphdb.GraphDatabaseService;
- import org.neo4j.graphdb.factory.GraphDatabaseFactory;
- import org.neo4j.io.fs.FileUtils;
- import org.neo4j.rest.graphdb.RestAPIImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Profile;
- import org.springframework.core.env.Environment;
- import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
- import java.io.File;
- import java.net.URL;
- @Configuration
- @Slf4j
- public class Neo4jConfiguration {
- private static final String HEROKU_NEO4J_ENV="GRAPHSTORY_URL";
- @Autowired
- private Environment environment;
- @Bean(name = "graphDatabaseService")
- @Profile("!unit")
- @SneakyThrows
- public GraphDatabaseService graphDatabaseServiceHeroku() {
- log.info("Using heroku neo4j");
- if (!environment.containsProperty(HEROKU_NEO4J_ENV))
- throw new RuntimeException(String.format("The heroku environment variable `%s` was not found", HEROKU_NEO4J_ENV));
- String url = environment.getProperty(HEROKU_NEO4J_ENV);
- URL realUrl = new URL(url);
- String newUrl = String.format("%s://%s:%d", realUrl.getProtocol(), realUrl.getHost(), realUrl.getPort());
- String[] auth = realUrl.getUserInfo().split(":");
- String user = auth[0];
- String passwd = auth[1];
- log.info("Using credentials: D='{}' U='{}' P='{}'", newUrl, user, passwd);
- return new SpringCypherRestGraphDatabase(new RestAPIImpl(newUrl, user, passwd));
- }
- @Bean(name = "graphDatabaseService", destroyMethod = "shutdown")
- @SneakyThrows
- @Profile("unit")
- public GraphDatabaseService graphDatabaseServiceUnit() {
- log.info("Using embedded neo4j");
- File tmpDbLocation = new File("tmp/db");
- FileUtils.deleteRecursively(tmpDbLocation);
- return new GraphDatabaseFactory().newEmbeddedDatabase(tmpDbLocation.getPath());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement