Skip to content

Commit c771b59

Browse files
Add initial implementation of Sparkly Server with authentication, user management, and Docker configuration.
0 parents  commit c771b59

27 files changed

+698
-0
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.dockerignore
2+
**/.env
3+
**/.git
4+
**/.gitignore
5+
**/.project
6+
**/.settings
7+
**/.toolstarget
8+
**/.vs
9+
**/.vscode
10+
**/.idea
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin/
2+
obj/
3+
/packages/
4+
riderModule.iml
5+
/_ReSharper.Caches/
6+
.idea
7+
.env

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
2+
USER $APP_UID
3+
WORKDIR /app
4+
EXPOSE 8080
5+
6+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
7+
ARG BUILD_CONFIGURATION=Release
8+
WORKDIR /src
9+
COPY ["sparkly-server.csproj", "./"]
10+
RUN dotnet restore "sparkly-server.csproj"
11+
COPY . .
12+
WORKDIR "/src/"
13+
RUN dotnet build "./sparkly-server.csproj" -c $BUILD_CONFIGURATION -o /app/build
14+
15+
FROM build AS publish
16+
ARG BUILD_CONFIGURATION=Release
17+
RUN dotnet publish "./sparkly-server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
18+
19+
FROM base AS final
20+
WORKDIR /app
21+
COPY --from=publish /app/publish .
22+
ENTRYPOINT ["dotnet", "sparkly-server.dll"]

Program.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.IdentityModel.Tokens;
3+
using sparkly_server.Enum;
4+
using sparkly_server.Services.Auth;
5+
using sparkly_server.Services.Users;
6+
using sparkly_server.Services.UserServices;
7+
using System.Text;
8+
9+
namespace sparkly_server;
10+
11+
public class Program
12+
{
13+
public static void Main(string[] args)
14+
{
15+
16+
var jwtKey = Environment.GetEnvironmentVariable("SPARKLY_JWT_KEY")!;
17+
var jwtIssuer = Environment.GetEnvironmentVariable("SPARKLY_JWT_ISSUER") ?? "sparkly";
18+
var jwtAudience = Environment.GetEnvironmentVariable("SPARKLY_JWT_AUDIENCE") ?? "sparkly-api";
19+
20+
var builder = WebApplication.CreateBuilder(args);
21+
22+
builder.Services.AddHttpContextAccessor();
23+
24+
builder.Services.AddAuthorization(options =>
25+
{
26+
options.AddPolicy("AdminOnly", policy =>
27+
policy.RequireRole(Roles.Admin));
28+
});
29+
30+
builder.Services.AddScoped<IUserRepository, UserRepository>();
31+
builder.Services.AddScoped<IUserService, UserService>();
32+
builder.Services.AddScoped<IJwtProvider, JwtProvider>();
33+
builder.Services.AddScoped<IAuthService, AuthService>();
34+
builder.Services.AddScoped<ICurrentUser, CurrentUser>();
35+
36+
builder.Services.AddControllers();
37+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
38+
builder.Services.AddOpenApi();
39+
40+
builder.Services
41+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
42+
.AddJwtBearer(options =>
43+
{
44+
var key = builder.Configuration["SPARKLY_JWT_KEY"]
45+
?? throw new Exception("JWT key missing");
46+
47+
options.TokenValidationParameters = new TokenValidationParameters
48+
{
49+
ValidateIssuer = false,
50+
ValidateAudience = false,
51+
ValidateLifetime = true,
52+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)),
53+
ValidateIssuerSigningKey = true,
54+
ClockSkew = TimeSpan.FromMinutes(1),
55+
};
56+
});
57+
58+
var app = builder.Build();
59+
60+
// Configure the HTTP request pipeline.
61+
if (app.Environment.IsDevelopment())
62+
{
63+
app.MapOpenApi();
64+
}
65+
66+
app.UseHttpsRedirection();
67+
68+
app.UseAuthentication();
69+
app.UseAuthorization();
70+
71+
app.MapControllers();
72+
73+
app.Run();
74+
}
75+
}

appsettings.Development.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

compose.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
services:
2+
api:
3+
build: .
4+
environment:
5+
ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT:-Development}
6+
ASPNETCORE_URLS: ${ASPNETCORE_URLS:-http://+:8080}
7+
TRILY_JWT_KEY: ${SPARKLY_JWT_KEY}
8+
ConnectionStrings__Default: "Host=pg;Port=5432;Database=sparkly;Username=sparkly;Password=${POSTGRES_PASSWORD}"
9+
ports:
10+
- "${API_PORT:-8080}:8080"
11+
depends_on:
12+
pg:
13+
condition: service_healthy
14+
redis:
15+
condition: service_healthy
16+
pg:
17+
image: postgres:16
18+
environment:
19+
POSTGRES_USER: ${POSTGRES_USER}
20+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
21+
POSTGRES_DB: ${POSTGRES_USER}
22+
healthcheck:
23+
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
24+
interval: 5s
25+
timeout: 3s
26+
retries: 10
27+
redis:
28+
image: redis:7
29+
healthcheck:
30+
test: ["CMD", "redis-cli", "ping"]
31+
interval: 5s
32+
timeout: 3s
33+
retries: 10

global.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.0",
4+
"rollForward": "latestMinor",
5+
"allowPrerelease": false
6+
}
7+
}

sparkly-server.csproj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>sparkly_server</RootNamespace>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.11" />
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.11">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
23+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<None Remove="Properties\launchSettings.json" />
28+
</ItemGroup>
29+
30+
</Project>

sparkly-server.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sparkly-server", "sparkly-server.csproj", "{B04A64D4-FEE1-4614-8BCD-B3B4C5B8E20A}"
4+
EndProject
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{77183F25-7A28-4F02-A395-89576DB3B36A}"
6+
ProjectSection(SolutionItems) = preProject
7+
compose.yaml = compose.yaml
8+
EndProjectSection
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B04A64D4-FEE1-4614-8BCD-B3B4C5B8E20A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B04A64D4-FEE1-4614-8BCD-B3B4C5B8E20A}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B04A64D4-FEE1-4614-8BCD-B3B4C5B8E20A}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B04A64D4-FEE1-4614-8BCD-B3B4C5B8E20A}.Release|Any CPU.Build.0 = Release|Any CPU
20+
EndGlobalSection
21+
EndGlobal

0 commit comments

Comments
 (0)