Skip to content

Commit e2b8890

Browse files
authored
feat(IP2Region): add BootstrapBlazor.IP2Region project (#319)
* refactor: 增加 IP 地理位置库 * feat: 增加定位服务 * feat: 增加扩展方法 * feat: 增加自动化脚本 * feat: 增加配置类 * feat: 更新包配置信息 * chore: 增加 IP 定位包
1 parent a62bc03 commit e2b8890

7 files changed

Lines changed: 153 additions & 0 deletions

File tree

BootstrapBlazor.Extensions.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.SmilesDrawe
170170
EndProject
171171
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.OctIcon", "src\components\BootstrapBlazor.OctIcon\BootstrapBlazor.OctIcon.csproj", "{AA4EDA37-1D81-4235-A7F6-F1C112B364EF}"
172172
EndProject
173+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.IP2Region", "src\components\BootstrapBlazor.IP2Region\BootstrapBlazor.IP2Region.csproj", "{8A46FEDF-FD76-48AB-9BB2-47254C7623A4}"
174+
EndProject
173175
Global
174176
GlobalSection(SolutionConfigurationPlatforms) = preSolution
175177
Debug|Any CPU = Debug|Any CPU
@@ -456,6 +458,10 @@ Global
456458
{AA4EDA37-1D81-4235-A7F6-F1C112B364EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
457459
{AA4EDA37-1D81-4235-A7F6-F1C112B364EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
458460
{AA4EDA37-1D81-4235-A7F6-F1C112B364EF}.Release|Any CPU.Build.0 = Release|Any CPU
461+
{8A46FEDF-FD76-48AB-9BB2-47254C7623A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
462+
{8A46FEDF-FD76-48AB-9BB2-47254C7623A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
463+
{8A46FEDF-FD76-48AB-9BB2-47254C7623A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
464+
{8A46FEDF-FD76-48AB-9BB2-47254C7623A4}.Release|Any CPU.Build.0 = Release|Any CPU
459465
EndGlobalSection
460466
GlobalSection(SolutionProperties) = preSolution
461467
HideSolutionNode = FALSE
@@ -535,6 +541,7 @@ Global
535541
{7328E464-AE3C-4277-BEC3-422C56637066} = {FF1089BE-C704-4374-B629-C57C08E1798F}
536542
{84823875-1B07-4CCE-A009-29AEF90C6C10} = {FF1089BE-C704-4374-B629-C57C08E1798F}
537543
{AA4EDA37-1D81-4235-A7F6-F1C112B364EF} = {FF1089BE-C704-4374-B629-C57C08E1798F}
544+
{8A46FEDF-FD76-48AB-9BB2-47254C7623A4} = {FF1089BE-C704-4374-B629-C57C08E1798F}
538545
EndGlobalSection
539546
GlobalSection(ExtensibilityGlobals) = postSolution
540547
SolutionGuid = {D5EB1960-6F30-4CE1-B375-EAE1F787D6FF}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<Version>9.0.0</Version>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Ip Region</PackageTags>
9+
<Description>Bootstrap UI components extensions of IP2Region.NET</Description>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Content Include="Build\*.targets" PackagePath="build\" />
14+
<Content Include="Data\ip2region.xdb" PackagePath="contentFiles/ip2region.xdb" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
19+
<PackageReference Include="IP2Region.Net" Version="2.0.2" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project>
2+
3+
<ItemGroup>
4+
<Files Include="$(MSBuildThisFileDirectory)/../contentFiles/*.*"/>
5+
</ItemGroup>
6+
7+
<Target Name="CopyFiles" AfterTargets="Build">
8+
<Copy SourceFiles="@(Files)" DestinationFolder="$(TargetDir)/%(RecursiveDir)"/>
9+
</Target>
10+
11+
</Project>
10.6 MB
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using BootstrapBlazor.Components;
6+
7+
namespace Microsoft.Extensions.DependencyInjection;
8+
9+
/// <summary>
10+
/// BootstrapBlazor 服务扩展类
11+
/// </summary>
12+
public static class BootstrapBlazoIP2RegionExtensions
13+
{
14+
/// <summary>
15+
/// 添加 AzureOpenAIService 服务
16+
/// </summary>
17+
/// <param name="services"></param>
18+
public static IServiceCollection AddBootstrapBlazorIP2RegionfService(this IServiceCollection services)
19+
{
20+
services.AddSingleton<IIpLocatorProvider, IP2RegionService>();
21+
#if NET8_0_OR_GREATER
22+
services.AddKeyedSingleton<IIpLocatorProvider, IP2RegionService>("BootstrapBlazor.IP2Region");
23+
#endif
24+
25+
services.AddOptionsMonitor<IP2RegionOptions>();
26+
return services;
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// IP2Region 配置类
9+
/// </summary>
10+
public class IP2RegionOptions
11+
{
12+
/// <summary>
13+
/// 获取/设置 IP2Region 数据文件路径
14+
/// </summary>
15+
public string? XdbPath { get; set; }
16+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using IP2Region.Net.XDB;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace BootstrapBlazor.Components;
10+
11+
/// <summary>
12+
/// 默认 IP2Region 实现
13+
/// </summary>
14+
class IP2RegionService : DefaultIpLocatorProvider
15+
{
16+
/// <summary>
17+
/// <inheritdoc/>
18+
/// </summary>
19+
/// <param name="options"></param>
20+
/// <param name="ipRegionOptions"></param>
21+
/// <param name="logger"></param>
22+
public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2RegionOptions> ipRegionOptions, ILogger<IP2RegionService> logger) : base(options)
23+
{
24+
_options = options;
25+
_ipOptions = ipRegionOptions;
26+
_logger = logger;
27+
28+
InitSearch();
29+
}
30+
31+
private readonly IOptions<BootstrapBlazorOptions> _options;
32+
private readonly IOptions<IP2RegionOptions> _ipOptions;
33+
private readonly ILogger<IP2RegionService> _logger;
34+
private Searcher? _search;
35+
36+
/// <summary>
37+
/// <inheritdoc/>
38+
/// </summary>
39+
/// <param name="ip"></param>
40+
protected override Task<string?> LocateByIp(string ip)
41+
{
42+
string? result = null;
43+
if (_search != null && _options.Value.WebClientOptions.EnableIpLocator)
44+
{
45+
result = _search.Search(ip);
46+
}
47+
return Task.FromResult(result);
48+
}
49+
50+
private void InitSearch()
51+
{
52+
var xdbPath = _ipOptions.Value.XdbPath ?? Path.Combine(AppContext.BaseDirectory, "ip2region.xdb");
53+
if (!File.Exists(xdbPath))
54+
{
55+
_logger.LogWarning("IP2Region xdb file not found, please check the file path: {dbPath}", xdbPath);
56+
return;
57+
}
58+
59+
try
60+
{
61+
_search = new Searcher(CachePolicy.Content, xdbPath);
62+
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
63+
}
64+
catch (Exception ex)
65+
{
66+
_logger.LogError(ex, "IP2Region xdb file path: {dbPath}", xdbPath);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)