Skip to content

Commit 4f52775

Browse files
committed
add server project
1 parent bf164c5 commit 4f52775

File tree

7 files changed

+126
-7
lines changed

7 files changed

+126
-7
lines changed

Diff for: paket.dependencies

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ nuget Fake.JavaScript.Npm
88
nuget FSharp.Core ~> 8
99

1010

11-
nuget Fable.Remoting.Client ~> 7
11+
nuget Fable.Remoting.Client
1212
nuget Fable.SimpleJson
13-
nuget Fable.Mocha
13+
nuget Fable.Mocha
14+
15+
nuget Fable.Remoting.Giraffe

Diff for: paket.lock

+31
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ NUGET
4040
Fable.Remoting.MsgPack (>= 1.24)
4141
Fable.SimpleJson (>= 3.24)
4242
FSharp.Core (>= 4.7.2)
43+
Fable.Remoting.Giraffe (5.21)
44+
Fable.Remoting.Server (>= 5.39)
45+
FSharp.Core (>= 6.0)
46+
Giraffe (>= 5.0)
47+
Microsoft.IO.RecyclableMemoryStream (>= 3.0 < 4.0)
48+
Fable.Remoting.Json (2.25)
49+
FSharp.Core (>= 6.0)
50+
Newtonsoft.Json (>= 13.0.3)
4351
Fable.Remoting.MsgPack (1.24)
4452
FSharp.Core (>= 4.7.2)
53+
Fable.Remoting.Server (5.39)
54+
Fable.Remoting.Json (>= 2.25)
55+
Fable.Remoting.MsgPack (>= 1.24)
56+
FSharp.Core (>= 6.0)
57+
Microsoft.IO.RecyclableMemoryStream (>= 3.0 < 4.0)
4558
Fable.SimpleJson (3.24)
4659
Fable.Core (>= 3.1.5)
4760
Fable.Parsimmon (>= 4.0)
@@ -97,5 +110,23 @@ NUGET
97110
FSharp.Core (>= 6.0.7)
98111
System.Reactive (>= 6.0.1)
99112
FSharp.Core (8.0.403)
113+
FSharp.SystemTextJson (1.3.13)
114+
FSharp.Core (>= 4.7)
115+
System.Text.Json (>= 6.0)
116+
Giraffe (7.0.2)
117+
FSharp.Core (>= 6.0)
118+
FSharp.SystemTextJson (>= 1.3.13)
119+
Giraffe.ViewEngine (>= 1.4)
120+
Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
121+
System.Text.Json (>= 8.0.5)
122+
Giraffe.ViewEngine (1.4)
123+
FSharp.Core (>= 5.0)
124+
Microsoft.IO.RecyclableMemoryStream (3.0.1)
125+
Newtonsoft.Json (13.0.3)
100126
System.Collections.Immutable (9.0.4)
127+
System.IO.Pipelines (9.0.4)
101128
System.Reactive (6.0.1)
129+
System.Text.Encodings.Web (9.0.4)
130+
System.Text.Json (9.0.4)
131+
System.IO.Pipelines (>= 9.0.4)
132+
System.Text.Encodings.Web (>= 9.0.4)

Diff for: src/Server/SAFE.Server.Utils.fsproj

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<RootNamespace>SAFE</RootNamespace>
5+
<Authors>Compositional IT</Authors>
6+
<Copyright>Copyright (c) 2024 Compositional IT</Copyright>
7+
<TargetFramework>net8.0</TargetFramework>
8+
<PackageId>SAFE.Server.Utils</PackageId>
9+
</PropertyGroup>
10+
11+
<PropertyGroup>
12+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
13+
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../../RELEASE-NOTES.txt"))</PackageReleaseNotes>
14+
<PackageProjectUrl>https://github.com/SAFE-Stack/SAFE.Utils</PackageProjectUrl>
15+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
16+
<PackageReadmeFile>readme.md</PackageReadmeFile>
17+
<PackageIcon>safe-logo.png</PackageIcon>
18+
<RepositoryUrl>https://github.com/SAFE-Stack/SAFE.Utils.git</RepositoryUrl>
19+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<None Include="readme.md" Pack="true" PackagePath="" />
24+
<None Include="../../LICENSE" Pack="true" PackagePath="" />
25+
<None Include="../../safe-logo.png" Pack="true" PackagePath="" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Compile Include="SAFE.fs" />
30+
</ItemGroup>
31+
<Import Project="..\..\.paket\Paket.Restore.targets" />
32+
</Project>

