Advertisement
Guest User

working

a guest
Feb 6th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.66 KB | None | 0 0
  1. #r "System.Net.Http"
  2.  
  3. open System.Net
  4. open System.Net.Http
  5. open FParsec
  6. open System.IO
  7. open Chiron
  8.  
  9. type Elem = IDUser of string | IDAs of string | Question of string * string | Exercise of string * Elem list
  10.     with static member ToJson(e:Elem) = match e with
  11.         | IDUser x -> Json.write "user_id" x
  12.         | IDAs x -> Json.write "as_id" x
  13.         | Question (d, c) -> Json.map2 (fun _ x -> x) (Json.write "description" d) (Json.write "code" c)
  14.         | Exercise (d, qs) -> Json.map2 (fun _ x -> x) (Json.write "description" d) (Json.write "questions" qs)
  15.  
  16. //general parsers
  17.  
  18.  
  19. let Run(req: HttpRequestMessage, log: TraceWriter) =
  20.     let anyChars = manyChars (noneOf "#`")
  21.     // question parser functions
  22.     let code = pstring "```" >>. anyChars .>> pstring "```"
  23.     let question = pstring "##" >>. pipe3 anyChars (code .>> spaces) (pstring "##END" .>> spaces) (fun d c _ -> Question (d,c))
  24.     let questions = many question .>> spaces
  25.     let exercise = pstring "#" >>. pipe2 anyChars (questions .>> pstring "#END" ) (fun d qs -> Exercise (d, qs))
  26.     let exercises = spaces >>. many (exercise .>> spaces)
  27.  
  28.     let test p str = match run p str with
  29.     | Success(res, _, _) -> Some res
  30.     | Failure(_, _, _) -> None
  31.  
  32.     async {
  33.         log.Info("start")
  34.         // Set name to query string
  35.         let name =
  36.             req.GetQueryNameValuePairs()
  37.             |> Seq.tryFind (fun q -> q.Key = "name")
  38.  
  39.         match name with
  40.         | Some x ->
  41.             return req.CreateResponse(HttpStatusCode.OK, "Hello " + x.Value);
  42.         | None ->
  43.             return req.CreateResponse(HttpStatusCode.OK, "lol")
  44.     } |> Async.RunSynchronously
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement