-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
230 lines (182 loc) · 8.11 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Catalog.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Catalog.Settings;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using System.Text.Json;
using System.Net.Mime;
using Microsoft.AspNetCore.Http;
namespace Catalog
{
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)
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));
var mongoDbsettings=Configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
services.AddSingleton<IMongoClient>(serviceProvider=>{
return new MongoClient(mongoDbsettings.ConnectionString);
});
//services.AddSingleton<IItemsRepository,InMemItemsRepository>();
services.AddSingleton<IItemsRepository,MongoDbItemsRepository>();
services.AddControllers(options=>
{
options.SuppressAsyncSuffixInActionNames=false;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Catalog", Version = "v1" });
});
/*services.AddHealthChecks();//to check if the api application is running to serve only*/
/*
1. to check if the api application is running to serve
2. this addition will check if the database is also healthy or not
*/
/*services.AddHealthChecks()
.AddMongoDb(
mongoDbsettings.ConnectionString
,name:"mongoDb"
,timeout:TimeSpan.FromSeconds(3));*/
services.AddHealthChecks()
.AddMongoDb(
mongoDbsettings.ConnectionString
,name:"mongoDb"
,timeout:TimeSpan.FromSeconds(3)
,tags: new[]{"ready"});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Catalog v1"));
}
if(env.IsDevelopment()){
app.UseHttpsRedirection();
}
app.UseRouting();
app.UseAuthorization();
/*app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health");
});*/
//more healthChecks -https://github.com/Xabaril/AspnetCore.Diagnostics.HealthChecks
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health/ready", new HealthCheckOptions{
Predicate=(check)=>check.Tags.Contains("ready"),
ResponseWriter=async(context,report)=>{
var result=JsonSerializer.Serialize(
new {
status=report.Status.ToString()
,checks=report.Entries.Select(entry=>new
{
name=entry.Key,
status=entry.Value.Status.ToString(),
exception=entry.Value.Exception!=null ?entry.Value.Exception.Message:"none",
duration=entry.Value.Duration.ToString()
})
}
);
context.Response.ContentType=MediaTypeNames.Application.Json;
await context.Response.WriteAsync(result);
}
});
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions{
Predicate=(_)=>false
});
});
}
}
}
/*
TO MAKE MONGO DB IMAGE
docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db mongo
docker stop mongo
docker volume ls
docker volume rm mongodbdata
docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=mongoadmin mongo
TO MAKE RESTAPI IMAGE:
* essentials for deployment in production environment:
1. Operating System version
2. .NET 5 Runtime installed
3. Dependencies such as dlls
4. Rest API applicaiton
5. database requirements such as mongoDbEngine etc.
WHY NOT DOCKER?
* Prepare a box
-> Physical machine or virtual machine
-> Linus or Windows, pick the correct OS version
* How to take the files to the production machine?
->through ftp/pendrive etc
* What if DB requires different version of OS or dependencies?
* What if we want to move to a new version of .NET?
* How do we quickly start the REST API on the machine?
* What if one instance is not enough to handle the load?
DOCKER FILE CAN BE A RESCUE:
* operating system
* .NET/ASP.NET Core Runtime
* Dependencies
* Where to place the files
* How to start the REST API
DOCKER LIFE CYCLE:
* make a docker image on the Docker Engine using a docker file
->you can push that image to a Container Registry
->pull that image to the REST API container into the production environment
-> not only one single instance of your docker container, you can run multiple instances in produciton environment
DOCKER BENEFITS:
* Efficien t resource usage
* Fast start
* Isolation of each container
* Runs anywhere where DockerEngine is available
* Scalability
control shift p->Docker: Add Docker Files to workspace
->.NET ASP>NET Core
->Linux
->port 80
->add compose files-NO
---------
docker build -t catalog:v1 .
docker network create net5tutorial
docker network ls
docker stop mongo
docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=mongoadmin --network=net5tutotial mongo
docker images
docker run -it --rm -p 8080:80 -e MongoDbSettings:Host=mongo -e MongoDbSettings:Password=mongoadmin --network=net5tutorial catalog:v1
--------------------------------------------------------push your image to dub
-- push the api image to dockerHub
docker login
docker tag catalog:v1 imtiaj/catalog:v1
docker push imtiajahammad/cataqlog:v1
-- push the api image to dockerHub
--pulling the api image only
docker logout
docker run -it --rm -p 8080:80 -e MongoDbSettings:Host=mongo -e MongoDbSettings:Password=mongoadmin --network=net5tutorial imtiajahammad/catalog:v1
--pulling the api image only
*/