Skip to content

Commit a867e84

Browse files
committed
🚀 add backend api hosted at fly.io via docker
1 parent 4e896ca commit a867e84

12 files changed

+262
-0
lines changed

backend/.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
obj/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace backend.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
22+
{
23+
_logger = logger;
24+
}
25+
26+
[HttpGet]
27+
public IEnumerable<WeatherForecast> Get()
28+
{
29+
var rng = new Random();
30+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31+
{
32+
Date = DateTime.Now.AddDays(index),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = Summaries[rng.Next(Summaries.Length)]
35+
})
36+
.ToArray();
37+
}
38+
}
39+
}

backend/Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
2+
# update the debian based system
3+
RUN apt-get update && apt-get upgrade -y
4+
# install my dev dependacies inc sqllite and curl and unzip
5+
RUN apt-get install -y sqlite3
6+
RUN apt-get install -y libsqlite3-dev
7+
RUN apt-get install -y curl
8+
RUN apt-get install -y unzip
9+
# not sure why im deleting these
10+
RUN rm -rf /var/lib/apt/lists/*
11+
12+
# add debugging in a docker tooling - install the dependencies for Visual Studio Remote Debugger
13+
RUN apt-get update && apt-get install -y --no-install-recommends unzip procps
14+
# install Visual Studio Remote Debugger
15+
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg
16+
WORKDIR /app/web
17+
18+
# layer and build
19+
COPY . .
20+
WORKDIR /app/web
21+
RUN dotnet restore
22+
23+
# layer adding linker then publish after tree shaking
24+
FROM build AS publish
25+
WORKDIR /app/web
26+
RUN dotnet publish -c Release -o out
27+
28+
# final layer using smallest runtime available
29+
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
30+
WORKDIR /app/web
31+
COPY --from=publish app/web/out ./
32+
33+
# expose port and execute aspnetcore app
34+
EXPOSE 5000
35+
ENV ASPNETCORE_URLS=http://+:5000
36+
ENTRYPOINT ["dotnet", "backend.dll"]

backend/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace backend
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:54822",
8+
"sslPort": 44346
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "weatherforecast",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"backend": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"launchUrl": "weatherforecast",
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}

backend/Startup.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.Logging;
13+
14+
namespace backend
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration)
19+
{
20+
Configuration = configuration;
21+
}
22+
23+
public IConfiguration Configuration { get; }
24+
25+
// This method gets called by the runtime. Use this method to add services to the container.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
services.AddControllers();
29+
}
30+
31+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
33+
{
34+
if (env.IsDevelopment())
35+
{
36+
app.UseDeveloperExceptionPage();
37+
}
38+
39+
app.UseHttpsRedirection();
40+
41+
app.UseRouting();
42+
43+
app.UseAuthorization();
44+
45+
app.UseEndpoints(endpoints =>
46+
{
47+
endpoints.MapControllers();
48+
});
49+
}
50+
}
51+
}

backend/WeatherForecast.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace backend
4+
{
5+
public class WeatherForecast
6+
{
7+
public DateTime Date { get; set; }
8+
9+
public int TemperatureC { get; set; }
10+
11+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
12+
13+
public string Summary { get; set; }
14+
}
15+
}

backend/appsettings.Development.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}

backend/appsettings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

backend/backend.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
8+
</Project>

backend/fly.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
app = "blue-dust-2805"
2+
3+
4+
[[services]]
5+
internal_port = 5000
6+
protocol = "tcp"
7+
8+
[services.concurrency]
9+
hard_limit = 25
10+
soft_limit = 20
11+
12+
[[services.ports]]
13+
handlers = ["http"]
14+
port = "80"
15+
16+
[[services.ports]]
17+
handlers = ["tls", "http"]
18+
port = "443"
19+
20+
[[services.tcp_checks]]
21+
interval = 10000
22+
timeout = 2000

blazor.sln

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "frontend", "frontend\frontend.csproj", "{2D2BE2F9-714E-43B0-87AD-60230178F804}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "backend", "backend\backend.csproj", "{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -30,5 +32,17 @@ Global
3032
{2D2BE2F9-714E-43B0-87AD-60230178F804}.Release|x64.Build.0 = Release|Any CPU
3133
{2D2BE2F9-714E-43B0-87AD-60230178F804}.Release|x86.ActiveCfg = Release|Any CPU
3234
{2D2BE2F9-714E-43B0-87AD-60230178F804}.Release|x86.Build.0 = Release|Any CPU
35+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|x64.ActiveCfg = Debug|Any CPU
38+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|x64.Build.0 = Debug|Any CPU
39+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|x86.ActiveCfg = Debug|Any CPU
40+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Debug|x86.Build.0 = Debug|Any CPU
41+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|x64.ActiveCfg = Release|Any CPU
44+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|x64.Build.0 = Release|Any CPU
45+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|x86.ActiveCfg = Release|Any CPU
46+
{DA9BE63F-8AAF-4AA5-B12D-5C156441243A}.Release|x86.Build.0 = Release|Any CPU
3347
EndGlobalSection
3448
EndGlobal

0 commit comments

Comments
 (0)