I am using rocket to make a web server that serves markdown as html. It was going OK until I tried to use css. It loads, but my CSS settings were not working. When I opened up the Inspector, It gave this error:
can not load style sheet
use markdown::*;
use rocket::response::content;
use rocket::response::content::Html;
use rocket::*;
use serde_derive::Deserialize;
use std::fs::File;
use std::io::Read;
use std::ops::Add;
use tokio::*;
use toml;
#[derive(Deserialize)]
struct Config {
pubdir: String,
cssfile: String,
}
#[launch]
async fn rocket() -> _ {
rocket::build().mount("/", routes![getmd, getcssfile])
}
#[get("/<path>")]
fn getmd(path: &str) -> Html<String> {
let mdp = getconf().unwrap().pubdir + path;
println!("{}", mdp);
let mut mdf = File::open(&mdp);
let mut md = String::new();
mdf.unwrap().read_to_string(&mut md);
let html = addcss(gethtm(&md));
println!("{}", html);
return content::Html(html);
}
#[get("/css.css")]
fn getcssfile() -> String {
let css = File::open(getconf().unwrap().cssfile);
let mut csstext = String::new();
css.unwrap().read_to_string(&mut csstext);
println!("{}", csstext);
csstext
}
fn getconf() -> Result<Config, toml::de::Error> {
let mut cFile = File::open("config.toml");
let mut cString = String::new();
cFile.unwrap().read_to_string(&mut cString);
println!("a");
//let config: Result<Config, toml::de::Error> = toml::from_str(&cString);
return toml::from_str(&cString);
}
fn gethtm(md: &str) -> String {
println!("b");
return markdown::to_html(md);
}
fn addcss(htm: String) -> String {
println!("{}", htm);
let mut ohtml = htm.clone();
let mut h = String::new();
let hh = h.clone().add(&("
<head>
<link rel=\u{0022}stylesheet\u{0022} type=\u{0022}text/css\u{0022} href=\u{0022}css.css\u{0022}>
</head>
<body>".to_owned()
+
&htm
+
&"</body>".to_owned()));
//h.add(&htm);
//h.add("</body>");
ohtml.insert_str(htm.len(), "</body>");
println!("{}", ohtml);
return hh;
}
h1 {
color: red;
}
what my browser shows in the "view source"(for the test text I just mashed my keyboard)
<head>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body><h1 id='oiuuoiuiouoi'>oiuuoiuiouoi</h1>
</body>