Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    liweijian
    @liweijian
    Hi, I'm new in this gitter, I don't know what's the topic shall we talk. Shall we only talk about Opium? Or we can talk some general topic about OCaml?
    I'm reading 'Real World OCaml', on page 221, there's let (module Bumpable) = int_bumper in Bumpable.bump 3;;. Well, it seems that I've never seen this syntax before, is that some kind of pattern-matching?
    Rudi Grinberg
    @rgrinberg
    This channel is best for talking about Opium. But to answer your question, that syntax is used for unpacking a first class module
    liweijian
    @liweijian

    Can we print the parameter in the terminal that run the opium ? I wrote a function to debug the request from the client

    let getid = get "/getreq" (fun req ->
      let uri = Request.uri req in
      let ua = Uri.get_query_param uri "reqs" in
      let resp =
        match ua with
        | Some cont -> cont
        | None -> "-1"
      in
      Printf.printf "abc";
      `String resp |> respond')

    I can't see the "abc" in the terminal. What's more, if I change "abc" to resp, there's an error

    Error: This expression has type bytes but an expression was expected of type
             ('a, out_channel, unit) format =
               ('a, out_channel, unit, unit, unit, unit) format6
    Rudi Grinberg
    @rgrinberg
    You definitely should be able to see the result of ”abc”. What if you try printing with print_endline?
    If you want to try printing a response you should convert into an sexp first
    print_endline (Sexp.to_string_hum (Response.sexp_of_t resp))
    liweijian
    @liweijian

    thank you @rgrinberg

    I'm reading the source code of opium, I found a lot of >>|, for example:

    module Request_helpers = struct
      let json_exn req =
        req |> Request.body |> Body.to_string >>| Ezjsonm.from_string
      let string_exn req =
        req |> Request.body |> Body.to_string
      let pairs_exn req =
        req |> Request.body |> Body.to_string >>| Uri.query_of_encoded
    end

    When I was reading RWO, it saids >>| is Deferred.map, is that the same in opium?

    Rudi Grinberg
    @rgrinberg
    yup
    open Async.Std brings >>| into scope
    liweijian
    @liweijian
    This message was deleted

    I combined the examples of both get and post parameters as

    let getid req =
      let uri = Request.uri req in
      let ua = Uri.get_query_param uri "ua" in
      let resp = 
        match ua with
        | Some cont -> cont
        | None -> "na"
      in
      App.string_of_body_exn req >>| fun rbody ->
      print_endline (rbody^resp);
      respond (`String (rbody^resp))

    I was wondering if it was the most elegant way?

    Rudi Grinberg
    @rgrinberg
    your code looks fine to me. Although, shouldn’t that be respond’?
    you can also write resp as:
     let resp = Option.value (Uri.get_query_param uri “ua”) ~default:”na” in
    liweijian
    @liweijian
    No, respond works well for me, my testing code was:
    let getid req =
      let uri = Request.uri req in
      let resp = Option.value (Uri.get_query_param uri "ua") ~default:"na"
      in App.string_of_body_exn req >>| fun rbody ->
      print_endline (rbody^resp);
      respond (`String (rbody^resp))
    
    let _ =
      App.empty
      |> get "/" hello
      |> post "/gid" getid
      |> App.run_command
    Vladimir Melnick
    @egoholic
    Hi, guys. My name is Volodymyr. I'm from Kyiv, Ukraine. I'm newbie in OCaml and Opium micro-framework.
    Rudi Grinberg
    @rgrinberg
    hi @egoholic
    Vladimir Melnick
    @egoholic
    Hi. Do you have chat-room for OCaml?
    Gage
    @justgage
    There's a IRC channel for OCaml which is pretty friendly. Also there's the ocaml beginners mailing list https://groups.yahoo.com/neo/groups/ocaml_beginners/info
    @egoholic
    Gage
    @justgage
    Pretty much a newbie in OCaml here too. I was trying to run the first example in the Readme and it doesn't print anything on the console, and when I try to visit localhost:9000/hello/ I get a 404
    Any ideas?
    Rudi Grinberg
    @rgrinberg
    @justgage how are you running the example?
    i’ll try it myself now
    Gage
    @justgage
    ./main.native -p 9000
    Rudi Grinberg
    @rgrinberg
    @justgage there isn’t an example named main
    Gage
    @justgage
    I named it that,
    Rudi Grinberg
    @rgrinberg
    can i see your code
    Gage
    @justgage
    I just coppied and pasted it from the Readme
    open Core_kernel.Std
    open Opium.Std
    
    type person = {
      name: string;
      age: int;
    }
    
    let json_of_person { name ; age } =
      let open Ezjsonm in
      dict [ "name", (string name)
           ; "age", (int age) ]
    
    let print_param = put "/hello/:name" begin fun req ->
      `String ("Hello " ^ param req "name") |> respond'
    end
    
    let print_person = get "/person/:name/:age" begin fun req ->
      let person = {
        name = param req "name";
        age = "age" |> param req |> Int.of_string;
      } in
      `Json (person |> json_of_person |> Ezjsonm.wrap) |> respond'
    end
    
    let _ =
      App.empty
      |> print_param
      |> print_person
      |> App.run_command
    Rudi Grinberg
    @rgrinberg
    oh ok, notice that /hello/:name expects a paramter
    and it’s also a put request rather than get
    Try hitting it with $ curl -X PUT http://localhost:9000/hello/testing
    Gage
    @justgage
    Ah, I was trying to do it in the browser. I should have looked at the code closer
    It works :).
    nodecraic
    @webworkio
    hey, if anyone has any remote remote , designer, DevOps or Sysadmin jobs they can post them at http://webwork.io
    lamcee
    @lamcee
    Any body know how to get all the request's header? I mean not just the cookie, I found an example to get the cookie from a req as https://github.com/rgrinberg/opium/blob/119a39c6c6fe937bf44eee9c548711bbbcf6e452/examples/sample.ml#L31 , I was wondering if maybe we could get some other header such as referer?
    Rudi Grinberg
    @rgrinberg
    @lamcee you should use the cohttp header API
    liweijian
    @liweijian
    @rgrinberg thanks, the cohttp header API is clean and clear. let headers = req |> Request.headers
    decapo01
    @decapo01
    when i run opam install opium i get a compile error with Unbound module Lwt_log