Skip to content

Instantly share code, notes, and snippets.

@Ronaldho80
Last active August 25, 2017 11:23
Show Gist options
  • Save Ronaldho80/3ea760b24266ad07ae91d441a2ea9328 to your computer and use it in GitHub Desktop.
Save Ronaldho80/3ea760b24266ad07ae91d441a2ea9328 to your computer and use it in GitHub Desktop.
Import of Json not working
$ rustup show
Default host: x86_64-unknown-linux-gnu
installed toolchains
--------------------
stable-x86_64-unknown-linux-gnu
nightly-x86_64-unknown-linux-gnu (default)
active toolchain
----------------
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.21.0-nightly (2bb8fca18 2017-08-23)
#![feature(plugin)]
#![plugin(rocket_codegen)]
#[macro_use] extern crate rocket_contrib;
extern crate rocket;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
mod tests;
mod static_files;
use std::io;
use std::io::Read;
use std::path::Path;
use rocket::Request;
use rocket::response::Redirect;
use rocket_contrib::{Template, Json};
use rocket::Data;
use rocket::http::RawStr;
#[derive(Serialize)]
struct TemplateContext {
name: String,
items: Vec<String>,
}
#[get("/")]
fn index() -> Template {
//Redirect::to("/hello/Unknown")
Template::render(
"index",
TemplateContext {
name: "LTX2".to_string(),
items: vec!["one".to_string()],
},
)
}
/// Upload a file
#[post("/upload", data = "<paste>")]
fn upload(paste: Data) -> io::Result<String> {
let filename = format!("upload/uploaded_file.txt");
let url = format!("{host}/uploaded_file.txt\n", host = "http://localhost:8000");
paste.stream_to_file(Path::new(&filename))?;
// Ok(url)
Ok("{\"success\":true}".to_string())
}
#[error(404)]
fn not_found(req: &Request) -> Template {
let mut map = std::collections::HashMap::new();
map.insert("path", req.uri().as_str());
Template::render("error/404", &map)
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, upload, static_files::all])
// .mount("/hello", routes![upload])
.attach(Template::fairing())
.catch(errors![not_found])
}
fn main() {
rocket().launch();
}
use std::path::{Path, PathBuf};
use rocket::response::NamedFile;
#[get("/<path..>", rank=5)]
fn all(path: PathBuf) -> Option<NamedFile> {
println!(">>> {:?}", Path::new("static/").join(path.clone()));
NamedFile::open(Path::new("static/").join(path)).ok()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment