Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Simon Persson
    @SimonPersson
    @ruizander Regarding String/&str, you generally want to use &str, simply because you can create a &str cheaply from a String, but not the other way around.
    Of course there are times when you really need an owned string, then you must use String (and the compiler will tell you if you need to).
    Masood Ahmed
    @masoodahm
    yes thanks @SimonPersson a little bit more help, if you don't mind, the mound example on github says " * the request uri must be reset so that it can be matched against other middleware."
    how do i reset the request uri
    Simon Persson
    @SimonPersson
    @ruizander Perhaps this guide to learning Rust by writing a bunch of linked lists can be helpful to understand references and lifetimes in Rust?
    @masoodahm Aha, that's just a justification for why we that test case exists. It just means that after the middleware is incoked, the request uri is reset to whatever it was before. This is due to the nature of the mount middleware. See the actual implementation here.
    Masood Ahmed
    @masoodahm
    ok how do I redirect in the middleware of server.mount
    if i do res.redirect, it says use of moved value res
    here is what I have tried so far http://is.gd/1P9dYA
    Simon Persson
    @SimonPersson
    @masoodahm But, do you really want to use mount? I only meant for you to look at it as an example of how to run some code before a middleware is reached. You probably want some other logic, where you send users to different pages depending on whether they're logged in or not, no?
    ruizander
    @ruizander
    @SimonPersson thanks for the link! I'm trying....
    @SimonPersson Yes, I've noticed that &'str is used more often, but I didn't found out the reason... is there any special case that String is more recommended? I've done some exercises using Json on ToJson impl, and it only worked using String...
    Simon Persson
    @SimonPersson
    @ruizander Consider the case where you have a function fn a(foo: &str) -> &str, and in this function you want to call a function fn b(bar: String) -> String with foo as the parameter. In this case you will have to write foo.to_owned() which will have to copy every character to create a String, which is undesirable for performance reason. That is why we never actually take String as a parameter unless we really need an owned string.
    @ruizander A reason for wanting an owned string can be that you want to resize the string. Then it won't be enough to have a borrowed slice, we have to own the string.
    @ruizander Another good reason could be that you want to create a new string in a function and return it, then it makes no sense to return a borrowed slice (who would be the owner after the function that creates the string returns?).
    Ryman
    @Ryman
    @masoodahm try return res.redirect("/login"), but as Simon says, you might want to just use a regular handler, e.g. server.use(middleware! { .. your code.. }), which will run on all requests (unless you add middleware before that one).
    dopin
    @dopin
    Hi, I've just started using nicker.rs. But I'm stuck on using server.post which returns 404...
    server.post("/webhooks", middleware! { |request, response| // ...
    curl -X POST http://localhost:6969/webhooks returns 404, I meant.
    Maybe I'm doing something wrong. Can anyone help me? :sob:
    dopin
    @dopin
    I could handle it by doing server.post("**") anyway..
    Masood Ahmed
    @masoodahm
    yes @SimonPersson your are right I didn't need mount, anyways thanks guys I got it, I was under the impression that if a single middleware handler is found the server does not process further, but apparently that is not the case. . nicely architectured guys I can do middleware chaining with this, thanks again @Ryman and @SimonPersson
    Ryman
    @Ryman
    @dopin can you post more of the handler? My guess is that it's hitting the middleware but the middleware is returning a Continue (probably via ())
    dopin
    @dopin
    @Ryman Thanks for replying. I just tried to run on another machine and it worked. I don't know why... I use multirust and maybe I did some mistake yesterday...
    ruizander
    @ruizander
    @Ryman I've been doing some exercises on using rust datatypes, but I confess the compiler is fighting bravely against me... :worried: I'm trying to convert some datatypes from postgres, but I haven't find a good approach doing so...
    ruizander
    @ruizander
    here is what i'm trying to do: I have a struct with one field called data: String, and I have a field comming from the database on json format... I though it would be Option<Json>, so I'm trying to convert this Json to String, but no way till now...
            data: match row.get_opt("data") {
              Ok(info) => { info.to_string() },
              Err(err) => { println!("Error: {:?}", err); "".to_string() }
            }
    I could do using an ugly way, though...
          let data: Option<Json> = row.get_opt("data").unwrap();
          let content;
          match data {
            Some(y) => { content = y.to_string() },
            None => { content = "{}".to_string() }
          }
    but I think unwrapping the field is not the right way to handle, 'cause the field on the database does not require any value... i should simply convert null field to something like "".to_string()
    ruizander
    @ruizander
    the complain is that "info" type, on the first snippet, can't be figured out by the checker... I managed to hint expliciting Option<Json> on the second snippet...
    but i'm almost sure there is a better way
    ruizander
    @ruizander
    Ryman
    @Ryman

    @ruizander [I might be misinterpreting your question!]

    If you know the column names at compile time I think get("data") is ok to use (it's what is in a lot of the documentation too).

    As for the type-hint, it's probably a lot easier if you have your datamodel mirror your data, and then manipulate it once you're in rust types, so just have your data on Block be Option<Json>, e.g:

    struct Block {
        id: Uuid,
        username: String,
        data: Option<Json>,
    }
    
    for row in &rows {
        let block = Block { 
            id: row.get("id"),
            username: row.get("username"),
            data: row.get("data"),
        };
    
        let content = data.map(|j| j.to_string()).unwrap_or_else(|| "{}".into());
        println!("result:  {}, {}, {:?}", block.id, block.username, block.data);
    }
    ruizander
    @ruizander
    @Ryman thanks for the response and considerations! I commented your last 2 lines, only to see if the assignment works, but the program panics... this happens because the database retrieves NULL in the field "data" for various rows, this is expected...
    that's why I was using get_opt("data"), but then I have to unwrap the option...
    ruizander
    @ruizander
    I'd like to handle these NULL's correctly, so the "block" collection only would have rows with "data" field with a string value... this collection will be received by another program, that knows nothing about handling NULL values...
    Ryman
    @Ryman
    @ruizander I thought rust-postgres handled making nullable fields into Option, seems like something a bit weird. But perhaps, would data: row.get_opt("data").ok() work?
    Christoph Burgdorf
    @cburgdorf
    @Ryman I've been waiting for github to create an option to rebase & merge instead of just merge. Great to see homu adds this option. I'm all for it.
    ruizander
    @ruizander
    @Ryman I've noticed that using the hello world example, if I telnet to the server and GET / HTTP/1.1 I can see a curious output....
    https://gist.github.com/ruizander/63173d166b16d95093c5
    what are the "C" and "0"?
    ruizander
    @ruizander
    researching a little, I was reading about keep_alive feature, enabled by default on hyper 0.6.14 and disabled on updated versions... and I was quite impressed with the requests/sec!
    I could not believe how different the behavior could be only specifying a nickel dependency version without touching anything in the source... is it ment to be used this way? or I've got all wrong?
    Ryman
    @Ryman
    @ruizander re "C" and "0", it's this: https://en.wikipedia.org/wiki/Chunked_transfer_encoding
    C = 12, "Hello World!" is 12 octets(bytes)
    0 = 0, signals the end of the stream
    Ryman
    @Ryman
    And the keep-alive thing is pretty bad, it's a case of bad defaults after the change, I think we should be calling keep_alive after we spawn the server, and probably gate it on the timeouts feature: https://github.com/hyperium/hyper/pull/661/files#diff-afb1712a59f738bc3dfe39004cb5e4b9R184
    Andy Russell
    @euclio
    is it possible to use two different static file handler middlewares?
    Ryman
    @Ryman
    @euclio It should work yes, are you having trouble with it?
    Andy Russell
    @euclio
    @Ryman ok, I was, but I'll take another look