Skip to content

Commit

Permalink
(#147) Web: add and enable a web project template
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 2, 2022
1 parent baafc57 commit f5d9427
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
35 changes: 35 additions & 0 deletions Emulsion.Web/Controllers/WeatherForecastController.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Emulsion.Web.Controllers

open System
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Logging
open Emulsion.Web

[<ApiController>]
[<Route("[controller]")>]
type WeatherForecastController (logger : ILogger<WeatherForecastController>) =
inherit ControllerBase()

let summaries =
[|
"Freezing"
"Bracing"
"Chilly"
"Cool"
"Mild"
"Warm"
"Balmy"
"Hot"
"Sweltering"
"Scorching"
|]

[<HttpGet>]
member _.Get() =
let rng = System.Random()
[|
for index in 0..4 ->
{ Date = DateTime.Now.AddDays(float index)
TemperatureC = rng.Next(-20,55)
Summary = summaries.[rng.Next(summaries.Length)] }
|]
12 changes: 12 additions & 0 deletions Emulsion.Web/Emulsion.Web.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="WeatherForecast.fs"/>
<Compile Include="Controllers/WeatherForecastController.fs"/>
<Compile Include="WebServer.fs"/>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions Emulsion.Web/WeatherForecast.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Emulsion.Web

open System

type WeatherForecast =
{ Date: DateTime
TemperatureC: int
Summary: string }

member this.TemperatureF =
32.0 + (float this.TemperatureC / 0.5556)
20 changes: 20 additions & 0 deletions Emulsion.Web/WebServer.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Emulsion.Web.WebServer

open System
open System.Threading.Tasks

open Emulsion.Web.Controllers
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection

let run(baseUri: Uri): Task =
// TODO: Pass baseUri
let builder = WebApplication.CreateBuilder()
builder.Services
.AddControllers()
.AddApplicationPart(typeof<WeatherForecastController>.Assembly)
|> ignore

let app = builder.Build()
app.MapControllers() |> ignore
app.RunAsync()
6 changes: 6 additions & 0 deletions Emulsion.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ EndProjectSection
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Emulsion.ContentProxy", "Emulsion.ContentProxy\Emulsion.ContentProxy.fsproj", "{A520FD41-A1CB-4062-AFBC-62A8BED12E81}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Emulsion.Web", "Emulsion.Web\Emulsion.Web.fsproj", "{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -65,6 +67,10 @@ Global
{A520FD41-A1CB-4062-AFBC-62A8BED12E81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A520FD41-A1CB-4062-AFBC-62A8BED12E81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A520FD41-A1CB-4062-AFBC-62A8BED12E81}.Release|Any CPU.Build.0 = Release|Any CPU
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EFD8F6C-43D3-4CE0-ADB8-63401E4A64FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7D1ADF47-BF1C-4007-BB9B-08C283044467} = {973131E1-E645-4A50-A0D2-1886A1A8F0C6}
Expand Down
1 change: 1 addition & 0 deletions Emulsion/Emulsion.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Emulsion.ContentProxy\Emulsion.ContentProxy.fsproj" />
<ProjectReference Include="..\Emulsion.Web\Emulsion.Web.fsproj" />
</ItemGroup>
</Project>
25 changes: 20 additions & 5 deletions Emulsion/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Emulsion.Actors
open Emulsion.Database
open Emulsion.MessageSystem
open Emulsion.Settings
open Emulsion.Web
open Emulsion.Xmpp

let private getConfiguration directory (fileName: string) =
Expand Down Expand Up @@ -50,6 +51,13 @@ let private startApp config =
| Some dbSettings -> do! migrateDatabase logger dbSettings
| None -> ()

let webServerTask =
config.Hosting
|> Option.map (fun hosting ->
logger.Information "Initializing web server…"
WebServer.run hosting.BaseUri
)

logger.Information "Actor system preparation…"
use system = ActorSystem.Create("emulsion")
logger.Information "Clients preparation…"
Expand All @@ -73,11 +81,18 @@ let private startApp config =
let! xmppSystem = startMessageSystem logger xmpp core.Tell
logger.Information "System ready"

logger.Information "Waiting for actor system termination…"
do! Async.AwaitTask system.WhenTerminated
logger.Information "Waiting for message systems termination…"
do! telegramSystem
do! xmppSystem
logger.Information "Waiting for the systems to terminate…"
let! _ = Async.Parallel(seq {
yield Async.AwaitTask system.WhenTerminated
yield telegramSystem
yield xmppSystem

match webServerTask with
| Some task -> yield Async.AwaitTask task
| None -> ()
})

logger.Information "Terminated successfully."
with
| error ->
logger.Fatal(error, "General application failure")
Expand Down

0 comments on commit f5d9427

Please sign in to comment.