Skip to content

Instantly share code, notes, and snippets.

@kunjee17
Created July 24, 2018 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunjee17/09297fae74fc1133a7652831e7e6e07c to your computer and use it in GitHub Desktop.
Save kunjee17/09297fae74fc1133a7652831e7e6e07c to your computer and use it in GitHub Desktop.
Sample with service stack and F#
namespace Todos
open System;
open System.IO;
open Microsoft.AspNetCore.Builder;
open Microsoft.AspNetCore.Hosting;
open Microsoft.Extensions.Logging;
open Funq;
open ServiceStack;
open ServiceStack.Configuration;
open ServiceStack.Host.Handlers;
open ServiceStack.Redis;
[<CLIMutableAttribute>]
[<RouteAttribute("/todos")>]
[<RouteAttribute("/todos/{Id}")>]
type Todo = {
Id: int64
Content : string
Order : int
Done : bool
}
type TodoService() =
inherit Service()
member __.Get (todo : Todo) =
if todo.Id <> 0L then __.Redis.As<Todo>().GetById todo.Id |> box else __.Redis.As<Todo>().GetAll() |> box
member __.Post (todo : Todo) =
let redis = __.Redis.As<Todo>()
if todo.Id = 0L then
redis.Store ({todo with Id = redis.GetNextSequence()})
else
redis.Store todo
member __.Put (todo : Todo) =
__.Post(todo)
member __.Delete (todo : Todo) =
__.Redis.As<Todo>().DeleteById todo.Id
type AppHost() as this=
inherit AppHostBase("Backbone.js TODO", typeof<TodoService>.GetAssembly())
do
this.AppSettings <- MultiAppSettings(EnvironmentVariableSettings(), AppSettings())
override __.Configure (container : Container) =
let rm = new RedisManagerPool(ServiceStackHost.Instance.AppSettings.Get("REDIS_HOST", "localhost")) :> IRedisClientsManager
container.Register<IRedisClientsManager>(fun c -> rm) |> ignore
type Startup () =
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment, loggerFactory: ILoggerFactory) =
loggerFactory.AddConsole() |> ignore
if (env.IsDevelopment()) then
app.UseDeveloperExceptionPage() |> ignore
else
ignore()
app.UseServiceStack(new AppHost()) |>ignore
app.Use(new RequestInfoHandler()) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment