diff --git a/Docker/docker-compose.override.yml b/Docker/docker-compose.override.yml index 0356880..289a3a2 100644 --- a/Docker/docker-compose.override.yml +++ b/Docker/docker-compose.override.yml @@ -4,5 +4,9 @@ services: qrcodeservice: environment: - ASPNETCORE_ENVIRONMENT=Development + - ConnectionStrings__Default=server=mariadb;user=root;password=root;database=qrcode ports: - - "80" + - 5001:80 + mariadb: + environment: + - MYSQL_ROOT_PASSWORD=root \ No newline at end of file diff --git a/Docker/docker-compose.yml b/Docker/docker-compose.yml index 82fdc94..35f6713 100644 --- a/Docker/docker-compose.yml +++ b/Docker/docker-compose.yml @@ -5,4 +5,7 @@ services: image: ${DOCKER_REGISTRY-}qrcodeservice build: context: . - dockerfile: QRCodeService/Dockerfile + dockerfile: ../QRCodeService/Dockerfile + mariadb: + image: mariadb:10.5.9 + \ No newline at end of file diff --git a/QRCodeService/Application/Queries/AppQueries.cs b/QRCodeService/Application/Queries/AppQueries.cs index 1db9f87..f876509 100644 --- a/QRCodeService/Application/Queries/AppQueries.cs +++ b/QRCodeService/Application/Queries/AppQueries.cs @@ -13,9 +13,9 @@ namespace QRCodeService.Application.Queries { readonly string _connectionString; - public AppQueries(IConfiguration configuration) + public AppQueries(string connectionString) { - _connectionString = configuration.GetConnectionString("default"); + _connectionString = connectionString; } public async Task GetAppAsync(int id) diff --git a/QRCodeService/Application/Queries/LinkQueries.cs b/QRCodeService/Application/Queries/LinkQueries.cs index ffd2f77..1020814 100644 --- a/QRCodeService/Application/Queries/LinkQueries.cs +++ b/QRCodeService/Application/Queries/LinkQueries.cs @@ -14,9 +14,9 @@ namespace QRCodeService.Application.Queries { readonly string _connectionString; - public LinkQueries(IConfiguration configuration) + public LinkQueries(string connectionString) { - _connectionString = configuration.GetConnectionString("default"); + _connectionString = connectionString; } public async Task GetLinkAsync(string shortCode) diff --git a/QRCodeService/Controllers/ImageController.cs b/QRCodeService/Controllers/ImageController.cs index 1bf3b78..4920173 100644 --- a/QRCodeService/Controllers/ImageController.cs +++ b/QRCodeService/Controllers/ImageController.cs @@ -8,6 +8,7 @@ using QRCoder; using System.IO; using QRCodeService.Application.Queries; using QRCodeService.Models; +using QRCodeService.Options; namespace QRCodeService.Controllers { @@ -17,11 +18,12 @@ namespace QRCodeService.Controllers { readonly ILinkQueries linkQueries; readonly IAppQueries appQueries; - - public ImageController(ILinkQueries linkQueries, IAppQueries appQueries) + readonly ServiceOption option; + public ImageController(ILinkQueries linkQueries, IAppQueries appQueries,ServiceOption option) { this.linkQueries = linkQueries; this.appQueries = appQueries; + this.option = option; } [Route("qrcode")] @@ -43,7 +45,7 @@ namespace QRCodeService.Controllers return BadRequest(); } var qrCodeGenerator = new QRCodeGenerator(); - var data = qrCodeGenerator.CreateQrCode($"http://localhost:5000/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q); + var data = qrCodeGenerator.CreateQrCode($"{option.BaseUrl}r/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q); var qrCode = new QRCode(data); using (var stream = new MemoryStream()) { diff --git a/QRCodeService/Controllers/RedirectController.cs b/QRCodeService/Controllers/RedirectController.cs index aabac85..296a319 100644 --- a/QRCodeService/Controllers/RedirectController.cs +++ b/QRCodeService/Controllers/RedirectController.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace QRCodeService.Controllers { - [Route("{shortCode}")] + [Route("r/{shortCode}")] public class RedirectController:Controller { private readonly ILinkQueries queries; diff --git a/QRCodeService/Options/ServiceOption.cs b/QRCodeService/Options/ServiceOption.cs new file mode 100644 index 0000000..1003404 --- /dev/null +++ b/QRCodeService/Options/ServiceOption.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace QRCodeService.Options +{ + public class ServiceOption + { + /// + /// 二维码输出地址的基本路径 + /// + public string BaseUrl { get; set; } + /// + /// 是否布置了反向代理 + /// + public bool IsForwarded { get; set; } + } +} diff --git a/QRCodeService/Startup.cs b/QRCodeService/Startup.cs index 03e8c55..1ad8655 100644 --- a/QRCodeService/Startup.cs +++ b/QRCodeService/Startup.cs @@ -22,6 +22,7 @@ using QRCodeService.Application.Behaviors; using QRCodeService.Application.Commands; using QRCodeService.Application.Queries; using QRCodeService.Application.Validations; +using QRCodeService.Options; using System; using System.Collections.Generic; using System.Linq; @@ -48,6 +49,9 @@ namespace QRCodeService { c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" }); }); + // + services.Configure(Configuration.GetSection("hosting")); + //validator services.AddTransient, CreateLinkCommandValidator>(); @@ -60,7 +64,7 @@ namespace QRCodeService services.AddDbContext( dbContextOptions => dbContextOptions .UseMySql( - "server=localhost;user=root;password=root;database=qrcode", + Configuration.GetConnectionString("Default"), // For common usages, see pull request #1233. new MariaDbServerVersion(new Version(10, 5, 9)), // use MariaDbServerVersion for MariaDB mySqlOptions => mySqlOptions @@ -74,8 +78,8 @@ namespace QRCodeService services.AddScoped(); services.AddScoped(); //Queries - services.AddScoped(); - services.AddScoped(); + services.AddScoped( s=>new LinkQueries(Configuration.GetConnectionString("Default"))); + services.AddScoped(s => new AppQueries(Configuration.GetConnectionString("Default"))); //redis cache services.AddDistributedRedisCache(options=>options.Configuration=Configuration.GetSection("redis:default").Value); @@ -88,7 +92,7 @@ namespace QRCodeService } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ServiceOption option) { if (env.IsDevelopment()) { @@ -96,7 +100,10 @@ namespace QRCodeService app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QRCodeService v1")); } - app.UseForwardedHeaders(); + if (option.IsForwarded) + { + app.UseForwardedHeaders(); + } app.UseRouting(); diff --git a/QRCodeService/appsettings.Development.json b/QRCodeService/appsettings.Development.json index 8983e0f..4f30a00 100644 --- a/QRCodeService/appsettings.Development.json +++ b/QRCodeService/appsettings.Development.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/QRCodeService/appsettings.Production.json b/QRCodeService/appsettings.Production.json new file mode 100644 index 0000000..b84fdfc --- /dev/null +++ b/QRCodeService/appsettings.Production.json @@ -0,0 +1,6 @@ +{ + "Hosting": { + "BaseUrl": "http://qrcode.bobandjuly.cyou/", + "IsForwarded": true + } +} \ No newline at end of file diff --git a/QRCodeService/appsettings.json b/QRCodeService/appsettings.json index d2edee1..ed482d5 100644 --- a/QRCodeService/appsettings.json +++ b/QRCodeService/appsettings.json @@ -8,7 +8,7 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "default": "server=localhost;user=root;password=root;database=qrcode" + "Default": "server=localhost;user=root;password=root;database=qrcode" }, "Serilog": { "Using": [ "SeriLog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async" ], @@ -49,5 +49,9 @@ "InstanceName": "local", "DefaultDB": 8 } + }, + "Hosting": { + "BaseUrl": "http://localhost:5000/", + "IsForwarded": false } }