词典代码

master
张宇 2023-12-10 21:53:47 +08:00
parent 9d30df3b42
commit 5f3bbb36ab
13 changed files with 165 additions and 12 deletions

View File

@ -0,0 +1,12 @@
# Dockerfile
[b|B]in
[O|o]bj
.dockerignore
.env
.git
.gitignore
.vs
.vscode
*/bin
*/obj
**/.toolstarget

View File

@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApi.Data;
namespace MyApp.Namespace;
[Route("[controller]")]
[ApiController]
public class DictionaryController(DictionaryDbContext dictionaryDbContext) : ControllerBase
{
private readonly DictionaryDbContext _dictionaryDbContext = dictionaryDbContext;
[HttpGet]
public async Task<IActionResult> GetWordDetailAsync(string word)
{
var result = _dictionaryDbContext.Dicts.Find(word);
await Task.Delay(10);
return Ok(result);
}
}

View File

@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Namespace
{
[Route("[controller]")]
[ApiController]
public class TodoController : ControllerBase
{
}
}

View File

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace WebApi.Data;
public class ApplicationIdentityDbContext(DbContextOptions options) : IdentityDbContext<IdentityUser>(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source = Data/DB/Identity");
}
}

Binary file not shown.

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using WebApi.Models;
namespace WebApi.Data;
public partial class DictionaryDbContext : DbContext
{
public DictionaryDbContext()
{
}
public DictionaryDbContext(DbContextOptions<DictionaryDbContext> options)
: base(options)
{
}
public virtual DbSet<Dict> Dicts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Name=ConnectionStrings:Dictionary");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Dict>(entity =>
{
entity.Property(e => e.Word).UseCollation("NOCASE");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

11
WebApi/Dockerfile 100644
View File

@ -0,0 +1,11 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source
COPY WebApi.csproj .
RUN dotnet restore WebApi.csproj
COPY . .
RUN dotnet publish -c release -o /app
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "WebApi.dll"]

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WebApi.Models;
[Table("Dict")]
[Index("Word", Name = "wordIndex")]
public partial class Dict
{
[Key]
[Column("word")]
public string Word { get; set; } = null!;
[Column("autoSugg")]
public string? AutoSugg { get; set; }
public string Defi { get; set; } = null!;
}

View File

@ -1,3 +1,8 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using WebApi.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@ -7,6 +12,16 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// builder.Services.AddIdentityApiEndpoints<IdentityUser>();
// builder.Services.AddIdentity<IdentityUser,IdentityRole>();
builder.Services.AddDbContext<ApplicationIdentityDbContext>(options => options.UseSqlite());
builder.Services.AddDbContext<DictionaryDbContext>();
builder.Services.AddAuthorization();
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddEntityFrameworkStores<ApplicationIdentityDbContext>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -16,7 +31,9 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// app.UseHttpsRedirection();
app.MapIdentityApi<IdentityUser>();
app.UseAuthorization();

View File

@ -19,16 +19,6 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7275;http://localhost:5074",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,

View File

@ -8,6 +8,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ardalis.Specification" Version="8.0.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"Dictionary": "Data Source = Data/DB/offline_enhanceV2.db;Mode=ReadOnly",
"Identity": "Data Source = Data/DB/Identity.db;"
}
}

View File

@ -0,0 +1,8 @@
services:
api:
build:
context: ./WebApi
dockerfile: Dockerfile
network: host
ports:
- 8080:8080