Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "System.Net.Http"
- open System.Net
- open System.Net.Http
- open FParsec
- open System.IO
- open Chiron
- type Elem = IDUser of string | IDAs of string | Question of string * string | Exercise of string * Elem list
- with static member ToJson(e:Elem) = match e with
- | IDUser x -> Json.write "user_id" x
- | IDAs x -> Json.write "as_id" x
- | Question (d, c) -> Json.map2 (fun _ x -> x) (Json.write "description" d) (Json.write "code" c)
- | Exercise (d, qs) -> Json.map2 (fun _ x -> x) (Json.write "description" d) (Json.write "questions" qs)
- //general parsers
- let Run(req: HttpRequestMessage, log: TraceWriter) =
- let anyChars = manyChars (noneOf "#`")
- // question parser functions
- let code = pstring "```" >>. anyChars .>> pstring "```"
- let question = pstring "##" >>. pipe3 anyChars (code .>> spaces) (pstring "##END" .>> spaces) (fun d c _ -> Question (d,c))
- let questions = many question .>> spaces
- let exercise = pstring "#" >>. pipe2 anyChars (questions .>> pstring "#END" ) (fun d qs -> Exercise (d, qs))
- let exercises = spaces >>. many (exercise .>> spaces)
- let test p str = match run p str with
- | Success(res, _, _) -> Some res
- | Failure(_, _, _) -> None
- async {
- log.Info("start")
- // Set name to query string
- let name =
- req.GetQueryNameValuePairs()
- |> Seq.tryFind (fun q -> q.Key = "name")
- match name with
- | Some x ->
- return req.CreateResponse(HttpStatusCode.OK, "Hello " + x.Value);
- | None ->
- return req.CreateResponse(HttpStatusCode.OK, "lol")
- } |> Async.RunSynchronously
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement