Skip to content

Commit

Permalink
add s3 url
Browse files Browse the repository at this point in the history
  • Loading branch information
hoseinzadehashraf committed Feb 15, 2024
1 parent b85d363 commit c546577
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
38 changes: 23 additions & 15 deletions Olive.Microservices/Microservice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Olive
{
Expand All @@ -8,18 +10,27 @@ namespace Olive
public class Microservice
{
public string Name { get; private set; }
string BaseUrl, BaseResourceUrl, AccessKey;
string BaseUrl, BaseResourceUrl, BaseS3BucketUrl, AccessKey;

Microservice(string name) => Name = name;
Microservice(string name)
{
Name = name;

BaseUrl = Config.GetOrThrow("Microservice:" + name + ":Url").EnsureEndsWith("/");

var isDevelopment = Context.Current.ServiceProvider.GetRequiredService<IHostEnvironment>().IsDevelopment();
BaseResourceUrl = isDevelopment
? BaseUrl
: Config.Get("Authentication:Cookie:Domain").EnsureStartsWith("https://").EnsureEndsWith("/") + name.ToLower() + "/";

BaseS3BucketUrl =$"https://{Config.Get("Blob:S3:Bucket")}.s3.{Config.Get("Blob:S3:Region")}.amazonaws.com/"; ;

AccessKey = Config.Get("Microservice:" + name + ":AccessKey");
}

public static Microservice Of(string serviceName)
{
return new Microservice(serviceName)
{
BaseUrl = Config.GetOrThrow("Microservice:" + serviceName + ":Url").EnsureEndsWith("/"),
BaseResourceUrl = Config.Get("Authentication:Cookie:Domain").EnsureStartsWith("https://").EnsureEndsWith("/")+serviceName.ToLower()+"/",
AccessKey = Config.Get("Microservice:" + serviceName + ":AccessKey")
};
return new Microservice(serviceName);
}

public static Microservice Me
Expand All @@ -31,20 +42,17 @@ public static Microservice Me

var name = Config.GetOrThrow("Microservice:Me:Name");

return new Microservice(name)
{
BaseUrl = url,
BaseResourceUrl = Config.Get("Authentication:Cookie:Domain").EnsureStartsWith("https://").EnsureEndsWith("/")+name.ToLower()+"/"
};
return new Microservice(name);
}
}

/// <summary>
/// Returns the full url to a specified resource in this microservice by
/// concatinating the base url of this service with the specified relative url.
/// </summary>
public string Url(string relativeUrl = null) => relativeUrl?.Contains("://")==true ? relativeUrl : BaseUrl + relativeUrl.OrEmpty().TrimStart("/");
public string GetResourceUrl(string relativeUrl = null) => relativeUrl?.Contains("://")==true ? relativeUrl : BaseResourceUrl + relativeUrl.OrEmpty().TrimStart("/");
public string Url(string relativeUrl = null) => relativeUrl?.Contains("://") == true ? relativeUrl : BaseUrl + relativeUrl.OrEmpty().TrimStart("/");
public string GetResourceUrl(string relativeUrl = null) => relativeUrl?.Contains("://") == true ? relativeUrl : BaseResourceUrl + relativeUrl.OrEmpty().TrimStart("/");
public string GetS3BucketUrl(string relativeUrl = null) => relativeUrl?.Contains("://") == true ? relativeUrl : BaseS3BucketUrl + relativeUrl.OrEmpty().TrimStart("/");

/// <summary>
/// Creates an Api client for this service.
Expand Down
3 changes: 2 additions & 1 deletion Olive.Microservices/Olive.Microservices.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.116</Version>
<Version>2.1.117</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
</ItemGroup>
Expand Down

0 comments on commit c546577

Please sign in to comment.