Startup.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Masuit.LuceneEFCore.SearchEngine;
  2. using Masuit.LuceneEFCore.SearchEngine.Extensions;
  3. using Masuit.LuceneEFCore.SearchEngine.Interfaces;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.OpenApi.Models;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using WebSearchDemo.Database;
  16. namespace WebSearchDemo
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. }
  24. public IConfiguration Configuration { get; }
  25. // This method gets called by the runtime. Use this method to add services to the container.
  26. public void ConfigureServices(IServiceCollection services)
  27. {
  28. services.AddDbContext<DataContext>(db =>
  29. {
  30. db.UseInMemoryDatabase("test");
  31. //db.UseSqlServer("Data Source=.;Initial Catalog=MyBlogs;Integrated Security=True");
  32. });
  33. services.AddSearchEngine<DataContext>(new LuceneIndexerOptions()
  34. {
  35. Path = "lucene"
  36. });
  37. services.AddSwaggerGen(c =>
  38. {
  39. c.SwaggerDoc("v1", new OpenApiInfo
  40. {
  41. Version = "v1",
  42. Title = $"接口文档",
  43. Description = $"HTTP API ",
  44. Contact = new OpenApiContact { Name = "懒得勤快", Email = "[email protected]", Url = new Uri("https://masuit.coom") },
  45. License = new OpenApiLicense { Name = "懒得勤快", Url = new Uri("https://masuit.com") }
  46. });
  47. c.IncludeXmlComments(AppContext.BaseDirectory + "WebSearchDemo.xml");
  48. }); //配置swagger
  49. services.AddControllers();
  50. services.AddControllersWithViews().SetCompatibilityVersion(CompatibilityVersion.Latest);
  51. }
  52. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  53. public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataContext db, ISearchEngine<DataContext> searchEngine)
  54. {
  55. if (env.IsDevelopment())
  56. {
  57. app.UseDeveloperExceptionPage();
  58. }
  59. db.Post.AddRange(JsonConvert.DeserializeObject<List<Post>>(File.ReadAllText(AppContext.BaseDirectory + "Posts.json")));
  60. searchEngine.CreateIndex(new List<string>()
  61. {
  62. nameof(Post)
  63. });
  64. app.UseSwagger().UseSwaggerUI(c =>
  65. {
  66. c.SwaggerEndpoint($"/swagger/v1/swagger.json", "懒得勤快的博客,搜索引擎测试");
  67. }); //配置swagger
  68. app.UseRouting().UseEndpoints(endpoints =>
  69. {
  70. endpoints.MapControllers(); // 属性路由
  71. endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由
  72. });
  73. }
  74. }
  75. }