Coder Social home page Coder Social logo

fsprojects / fsharpplus.aspnetcore Goto Github PK

View Code? Open in Web Editor NEW
8.0 6.0 1.0 56 KB

F#+ bindings for asp.net core. This is intended to enable development of web api:s leveraging F#+ and asp.net core.

License: MIT License

F# 100.00%
fsharpplus fsharp aspnetcore asp-net-core

fsharpplus.aspnetcore's Introduction

FSharpPlus.AspNetCore

F#+ bindings for asp.net core. This is intended to enable development of web api:s leveraging F#+ and asp.net core.


Builds

MacOS/Linux Windows
Travis Badge Build status
Build History Build History

Nuget

Stable Prerelease
NuGet Badge NuGet Badge

Building

Make sure the following requirements are installed in your system:

PM> dotnet build

Getting Started

PM> mkdir MySite
PM> dotnet new console -lang f# -o MySite
PM> dotnet add ./MySite package FSharpPlus.AspNetCore
PM> dotnet add ./MySite package FSharpPlus.AspNetCore.Suave

You can then add your code in the style of Suave

// Learn more about F# at http://fsharp.org

open System

open FSharpPlus

open FSharpPlus.AspNetCore
open FSharpPlus.AspNetCore.Suave
open HttpAdapter
open Successful
open RequestErrors
open Filters
open Writers

open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder

open Newtonsoft.Json

module Json=
  let inline OK v=
    OK (JsonConvert.SerializeObject v)
    >=> setContentType "application/json; charset=utf-8"
  let inline CREATED v=
    CREATED (JsonConvert.SerializeObject v)
    >=> setContentType "application/json; charset=utf-8"

type Todo = { id:int; text:string }
type IDb =
  abstract member AddTodo: string -> Async<Todo>
  abstract member GetTodo: int ->Async<Todo option>

let myWebPart(db:IDb) =
  let register =
      POST >=> hasFormContentType >=> fun ctx -> monad {
      match ctx.request |> Request.Form.tryGet "text" with
      | Some text ->
        let! todo = lift (db.AddTodo (string text))
        return! Json.CREATED todo ctx
      | None ->
        return! BAD_REQUEST "Could not find form text" ctx
    }
  let getTodo id=
      GET >=> fun ctx -> monad {
        match! lift (db.GetTodo id) with
        | Some todo ->     
          return! Json.CREATED todo ctx
        | None ->
          return! NOT_FOUND "Could not find todo" ctx
    }
  WebPart.choose [ 
                  path "/" >=> (OK "/")
                  path "/todo" >=> register
                  pathScan "/todo/%d" getTodo
                  ]


type CmdArgs =
  { Json : string option }
[<EntryPoint>]
let main argv =
  let buildWebHost args =
    WebHost.CreateDefaultBuilder(args)
      .Configure(fun (app: IApplicationBuilder)->
        let db = { new IDb with
                   member this.AddTodo(text)=failwith "not implemented" 
                   member this.GetTodo(id)=failwith "not implemented" 
                 }
        let webPart = myWebPart db
        app
            |> appMap "/v1/" (Suave.appRun webPart)
            |> ignore
      ).Build()
  buildWebHost(argv).Run()
  0

Alternatives

If you are looking for a composable library for web then Suave and Giraffe are the ones mostly used. Giraffe should have better performance than this library. Suave has been around the longest (it inspired Giraffe). For a simple web framework you have also Frank.

fsharpplus.aspnetcore's People

Contributors

wallymathieu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

stjordanis

fsharpplus.aspnetcore's Issues

Reapply miniscaffold

Miniscaffold functionality was removed due to issues with paket. Reapply and file bug reports on paket.

This could be due to implicit dependency on NuGet specific behaviour encoded in the asp.net core package dependencies.

Documentation about why and why not use FSharpPlus.AspNetCore.Suave

Giraffe and Zebra are more performance focused. When it's less relevant to have extra performance, but more relevant to have very composable components (in the style of Suave).

Freya, Saturn and Frank are more oriented on MVC resource style API using CE builders. If that's what you have use for, you should use those.

Cookies and session

Description

Suave implements cookie session. See SuaveIO Example/Program.fs

    path "/session"
      >=> statefulForSession // Session.State.CookieStateStore
      >=> context (fun x ->
        match HttpContext.state x with
        | None ->
          // restarted server without keeping the key; set key manually?
          let msg = "Server Key, Cookie Serialiser reset, or Cookie Data Corrupt, "
                    + "if you refresh the browser page, you'll have gotten a new cookie."
          OK msg

        | Some store ->
          match store.get "counter" with
          | Some y ->
            store.set "counter" (y + 1)
            >=> OK (sprintf "Hello %d time(s)" (y + 1) )
          | None ->
            store.set "counter" 1
            >=> OK "First time")

While Giraffe implements functionality more in line with asp.net core: Giraffe/Auth.fs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.