Skip to content

Commit

Permalink
(#102) ContentController: make it work in manual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 28, 2022
1 parent 067da2d commit 9292428
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
1 change: 1 addition & 0 deletions Emulsion.ContentProxy/Emulsion.ContentProxy.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Compile Include="Proxy.fs" />
<Compile Include="ContentStorage.fs" />
<Compile Include="FileCache.fs" />
<Compile Include="SimpleHttpClientFactory.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions Emulsion.ContentProxy/FileCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type FileCache(logger: ILogger,
None
}

let assertCacheDirectoryExists() = async {
Directory.CreateDirectory settings.Directory |> ignore
}

let assertCacheValid() = async {
Directory.EnumerateFileSystemEntries settings.Directory
|> Seq.iter(fun entry ->
Expand All @@ -83,6 +87,7 @@ type FileCache(logger: ILogger,
if size > settings.FileSizeLimitBytes || size > settings.TotalCacheSizeLimitBytes then
return false
else
do! assertCacheDirectoryExists()
do! assertCacheValid()

let allEntries =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Emulsion.TestFramework
namespace Emulsion.ContentProxy

open System.Net.Http

Expand Down
1 change: 0 additions & 1 deletion Emulsion.TestFramework/Emulsion.TestFramework.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<Compile Include="Exceptions.fs" />
<Compile Include="TelegramClientMock.fs" />
<Compile Include="WebFileStorage.fs" />
<Compile Include="SimpleHttpClientFactory.fs" />
<Compile Include="FileCacheUtil.fs" />
<Compile Include="StreamUtils.fs" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Emulsion.Web/WebServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Serilog

open Emulsion.ContentProxy
open Emulsion.Database
open Emulsion.Settings
open Emulsion.Telegram
Expand All @@ -14,6 +15,7 @@ let run (logger: ILogger)
(hostingSettings: HostingSettings)
(databaseSettings: DatabaseSettings)
(telegram: ITelegramClient)
(fileCache: FileCache option)
: Task =
let builder = WebApplication.CreateBuilder(WebApplicationOptions())

Expand All @@ -23,6 +25,7 @@ let run (logger: ILogger)
builder.Services
.AddSingleton(hostingSettings)
.AddSingleton(telegram)
.AddSingleton(fileCache)
.AddTransient<EmulsionDbContext>(fun _ -> new EmulsionDbContext(databaseSettings.ContextOptions))
.AddControllers()
.AddApplicationPart(typeof<ContentController>.Assembly)
Expand Down
78 changes: 44 additions & 34 deletions Emulsion/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

open System
open System.IO
open System.Security.Cryptography

open Akka.Actor
open Microsoft.Extensions.Configuration
open Serilog

open Emulsion.Actors
open Emulsion.ContentProxy
open Emulsion.Database
open Emulsion.Messaging.MessageSystem
open Emulsion.Settings
Expand Down Expand Up @@ -58,42 +60,50 @@ let private startApp config =
config.Database,
config.Hosting)

match config.Database with
| Some dbSettings -> do! migrateDatabase logger dbSettings
| None -> ()

let webServerTask =
match config.Hosting, config.Database with
| Some hosting, Some database ->
logger.Information "Initializing web server…"
Some <| WebServer.run logger hosting database telegram
| _ -> None

logger.Information "Actor system preparation…"
use system = ActorSystem.Create("emulsion")
logger.Information "Clients preparation…"

let factories = { xmppFactory = Xmpp.spawn xmppLogger xmpp
telegramFactory = Telegram.spawn telegramLogger telegram }
logger.Information "Core preparation…"
let core = Core.spawn logger factories system "core"
logger.Information "Message systems preparation…"
let! telegramSystem = startMessageSystem logger telegram core.Tell
let! xmppSystem = startMessageSystem logger xmpp core.Tell
logger.Information "System ready"

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
use sha256 = SHA256.Create()
let fileCacheOption = config.FileCache |> Option.map(fun settings ->
let httpClientFactory = SimpleHttpClientFactory()
new FileCache(logger, settings, httpClientFactory, sha256)
)

try
match config.Database with
| Some dbSettings -> do! migrateDatabase logger dbSettings
| None -> ()
})

logger.Information "Terminated successfully."
let webServerTask =
match config.Hosting, config.Database with
| Some hosting, Some database ->
logger.Information "Initializing the web server…"
Some <| WebServer.run logger hosting database telegram fileCacheOption
| _ -> None

logger.Information "Actor system preparation…"
use system = ActorSystem.Create("emulsion")
logger.Information "Clients preparation…"

let factories = { xmppFactory = Xmpp.spawn xmppLogger xmpp
telegramFactory = Telegram.spawn telegramLogger telegram }
logger.Information "Core preparation…"
let core = Core.spawn logger factories system "core"
logger.Information "Message systems preparation…"
let! telegramSystem = startMessageSystem logger telegram core.Tell
let! xmppSystem = startMessageSystem logger xmpp core.Tell
logger.Information "System ready"

logger.Information "Waiting for the systems to terminate…"
do! Async.Ignore <| Async.Parallel(seq {
yield Async.AwaitTask system.WhenTerminated
yield telegramSystem
yield xmppSystem

match webServerTask with
| Some task -> yield Async.AwaitTask task
| None -> ()
})
finally
fileCacheOption |> Option.iter(fun x -> (x :> IDisposable).Dispose())
logger.Information "Terminated successfully."
with
| error ->
logger.Fatal(error, "General application failure")
Expand Down

0 comments on commit 9292428

Please sign in to comment.