Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
boilerplate for identity service
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafcafe committed Apr 28, 2020
1 parent a4175a4 commit 9e1165a
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dotnet/Services/Identity/App/Application.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Branch.Services.Identity.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ServiceStack.Redis;

namespace Branch.Services.Identity.App
{
public partial class Application
{
private ILogger _logger { get; }
private IRedisClientsManager _redisClientsManager { get; }
private Config _config { get; }

public Application(
ILoggerFactory loggerFactory,
IRedisClientsManager redisClientsManager,
IOptionsMonitor<Config> options
)
{
_logger = loggerFactory.CreateLogger(typeof(Application));
_redisClientsManager = redisClientsManager;
_config = options.CurrentValue;
}
}
}
23 changes: 23 additions & 0 deletions dotnet/Services/Identity/App/GetXblIdentity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Branch.Services.Identity.App
{
public partial class Application
{
private readonly string XblRedisKey = "identity:xbox-live";

public async Task<GetXblIdentityRes> GetXblIdentity(HttpContext ctx, GetXblIdentityReq req)
{
var useCache = !req.IgnoreCache;

using (var client = _redisClientsManager.GetClient())
{
return new GetXblIdentityRes
{
CacheInfo = null,
};
}
}
}
}
18 changes: 18 additions & 0 deletions dotnet/Services/Identity/Models/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Branch.Services.Identity.Models
{
public class Config
{
public string[] InternalKeys { get; set; }

public string RedisConnectionString { get; set; }

public static Config CreateDefault()
{
return new Config
{
InternalKeys = new string[] {"test"},
RedisConnectionString = "redis://127.0.0.1:6379?db=1",
};
}
}
}
42 changes: 42 additions & 0 deletions dotnet/Services/Identity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# service-identity

BaseURL: https://service-identity.branch.golf/1

## Versions

- `preview`: Service created

## Api

### Index

- `get_xbl_identity`

### Methods

#### `get_xbl_identity`

##### Request

```json
{
"type": "gamertag",
"value": "PhoenixBanTrain"
}
```

The `type` key can be either `gamertag`, or `xuid`.

##### Response

```json
{
"cache_overview": {
"cached_at": "2020-04-26T23:00:02.168Z",
"expires_at": "2020-04-26T23:15:13.614Z"
},
"gamertag": "xxCoLLaTeRaLx",
"xuid": 2533274824126595
}
```

41 changes: 41 additions & 0 deletions dotnet/Services/Identity/Server/GetXblIdentity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Branch.Services.Identity.Server
{
public partial class RpcServer : IIdentityService
{
public readonly string GetXblIdentitySchema = @"
{
""type"": ""object"",
""additionalProperties"": false,
""required"": [
""type"",
""value""
],
""properties"": {
""ignore_cache"": {
""type"": ""boolean""
},
""type"": {
""type"": ""string"",
""enum"": [""gamertag"", ""xuid""]
},
""value"": {
""type"": ""string"",
""minLength"": 1
},
}
}
";

public Task<GetXblIdentityRes> GetXblIdentity(HttpContext ctx, GetXblIdentityReq req)
{
return _app.GetXblIdentity(ctx, req);
}
}
}
14 changes: 14 additions & 0 deletions dotnet/Services/Identity/Server/RpcServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Branch.Services.Identity.App;

namespace Branch.Services.Identity.Server
{
public partial class RpcServer : IIdentityService
{
private Application _app;

public RpcServer(Application app)
{
_app = app;
}
}
}
29 changes: 29 additions & 0 deletions dotnet/Services/Identity/Service.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;
using Branch.Global.Contracts;
using Microsoft.AspNetCore.Http;

namespace Branch.Services.Identity
{
public interface IIdentityService
{
Task<GetXblIdentityRes> GetXblIdentity(HttpContext ctx, GetXblIdentityReq req);
}

public class GetXblIdentityReq
{
public bool IgnoreCache { get; set; }

public string Type { get; set; }

public string Value { get; set; }
}

public class GetXblIdentityRes : IBranchResponse
{
public CacheInfo CacheInfo { get; set; }

public string Gamertag { get; set; }

public string Xuid { get; set; }
}
}
58 changes: 58 additions & 0 deletions dotnet/Services/Identity/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Threading.Tasks;
using Branch.Global.Attributes;
using Branch.Services.Identity.App;
using Branch.Services.Identity.Models;
using Branch.Services.Identity.Server;
using Crpc;
using Crpc.Registration;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ServiceStack.Redis;

namespace Branch.Services.Identity
{
public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.Configure<Config>(Configuration);

var redisConnectionString = Configuration.GetSection("RedisConnectionString").Get<string>();

services.AddSingleton<IRedisClientsManager>(new BasicRedisClientManager(redisConnectionString));

services.AddCrpc<Application, RpcServer>(opts =>
{
opts.InternalKeys = Configuration.GetSection("InternalKeys").Get<string[]>();
});
}

public void Configure(IApplicationBuilder app)
{
app.UseCrpcHealthCheck();
app.UseCrpc<RpcServer>("/1", (c, s) => {
c.Authentication = AuthenticationType.AllowInternalAuthentication;

c.Register<GetXblIdentityReq, GetXblIdentityRes>(
"get_xbl_identity", "preview", s.GetXblIdentity, s.GetXblIdentitySchema
);
});
}

[ServiceEntry("service-identity")]
public static async Task Entry(string[] args) =>
await CrpcHost.CreateCrpcHost<Startup>(Config.CreateDefault())
.UseConsoleLifetime()
.Build()
.RunAsync();
}
}

0 comments on commit 9e1165a

Please sign in to comment.