Coder Social home page Coder Social logo

stjordanis / fsharpplus.aspnetcore Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fsprojects/fsharpplus.aspnetcore

0.0 0.0 0.0 54 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.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

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.