Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    dopin
    @dopin
    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
    Andy Russell
    @euclio
    hm, after another test it seems that my code is only using one of my static file handlers to try and find files
    Ryman
    @Ryman
    @euclio So the second isn't working? Can you share some example code?
    Andy Russell
    @euclio

    @Ryman Seems to be working now. I must have been using the API incorrectly. However, I found that StaticFilesHandler is not quite what I need. I'm trying to make a custom middleware that allows me to modify the path that static files are served from. Here's what I have (doesn't compile):

            server.utilize(middleware! { |request, response|
                let path = request.path_without_query().map(|path| {
                    path[1..].to_owned()
                });
    
                if let Some(path) = path {
                    let path = self.cwd.join(path);
                    match fs::metadata(&path) {
                        Ok(ref attr) if attr.is_file() => return response.send_file(&path),
                        Err(ref e) if e.kind() != NotFound => debug!("Error getting metadata \
                                                                      for file '{:?}': {:?}",
                                                                      path, e),
                        _ => {}
                    }
                };
            });

    self.cwd is a PathBuf. It's an attribute on a containing struct that is basically only responsible for starting the server.

    Ryman
    @Ryman
    @euclio It sounds like you might want to use the Mount middleware? https://github.com/nickel-org/nickel.rs/blob/master/examples/mount.rs#L20
    Andy Russell
    @euclio
    @Ryman I don't think so... Let me give some more context. I'm writing a markdown preview server that renders markdown files into HTML. I want to be able to render images, etc. in the markdown that have relative paths. I was using the StaticFilesHandler middleware to do this. The problem is that if the markdown being rendered is from a different directory, then I would have to change the middleware, which I'm not sure is possible after the server starts. My plan was to have custom middleware that queries the directory that the current markdown file is in, and then serves static files out of that. I'm running into problems with type inference and lifetimes with the middleware! macro though. The above code is basically what I want to do, where self.cwd is the directory that contains the markdown file.
    Ryman
    @Ryman
    If you have a public repo/branch though, it would be interesting to see if there's someway we could make the built in structures more flexible for the usecase.
    @euclio Got it, yeah sounds like you might need something custom or to emulate the mount in someway. The lifetime issues in the above I'm guessing is from self.cwd being in the closure? You'll need to wrap it in an Arc (and possible Mutex/RwLock) if you want to share it across threads (the handler may run across many threads)
    Andy Russell
    @euclio
    This message was deleted
    @Ryman Yeah, that's the problem. I think the issue is that self.cwd needs to be 'static. Now the question is how to do that.
    Ryman
    @Ryman
    @euclio does using std::sync::Arc not work for you?
    Andy Russell
    @euclio
    This message was deleted
    Andy Russell
    @euclio
    @Ryman yes it does! Can't believe I didn't think of that.
    野山菌
    @windyslove

    windys-OEM:~$ cargo install nickel -v
    Updating registry https://github.com/rust-lang/crates.io-index
    Downloading nickel v0.7.3
    Failed to download package nickel v0.7.3 from https://crates.io/api/v1/crates/nickel/0.7.3/download

    Caused by:
    Timeout was reached

    how to resolve this
    Andy Russell
    @euclio
    @windyslove probably a problem with your internet
    Denis Lisov
    @tanriol
    How does Nickel use threads? Does it use a fixed-size thread pool?
    I expect my server to have several long-running responses (proxying a data stream for hours). Do I need to somehow ensure that they don't occupy all threads and block new requests? The trivial "spawn a new thread and pass the Response to it" seems not to work.
    Simon Persson
    @SimonPersson
    @tanriol - It does use a fixed size thread pool, yes. There's an pull request that would enable you to add more threads, nickel-org/nickel.rs#309, but depending on how many connections you expect that might not be enough. In that case, you will have to wait for a change in hyper, discussed here: hyperium/hyper#395.
    Serejka
    @serejkaaa512
    Hello everyone. I've got a question - if i want to get param from request params, like this "/generator/:id/set_freq/:freq", where freq is float, i get all till "." For example if i send "3.5" - i get "3". Maybe someone has already done this, but i didn't find. Thanks.