1- namespace AzureSearchEmulator ;
1+ using System . Text . Json ;
2+ using AzureSearchEmulator ;
3+ using AzureSearchEmulator . Indexing ;
4+ using AzureSearchEmulator . Models ;
5+ using AzureSearchEmulator . Repositories ;
6+ using AzureSearchEmulator . SearchData ;
7+ using AzureSearchEmulator . Searching ;
8+ using Microsoft . AspNetCore . Http . Extensions ;
9+ using Microsoft . AspNetCore . Mvc ;
10+ using Microsoft . AspNetCore . OData ;
11+ using Microsoft . Extensions . Options ;
12+ using Microsoft . OData . Edm ;
13+ using Microsoft . OData . ModelBuilder ;
214
3- public class Program
15+ var builder = WebApplication . CreateBuilder ( args ) ;
16+
17+ var model = GetEdmModel ( ) ;
18+
19+ builder . Services . Configure < EmulatorOptions > ( builder . Configuration . GetSection ( "Emulator" ) ) ;
20+
21+ const string CorsDefaultPolicyName = "AllowAllOrigins" ;
22+
23+ builder . Services . AddCors ( options =>
24+ {
25+ options . AddPolicy ( CorsDefaultPolicyName ,
26+ cors =>
27+ {
28+ cors . AllowAnyOrigin ( )
29+ . AllowAnyMethod ( )
30+ . AllowAnyHeader ( ) ;
31+ } ) ;
32+ } ) ;
33+
34+ builder . Services . AddControllers ( )
35+ . AddJsonOptions ( options =>
36+ {
37+ options . JsonSerializerOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
38+ options . JsonSerializerOptions . DictionaryKeyPolicy = JsonNamingPolicy . CamelCase ;
39+ } )
40+ . AddOData ( options =>
41+ options . Count ( ) . Filter ( ) . Expand ( ) . Select ( ) . OrderBy ( ) . SetMaxTop ( 1000 )
42+ . AddRouteComponents ( "" , model ) ) ;
43+
44+ builder . Services . AddTransient ( sp =>
445{
5- public static void Main ( string [ ] args )
46+ var jsonOptions = sp . GetService < IOptions < JsonOptions > > ( ) ;
47+
48+ if ( jsonOptions == null )
649 {
7- CreateHostBuilder ( args ) . Build ( ) . Run ( ) ;
50+ throw new InvalidOperationException ( "JsonOptions not registered properly" ) ;
851 }
952
10- public static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
11- Host . CreateDefaultBuilder ( args )
12- . ConfigureWebHostDefaults ( webBuilder =>
13- {
14- webBuilder . UseStartup < Startup > ( ) ;
15- } ) ;
16- }
53+ return jsonOptions . Value . JsonSerializerOptions ;
54+ } ) ;
55+
56+ builder . Services . AddTransient < ISearchIndexRepository , FileSearchIndexRepository > ( ) ;
57+ builder . Services . AddSingleton < ILuceneDirectoryFactory , SimpleFSDirectoryFactory > ( ) ;
58+ builder . Services . AddSingleton < ILuceneIndexReaderFactory , LuceneDirectoryReaderFactory > ( ) ;
59+ builder . Services . AddTransient < IIndexSearcher , LuceneNetIndexSearcher > ( ) ;
60+ builder . Services . AddSingleton < ISearchIndexer , LuceneNetSearchIndexer > ( ) ;
61+
62+ var app = builder . Build ( ) ;
63+
64+ if ( builder . Environment . IsDevelopment ( ) )
65+ {
66+ app . UseDeveloperExceptionPage ( ) ;
67+ }
68+
69+ app . UseCors ( CorsDefaultPolicyName ) ;
70+ app . UseODataRouteDebug ( ) ;
71+ app . UseODataQueryRequest ( ) ;
72+ app . UseODataBatching ( ) ;
73+
74+ app . UseRouting ( ) ;
75+
76+ app . Use ( ( context , next ) =>
77+ {
78+ Console . WriteLine ( $ "{ context . Request . Method } { context . Request . GetDisplayUrl ( ) } ") ;
79+ return next ( ) ;
80+ } ) ;
81+
82+ app . MapControllers ( ) ;
83+
84+ await app . RunAsync ( ) ;
85+ return ;
86+
87+ static IEdmModel GetEdmModel ( )
88+ {
89+ var builder = new ODataConventionModelBuilder ( ) ;
90+ builder . EnableLowerCamelCase ( ) ;
91+
92+ var index = builder . EntitySet < SearchIndex > ( "indexes" ) . EntityType ;
93+ index . HasKey ( i => i . Name ) ;
94+
95+ return builder . GetEdmModel ( ) ;
96+ }
0 commit comments