Spaces:
No application file
No application file
Soham
commited on
Commit
·
44fe768
1
Parent(s):
0345e96
added folder structure
Browse files- DockerFile +24 -0
- program.cs +60 -0
DockerFile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official .NET SDK image to build the project
|
| 2 |
+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy project files and restore dependencies
|
| 6 |
+
COPY . ./
|
| 7 |
+
RUN dotnet restore
|
| 8 |
+
|
| 9 |
+
# Build the application
|
| 10 |
+
RUN dotnet publish -c Release -o /out --no-restore
|
| 11 |
+
|
| 12 |
+
# Use a smaller runtime image for final container
|
| 13 |
+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
|
| 14 |
+
WORKDIR /app
|
| 15 |
+
|
| 16 |
+
# Copy built files from build stage
|
| 17 |
+
COPY --from=build /out .
|
| 18 |
+
|
| 19 |
+
# Expose the port
|
| 20 |
+
EXPOSE 5000
|
| 21 |
+
EXPOSE 5001
|
| 22 |
+
|
| 23 |
+
# Start the application
|
| 24 |
+
ENTRYPOINT ["dotnet", "UserApi.dll"]
|
program.cs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using Microsoft.AspNetCore.Builder;
|
| 2 |
+
using Microsoft.AspNetCore.Http;
|
| 3 |
+
using Microsoft.AspNetCore.Mvc;
|
| 4 |
+
using Microsoft.EntityFrameworkCore;
|
| 5 |
+
using Microsoft.Extensions.DependencyInjection;
|
| 6 |
+
using Microsoft.Extensions.Hosting;
|
| 7 |
+
using System.Collections.Generic;
|
| 8 |
+
using System.Linq;
|
| 9 |
+
using System.Threading.Tasks;
|
| 10 |
+
|
| 11 |
+
var builder = WebApplication.CreateBuilder(args);
|
| 12 |
+
|
| 13 |
+
// Configure the database
|
| 14 |
+
builder.Services.AddDbContext<AppDbContext>(options =>
|
| 15 |
+
options.UseSqlite("Data Source=users.db"));
|
| 16 |
+
|
| 17 |
+
builder.Services.AddEndpointsApiExplorer();
|
| 18 |
+
builder.Services.AddSwaggerGen();
|
| 19 |
+
|
| 20 |
+
var app = builder.Build();
|
| 21 |
+
|
| 22 |
+
if (app.Environment.IsDevelopment())
|
| 23 |
+
{
|
| 24 |
+
app.UseSwagger();
|
| 25 |
+
app.UseSwaggerUI();
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
app.UseHttpsRedirection();
|
| 29 |
+
|
| 30 |
+
// Seed the database
|
| 31 |
+
using (var scope = app.Services.CreateScope())
|
| 32 |
+
{
|
| 33 |
+
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
| 34 |
+
db.Database.Migrate();
|
| 35 |
+
if (!db.Users.Any())
|
| 36 |
+
{
|
| 37 |
+
db.Users.AddRange(new User { Name = "Alice" }, new User { Name = "Bob" });
|
| 38 |
+
db.SaveChanges();
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// Define API endpoints
|
| 43 |
+
app.MapGet("/users", async (AppDbContext db) =>
|
| 44 |
+
await db.Users.ToListAsync());
|
| 45 |
+
|
| 46 |
+
app.Run();
|
| 47 |
+
|
| 48 |
+
// Database context
|
| 49 |
+
class AppDbContext : DbContext
|
| 50 |
+
{
|
| 51 |
+
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
| 52 |
+
public DbSet<User> Users => Set<User>();
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// User model
|
| 56 |
+
class User
|
| 57 |
+
{
|
| 58 |
+
public int Id { get; set; }
|
| 59 |
+
public string Name { get; set; }
|
| 60 |
+
}
|