mirror of
https://github.com/opelly27/WinStudentGoalTracker.git
synced 2026-05-20 02:57:36 +00:00
some changes
This commit is contained in:
+45
-1
@@ -1,11 +1,30 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
using DotNetEnv;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using WinStudentGoalTracker.Api.Configuration;
|
using WinStudentGoalTracker.Api.Configuration;
|
||||||
using WinStudentGoalTracker.Services;
|
using WinStudentGoalTracker.Services;
|
||||||
|
|
||||||
|
Env.TraversePath().Load();
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Build connection string from .env variables
|
||||||
|
var dbServer = Environment.GetEnvironmentVariable("MYSQL_HOST") ?? "localhost";
|
||||||
|
var dbPort = Environment.GetEnvironmentVariable("MYSQL_PORT") ?? "3309";
|
||||||
|
var dbName = Environment.GetEnvironmentVariable("MYSQL_DATABASE") ?? "winstudentgoaltracker";
|
||||||
|
var dbUser = Environment.GetEnvironmentVariable("MYSQL_USER") ?? "root";
|
||||||
|
var dbPassword = Environment.GetEnvironmentVariable("MYSQL_PASSWORD") ?? "";
|
||||||
|
builder.Configuration["ConnectionStrings:DefaultConnection"] =
|
||||||
|
$"Server={dbServer};Port={dbPort};Database={dbName};Uid={dbUser};Pwd={dbPassword};";
|
||||||
|
|
||||||
|
// Override JWT key from .env if present
|
||||||
|
var envJwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
|
||||||
|
if (!string.IsNullOrEmpty(envJwtKey))
|
||||||
|
builder.Configuration["Jwt:Key"] = envJwtKey;
|
||||||
|
|
||||||
|
Console.WriteLine($"Built connection string from .env: {builder.Configuration["ConnectionStrings:DefaultConnection"]}");
|
||||||
|
|
||||||
ConfigHelper.Configuration = builder.Configuration;
|
ConfigHelper.Configuration = builder.Configuration;
|
||||||
|
|
||||||
var jwtKey = builder.Configuration["Jwt:Key"] ?? "super_secret_key_change_me_in_production_123!";
|
var jwtKey = builder.Configuration["Jwt:Key"] ?? "super_secret_key_change_me_in_production_123!";
|
||||||
@@ -49,7 +68,32 @@ builder.Services.AddHttpClient<OllamaService>(client =>
|
|||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen(c =>
|
||||||
|
{
|
||||||
|
c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Name = "Authorization",
|
||||||
|
Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http,
|
||||||
|
Scheme = "bearer",
|
||||||
|
BearerFormat = "JWT",
|
||||||
|
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
||||||
|
Description = "Enter your JWT token (without 'Bearer ' prefix)."
|
||||||
|
});
|
||||||
|
c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
||||||
|
{
|
||||||
|
Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
||||||
|
Id = "Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Array.Empty<string>()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
{
|
{
|
||||||
options.AddDefaultPolicy(policy =>
|
options.AddDefaultPolicy(policy =>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
|
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
||||||
<PackageReference Include="MySql.Data" Version="8.4.0" />
|
<PackageReference Include="MySql.Data" Version="8.4.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
|
||||||
"DefaultConnection": "Server=localhost;Port=3306;Database=winstudentgoaltracker;Uid=root;Pwd=change_me;"
|
|
||||||
},
|
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=localhost;Port=3306;Database=winstudentgoaltracker;Uid=root;Pwd=change_me;"
|
"DefaultConnection": "Set via MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD in .env"
|
||||||
},
|
},
|
||||||
"Jwt": {
|
"Jwt": {
|
||||||
"Key": "super_secret_key_change_me_in_production_123!",
|
"Key": "Set via JWT_KEY in .env",
|
||||||
"Issuer": "WinStudentGoalTrackerAPI"
|
"Issuer": "WinStudentGoalTrackerAPI"
|
||||||
},
|
},
|
||||||
"Ollama": {
|
"Ollama": {
|
||||||
|
|||||||
@@ -19,6 +19,14 @@ public class StudentController : BaseController
|
|||||||
[ProducesResponseType(typeof(ResponseResult<IEnumerable<StudentResponse>>), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(ResponseResult<IEnumerable<StudentResponse>>), StatusCodes.Status200OK)]
|
||||||
public async Task<ActionResult<ResponseResult<IEnumerable<StudentResponse>>>> GetAll()
|
public async Task<ActionResult<ResponseResult<IEnumerable<StudentResponse>>>> GetAll()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
var (userId, email, programId, role, error) = GetProgramUserFromClaims();
|
||||||
|
|
||||||
|
if (error is not null)
|
||||||
|
{
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
var students = await _studentRepository.GetAllAsync();
|
var students = await _studentRepository.GetAllAsync();
|
||||||
var response = students.Select(StudentResponse.FromDatabaseModel);
|
var response = students.Select(StudentResponse.FromDatabaseModel);
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class AuthRepository
|
|||||||
string? userAgent)
|
string? userAgent)
|
||||||
{
|
{
|
||||||
using var db = Connection;
|
using var db = Connection;
|
||||||
var result = await db.QuerySingleOrDefaultAsync<string?>(
|
var result = await db.QuerySingleOrDefaultAsync<Guid?>(
|
||||||
"sp_RefreshToken_Create",
|
"sp_RefreshToken_Create",
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
@@ -33,7 +33,7 @@ public class AuthRepository
|
|||||||
p_user_agent = userAgent
|
p_user_agent = userAgent
|
||||||
},
|
},
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
return result != null ? Guid.Parse(result) : null;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<dbRefreshToken?> GetRefreshTokenByIdAsync(Guid refreshTokenId)
|
public async Task<dbRefreshToken?> GetRefreshTokenByIdAsync(Guid refreshTokenId)
|
||||||
|
|||||||
Reference in New Issue
Block a user