Startup.cs 3.5 KB

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