forked from ServiceStackApps/redis-geo
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathAppHost.cs
79 lines (70 loc) · 2.86 KB
/
AppHost.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections.Generic;
using System.IO;
using Funq;
using ServiceStack;
using RedisGeo.ServiceInterface;
using ServiceStack.Configuration;
using ServiceStack.Redis;
namespace RedisGeo
{
public class AppHost : AppHostBase
{
/// <summary>
/// Configure your ServiceStack AppHost singleton instance:
/// Call base constructor with App Name and assembly where Service classes are located
/// </summary>
public AppHost()
: base("RedisGeo", typeof(RedisGeoServices).Assembly) {}
public override void Configure(Container container)
{
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool(AppSettings.Get("REDIS_HOST", defaultValue:"localhost")));
ImportCountry(container.Resolve<IRedisClientsManager>(), "US");
}
public void ImportCountry(IRedisClientsManager redisManager, string countryCode)
{
using (var redis = redisManager.GetClient())
using (var reader = new StreamReader(File.OpenRead(MapProjectPath($"~/App_Data/{countryCode}.txt"))))
{
string line, lastState = null, lastCity = null;
var results = new List<ServiceStack.Redis.RedisGeo>();
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split('\t');
var city = parts[2];
var state = parts[4];
var latitude = double.Parse(parts[9]);
var longitude = double.Parse(parts[10]);
if (city == lastCity) //Skip duplicate entries
continue;
else
lastCity = city;
if (lastState == null)
lastState = state;
if (state != lastState)
{
redis.AddGeoMembers(lastState, results.ToArray());
lastState = state;
results.Clear();
}
results.Add(new ServiceStack.Redis.RedisGeo(longitude, latitude, city));
}
}
using (var redis = redisManager.GetClient())
using (var reader =
new StreamReader(File.OpenRead(MapProjectPath($"~/App_Data/{countryCode}_mapping.txt"))))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
if (line.Trim().Length == 0)
continue;
var stateInfo = line.Split("\t");
if(stateInfo.Length != 2)
continue;
redis.Set("mapping:" + stateInfo[1].Trim(), stateInfo[0].Trim());
}
}
}
}
}