init
This commit is contained in:
81
src/main.rs
Normal file
81
src/main.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::path::PathBuf;
|
||||
use std::fs::*;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use structopt::StructOpt;
|
||||
|
||||
use actix_web::{get, web, App, HttpServer, HttpResponse, Responder, Result, HttpRequest};
|
||||
|
||||
use actix_files;
|
||||
use actix_files::*;
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "personal site server")]
|
||||
struct Opt {
|
||||
#[structopt(short, long)]
|
||||
address: String,
|
||||
#[structopt(short, long)]
|
||||
port: u16,
|
||||
}
|
||||
|
||||
fn inject_js(html: &mut String) {
|
||||
let body_location;
|
||||
match html.find("</body>") {
|
||||
Some(val) => body_location = val,
|
||||
None => { eprintln!("this html string failed to inject the js string: {:?}", html); return; },
|
||||
}
|
||||
html.insert_str(body_location - 1 as usize, r#"<script type="text/javascript" src="main.js"></script>"#);
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
let mut file = File::open("index/index.html").unwrap();
|
||||
let mut html = String::new();
|
||||
file.read_to_string(&mut html).unwrap();
|
||||
inject_js(&mut html);
|
||||
HttpResponse::Ok().body(html)
|
||||
}
|
||||
|
||||
#[get("/resume")]
|
||||
async fn resume() -> impl Responder {
|
||||
let mut file = File::open("resume/index.html").unwrap();
|
||||
let mut html = String::new();
|
||||
file.read_to_string(&mut html).unwrap();
|
||||
inject_js(&mut html);
|
||||
HttpResponse::Ok().body(html)
|
||||
}
|
||||
|
||||
#[get("/contact")]
|
||||
async fn contact() -> impl Responder {
|
||||
let mut file = File::open("contact/index.html").unwrap();
|
||||
let mut html = String::new();
|
||||
file.read_to_string(&mut html).unwrap();
|
||||
inject_js(&mut html);
|
||||
HttpResponse::Ok().body(html)
|
||||
}
|
||||
|
||||
#[get("/main.js")]
|
||||
async fn js() -> impl Responder {
|
||||
let mut file = File::open("index/main.js").unwrap();
|
||||
let mut js = String::new();
|
||||
file.read_to_string(&mut js).unwrap();
|
||||
HttpResponse::Ok().body(js)
|
||||
}
|
||||
|
||||
#[actix_web::main] // or #[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
let opt = Opt::from_args();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(index)
|
||||
.service(js)
|
||||
.service(resume)
|
||||
.service(contact)
|
||||
})
|
||||
.bind((opt.address, opt.port))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user