Diff for: src/Server/SAFE.fs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace SAFE
2+
3+
open Giraffe
4+
open Fable.Remoting.Server
5+
open Fable.Remoting.Giraffe
6+
7+
[<AutoOpen>]
8+
module Constants =
9+
open Microsoft.Extensions.Hosting
10+
11+
/// Tests if the current environment is development based on the presence of the ASPNETCORE_ENVIRONMENT envionment variable.
12+
let isDev =
13+
System.Environment.GetEnvironmentVariable "ASPNETCORE_ENVIRONMENT" = Environments.Development
14+
15+
/// Pipeline wrappers to add different kinds of dependencies into the DI container.
16+
module DI =
17+
open Microsoft.Extensions.DependencyInjection
18+
/// Shortcut for services.AddSingleton<'T>
19+
let singleton<'T when 'T: not struct> (services: IServiceCollection) = services.AddSingleton<'T>()
20+
/// Shortcut for services.AddTransient<'T>
21+
let transient<'T when 'T: not struct> (services: IServiceCollection) = services.AddTransient<'T>()
22+
/// Shortcut for services.AddScoped<'T>
23+
let scoped<'T when 'T: not struct> (services: IServiceCollection) = services.AddScoped<'T>()
24+
25+
[<AutoOpen>]
26+
module Extensions =
27+
open Microsoft.AspNetCore.Http
28+
open Microsoft.Extensions.Configuration
29+
30+
type HttpContext with
31+
32+
/// Gets the current IConfiguration service from the IOC container.
33+
member this.Configuration = this.GetService<IConfiguration>()
34+
35+
[<RequireQualifiedAccess>]
36+
type Api =
37+
/// <summary>Quickly creates a Fable Remoting API as an HTTP Handler for Giraffe. For reference documentation, see https://zaid-ajaj.github.io/Fable.Remoting/#/server-setup/saturn.</summary>
38+
/// <param name="api">The Fable Remoting API. It must take in an `HTTPContext` as the only argument.</param>
39+
/// <param name="routeBuilder">A function which takes the name of the API and the name of the method and returns the route to the server. Defaults to `/api/{api name}/{method name}` e.g. `/api/ITodoApi/GetTodos`.</param>
40+
/// <param name="errorHandler">A function which takes an `Exception` and a `RouteInfo` and returns a Giraffe Handler. Defaults to `Ignore` the exception i.e. it will not be cascaded.</param>
41+
/// <param name="customOptions">A function which takes the current Fable Remoting options and returns a new set of options. Useful for applying any custom options.</param>
42+
static member make(api, ?routeBuilder, ?errorHandler, ?customOptions) =
43+
let routeBuilder = defaultArg routeBuilder (sprintf "/api/%s/%s")
44+
let errorHandler = defaultArg errorHandler (fun _ _ -> Ignore)
45+
let customOptions = defaultArg customOptions id
46+
47+
Remoting.createApi ()
48+
|> Remoting.withRouteBuilder routeBuilder
49+
|> Remoting.withErrorHandler errorHandler
50+
|> customOptions
51+
|> Remoting.fromContext api
52+
|> Remoting.buildHttpHandler

Diff for: src/Server/paket.references

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fable.Remoting.Giraffe

Diff for: src/Server/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Contains helper types for SAFE apps

Diff for: test/Client/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"type" : "module",
3-
"scripts": {
4-
"pretest": "dotnet fable -o bld",
5-
"test": "mocha bld"
6-
},
2+
"type": "module",
3+
"scripts": {
4+
"pretest": "dotnet fable -o bld --noCache",
5+
"test": "mocha bld"
6+
},
77
"devDependencies": {
88
"mocha": "^10.8.2"
99
}

0 commit comments

Comments
 (0)