Skip to content

Commit

Permalink
Migrate to docker #641 - move all addresses to configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Jan 29, 2020
1 parent be3b9b7 commit 2d0c3f8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 24 deletions.
22 changes: 11 additions & 11 deletions IsraelHiking.API/Executors/OsmLatestFileFetcherExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ namespace IsraelHiking.API.Executors
public class OsmLatestFileFetcherExecutor : IOsmLatestFileFetcherExecutor
{
private const string OSM_C_TOOLS_FOLDER = "OsmCTools";
private const string OSM_FILE_ADDRESS = "http://download.openstreetmap.fr/extracts/asia/israel_and_palestine-latest.osm.pbf";
private const string OSM_FILE_TIMESTAMP = "http://download.openstreetmap.fr/extracts/asia/israel_and_palestine.state.txt";
private const string MINUTES_FILES_BASE_ADDRESS = "http://download.openstreetmap.fr/replication/asia/israel_and_palestine";
private const string UPDATES_FILE_NAME = "israel-and-palestine-updates.osc";
private const string OSM_UPDATE_EXE = "osmup.exe";
private const string OSM_CONVERT_EXE = "osmconvert.exe";
Expand Down Expand Up @@ -80,20 +77,23 @@ public Stream Get()

private async Task DownloadDailyOsmFile(string workingDirectory)
{
var response = await _remoteFileFetcherGateway.GetFileContent(OSM_FILE_ADDRESS);
var response = await _remoteFileFetcherGateway.GetFileContent(_options.OsmFileAddress);
_fileSystemHelper.WriteAllBytes(Path.Combine(workingDirectory, Sources.OSM_FILE_NAME), response.Content);

// Update timestamp to match the one from the server.
var file = await _remoteFileFetcherGateway.GetFileContent(OSM_FILE_TIMESTAMP);
var stringContent = Encoding.UTF8.GetString(file.Content);
var lastLine = stringContent.Split('\n').Last(s => !string.IsNullOrWhiteSpace(s));
var timeStamp = lastLine.Split('=').Last().Replace("\\", "");
RunOsmConvert($"--timestamp={timeStamp} {Sources.OSM_FILE_NAME}", workingDirectory);
if (!string.IsNullOrWhiteSpace(_options.OsmFileTimeStampAddress))
{
// Update timestamp to match the one from the server.
var file = await _remoteFileFetcherGateway.GetFileContent(_options.OsmFileTimeStampAddress);
var stringContent = Encoding.UTF8.GetString(file.Content);
var lastLine = stringContent.Split('\n').Last(s => !string.IsNullOrWhiteSpace(s));
var timeStamp = lastLine.Split('=').Last().Replace("\\", "");
RunOsmConvert($"--timestamp={timeStamp} {Sources.OSM_FILE_NAME}", workingDirectory);
}
}

private void UpdateFileToLatestVersion(string workingDirectory)
{
_processHelper.Start(OSM_UPDATE_EXE, $"{Sources.OSM_FILE_NAME} {UPDATES_FILE_NAME} --base-url={MINUTES_FILES_BASE_ADDRESS} --minute", workingDirectory);
_processHelper.Start(OSM_UPDATE_EXE, $"{Sources.OSM_FILE_NAME} {UPDATES_FILE_NAME} --base-url={_options.OsmMinutsFileBaseAddress} --minute", workingDirectory);
RunOsmConvert($"{Sources.OSM_FILE_NAME} {UPDATES_FILE_NAME}", workingDirectory);
}
/// <inheritdoc />
Expand Down
25 changes: 25 additions & 0 deletions IsraelHiking.Common/ConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ public class ConfigurationData
/// </summary>
public string BinariesFolder { get; set; }
/// <summary>
/// GraphHopper server address
/// </summary>
public string GraphhopperServerAddress { get; set; }
/// <summary>
/// Elasticsearch server address
/// </summary>
public string ElasticsearchServerAddress { get; set; }
/// <summary>
/// The address of the OSM file to download for daily rebuild
/// </summary>
public string OsmFileAddress { get; set; }
/// <summary>
/// The address of the OSM file time stamp to update the osm file regarding its modification day and time
/// </summary>
public string OsmFileTimeStampAddress { get; set; }
/// <summary>
/// The base address to fetch minutes updates for OSM. used by osm-c-tools
/// </summary>
public string OsmMinutsFileBaseAddress { get; set; }
/// <summary>
/// An object that describe how to connect to OSM
/// </summary>
public OsmConfiguraionData OsmConfiguration { get; set; }
Expand Down Expand Up @@ -118,6 +138,11 @@ public ConfigurationData()
SearchFactor = 0.5;
MergePointsOfInterestThreshold = 1 / 60.0; // 1 minute
BinariesFolder = string.Empty;
GraphhopperServerAddress = "http://localhost:8989/";
ElasticsearchServerAddress = "http://localhost:9200/";
OsmFileAddress = "http://download.openstreetmap.fr/extracts/asia/israel_and_palestine-latest.osm.pbf";
OsmFileTimeStampAddress = "http://download.openstreetmap.fr/extracts/asia/israel_and_palestine.state.txt";
OsmMinutsFileBaseAddress = "http://download.openstreetmap.fr/replication/asia/israel_and_palestine";
OsmConfiguration = new OsmConfiguraionData
{
ConsumerKey = "E8p0RX0rnQPxDaj3IijgpMNeK8lRTyy6rlKxQ8IF",
Expand Down
8 changes: 6 additions & 2 deletions IsraelHiking.DataAccess/ElasticSearchGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using IsraelHiking.Common.Extensions;
using IsraelHiking.DataAccessInterfaces;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
Expand Down Expand Up @@ -48,16 +49,19 @@ public class ElasticSearchGateway : IElasticSearchGateway
private const int NUMBER_OF_RESULTS = 10;
private readonly ILogger _logger;
private readonly GeometryFactory _geometryFactory;
private readonly ConfigurationData _options;
private IElasticClient _elasticClient;

public ElasticSearchGateway(ILogger logger, GeometryFactory geometryFactory)
public ElasticSearchGateway(IOptions<ConfigurationData> options, ILogger logger, GeometryFactory geometryFactory)
{
_options = options.Value;
_logger = logger;
_geometryFactory = geometryFactory;
}

public void Initialize(string uri = "http://localhost:9200/")
public void Initialize()
{
var uri = _options.ElasticsearchServerAddress;
_logger.LogInformation("Initialing elastic search with uri: " + uri);
var pool = new SingleNodeConnectionPool(new Uri(uri));
var connectionString = new ConnectionSettings(
Expand Down
12 changes: 6 additions & 6 deletions IsraelHiking.DataAccess/GraphHopperGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using IsraelHiking.Common;
using IsraelHiking.DataAccessInterfaces;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetTopologySuite.Geometries;
using Newtonsoft.Json;
using System;
Expand All @@ -14,15 +15,14 @@ namespace IsraelHiking.DataAccess
{
public class GraphHopperGateway : IGraphHopperGateway
{
private const string BASE_ADDRESS = "http://localhost:8989/";

private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
private readonly ConfigurationData _options;


public GraphHopperGateway(IHttpClientFactory httpClientFactory, ILogger logger)
public GraphHopperGateway(IHttpClientFactory httpClientFactory, IOptions<ConfigurationData> options, ILogger logger)
{
_httpClientFactory = httpClientFactory;
_options = options.Value;
_logger = logger;
}

Expand All @@ -42,7 +42,7 @@ public async Task<LineString> GetRouting(RoutingGatewayRequest request)
vehicle = "car4wd";
break;
}
var requestAddress = $"{BASE_ADDRESS}route?instructions=false&points_encoded=false&elevation=true&point={request.From}&point={request.To}&vehicle={vehicle}";
var requestAddress = $"{_options.GraphhopperServerAddress}route?instructions=false&points_encoded=false&elevation=true&point={request.From}&point={request.To}&vehicle={vehicle}";
var response = await httpClient.GetAsync(requestAddress);
var content = await response.Content.ReadAsStringAsync();
var jsonResponse = JsonConvert.DeserializeObject<JsonGraphHopperResponse>(content);
Expand All @@ -64,7 +64,7 @@ public async Task Rebuild(MemoryStream osmFileStream)
_logger.LogInformation($"Starting creating graph hopper cache based on latest pbf file: {Sources.OSM_FILE_NAME}");
var httpClient = _httpClientFactory.CreateClient();
httpClient.Timeout = TimeSpan.FromMinutes(30);
var requestAddress = $"{BASE_ADDRESS}rebuild";
var requestAddress = $"{_options.GraphhopperServerAddress}rebuild";
ByteArrayContent bytes = new ByteArrayContent(osmFileStream.ToArray());
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Add(bytes, "file", Sources.OSM_FILE_NAME);
Expand Down
2 changes: 1 addition & 1 deletion IsraelHiking.DataAccessInterfaces/IElasticSearchGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace IsraelHiking.DataAccessInterfaces
{
public interface IElasticSearchGateway : IRepository
{
void Initialize(string uri = "http://localhost:9200/");
void Initialize();
Task<List<Feature>> Search(string searchTerm, string language);
Task<List<Feature>> SearchPlaces(string place, string language);
Task<List<Feature>> SearchByLocation(Coordinate nortEast, Coordinate southWest, string searchTerm, string language);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using GeoAPI.Geometries;
using IsraelHiking.Common;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetTopologySuite.Geometries;
using NSubstitute;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -15,7 +17,9 @@ public class ElasticSearchGatewayTests
[TestInitialize]
public void TestInitialize()
{
_gateway = new ElasticSearchGateway(new TraceLogger(), new GeometryFactory());
var options = Substitute.For<IOptions<ConfigurationData>>();
options.Value.Returns(new ConfigurationData());
_gateway = new ElasticSearchGateway(options, new TraceLogger(), new GeometryFactory());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.FileProviders;
using IsraelHiking.Common;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using System.IO;
Expand All @@ -14,10 +16,10 @@ public class GraphHopperHelperTests
public void Initialize_ShouldAddService()
{
var logger = new TraceLogger();
var physical = new PhysicalFileProvider(@"D:\Github\IsraelHikingMap\Site\IsraelHiking.Web\bin\Debug\netcoreapp1.1");
var physical = new PhysicalFileProvider(@"D:\Github\IsraelHikingMap\Site\IsraelHiking.Web\bin\Debug\netcoreapp3.1");
var factory = Substitute.For<IHttpClientFactory>();
factory.CreateClient().Returns(new HttpClient());
var gateway = new GraphHopperGateway(factory, logger);
var gateway = new GraphHopperGateway(factory, Substitute.For<IOptions<ConfigurationData>>(), logger);
var memoryStream = new MemoryStream();
physical.GetFileInfo("israel-and-palestine-latest.osm.pbf").CreateReadStream().CopyTo(memoryStream);
gateway.Rebuild(memoryStream).Wait();
Expand Down

0 comments on commit 2d0c3f8

Please sign in to comment.