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