Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticFiles Middleware #63

Closed
tak1n opened this issue Aug 15, 2014 · 15 comments
Closed

StaticFiles Middleware #63

tak1n opened this issue Aug 15, 2014 · 15 comments

Comments

@tak1n
Copy link
Contributor

tak1n commented Aug 15, 2014

Tried to setup my nickel.rs site on ẁww.bennyklotz.at:3000

Setup steps:

$ cargo build --release
$ ./target/release/website &

After starting my binary in background everything worked fine, after I closed the ssh connection to my server and exited the shell "No data received" was returned.

Here my coresponding src/main.rs file and screenshots without StaticFilesHandler:

extern crate http;
extern crate nickel;

use std::io::net::ip::Ipv4Addr;
use nickel::{
    Nickel, Request, Response
};

fn page (_request: &Request, response: &mut Response) {
        response.set_content_type("html");
        response.send("<h1>Hello Rust!</h1>");
}

fn main() {
    let mut server = Nickel::new();

    // server.utilize(Nickel::static_files("assets/"));

    server.get("/rust", page);
    server.listen(Ipv4Addr(134, 119, 3, 19), 3000);
}

After exiting from the ssh shell it's still working:

http://s30.postimg.org/9g9k8qpkh/working_root.png
http://s27.postimg.org/9cxm4cp8z/working_rust.png

Here my coresponding src/main.rs and screenshots with StaticFilesHandler:

extern crate http;
extern crate nickel;

use std::io::net::ip::Ipv4Addr;
use nickel::{
    Nickel, Request, Response
};

fn page (_request: &Request, response: &mut Response) {
        response.set_content_type("html");
        response.send("<h1>Hello Rust!</h1>");
}

fn main() {
    let mut server = Nickel::new();

    server.utilize(Nickel::static_files("assets/"));

    server.get("/rust", page);
    server.listen(Ipv4Addr(134, 119, 3, 19), 3000);
}

Before exiting fom the ssh connection everything works:

http://postimg.org/image/5v8l4o79r/
http://postimg.org/image/e20kw8xcf/

After exiting from the ssh connection it doesn't work anymore:

http://postimg.org/image/76cppvist/
http://postimg.org/image/jm9fjmc4t/

@tak1n
Copy link
Contributor Author

tak1n commented Aug 15, 2014

It also doesn't crash on the server, after reconnecting to the server and listing my processes website is shown, but it still doesn't work.

@tak1n
Copy link
Contributor Author

tak1n commented Aug 20, 2014

Commented out https://github.com/nickel-org/nickel.rs/blob/master/src/static_files_handler.rs#L51 and used my local nickel for building my app.
Without println! everything works fine in all cases:

  • cargo run
  • cargo build --release && target/release/website
  • cargo build --release && target/release/website && exit

So it's indeed the printing to stdout which causes this problem.

@tak1n
Copy link
Contributor Author

tak1n commented Aug 20, 2014

Not sure if we can use the code optimization level to decide if we should use println! or not.
This would mean:

  • cargo run / cargo build -> allow println!, because we are in "dev mode"
  • cargo build --release -> don't use println!, because we expect it to run in background

@tak1n
Copy link
Contributor Author

tak1n commented Aug 20, 2014

In addition we should not forbid println! in "production mode", but prefer to set stdout to a logpath.
http://doc.rust-lang.org/std/io/stdio/fn.set_stdout.html

@cburgdorf
Copy link
Member

iron uses debug!(). As I get it this will just be completely removed by the compiler if you compile with --release flag.

https://github.com/iron/static-file/blob/master/src/lib.rs#L88

@tak1n
Copy link
Contributor Author

tak1n commented Aug 21, 2014

Okay cool, so for now we should use the same approach.
But for the future I think logging for production would be useful.

@tak1n
Copy link
Contributor Author

tak1n commented Aug 21, 2014

Getting some weird error for: https://github.com/Benny1992/nickel.rs/blob/master/src/static_files_handler.rs#L55, also added the log crate into static_files_handlers.rs

Error:

Could not compile `nickel`.

--- stderr
<log macros>:3:21: 3:24 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:3         static LOC: ::log::LogLocation = ::log::LogLocation {
                                   ^~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:3:21: 3:39 error: use of undeclared type name `log::LogLocation`
<log macros>:3         static LOC: ::log::LogLocation = ::log::LogLocation {
                                   ^~~~~~~~~~~~~~~~~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:3:42: 3:45 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:3         static LOC: ::log::LogLocation = ::log::LogLocation {
                                                        ^~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:3:42: 3:60 error: `log::LogLocation` does not name a structure
<log macros>:3         static LOC: ::log::LogLocation = ::log::LogLocation {
                                                        ^~~~~~~~~~~~~~~~~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:2:51: 2:54 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:2     ($($arg:tt)*) => (if cfg!(not(ndebug)) { log!(::log::DEBUG, $($arg)*) })
                                                                 ^~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:2:51: 2:63 error: unresolved name `log::DEBUG`.
<log macros>:2     ($($arg:tt)*) => (if cfg!(not(ndebug)) { log!(::log::DEBUG, $($arg)*) })
                                                                 ^~~~~~~~~~~~
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:4:17: 4:20 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:4         (lvl != ::log::DEBUG || cfg!(not(ndebug))) &&
                               ^~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:4:17: 4:29 error: unresolved name `log::DEBUG`.
<log macros>:4         (lvl != ::log::DEBUG || cfg!(not(ndebug))) &&
                               ^~~~~~~~~~~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:5:16: 5:19 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:5         lvl <= ::log::log_level() &&
                              ^~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:5:16: 5:32 error: unresolved name `log::log_level`.
<log macros>:5         lvl <= ::log::log_level() &&
                              ^~~~~~~~~~~~~~~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:6:9: 6:12 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:6         ::log::mod_enabled(lvl, module_path!())
                       ^~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:6:9: 6:27 error: unresolved name `log::mod_enabled`.
<log macros>:6         ::log::mod_enabled(lvl, module_path!())
                       ^~~~~~~~~~~~~~~~~~
<log macros>:1:1: 8:2 note: in expansion of log_enabled!
<log macros>:9:12: 9:31 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:10:35: 10:38 error: failed to resolve. Maybe a missing `extern crate log`?
<log macros>:10             format_args!(|args| { ::log::log(lvl, &LOC, args) }, $($arg)+)
                                                  ^~~
note: in expansion of format_args!
<log macros>:10:13: 11:10 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
<log macros>:10:35: 10:45 error: unresolved name `log::log`.
<log macros>:10             format_args!(|args| { ::log::log(lvl, &LOC, args) }, $($arg)+)
                                                  ^~~~~~~~~~
note: in expansion of format_args!
<log macros>:10:13: 11:10 note: expansion site
<log macros>:1:1: 13:2 note: in expansion of log!
<log macros>:2:46: 2:76 note: expansion site
<log macros>:1:1: 3:2 note: in expansion of debug!
src/static_files_handler.rs:55:17: 55:116 note: expansion site
error: aborting due to 14 previous errors

@cburgdorf
Copy link
Member

Did you try with extern crate log?

@tak1n
Copy link
Contributor Author

tak1n commented Aug 21, 2014

https://github.com/Benny1992/nickel.rs/blob/master/src/static_files_handler.rs#L1-L3
You mean without feature and phase directive?

@cburgdorf
Copy link
Member

Oh, never mind. Sorry I'm on mobile. Can't check.

@tak1n
Copy link
Contributor Author

tak1n commented Aug 21, 2014

Ah okay np :)

@tak1n
Copy link
Contributor Author

tak1n commented Oct 23, 2014

This is closed by 3d53cf3

@tak1n tak1n closed this as completed Oct 23, 2014
@tak1n
Copy link
Contributor Author

tak1n commented Oct 23, 2014

The problem was I tried to use extern crate log in static_files_handler.rs instead of lib.rs

@Ryman
Copy link
Member

Ryman commented Oct 24, 2014

@bennyklotz for future reference, you can do use self::log if you use an extern crate in a file other than the crate root, but it's not something you want to do in most cases unless it's a test module or something.

@tak1n
Copy link
Contributor Author

tak1n commented Oct 24, 2014

@Ryman okay thx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants