serve spa

This commit is contained in:
cupcakearmy 2021-05-02 12:32:03 +02:00
parent 7b68bd684f
commit 11d714fe94
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
4 changed files with 51 additions and 33 deletions

10
src/client.rs Normal file
View File

@ -0,0 +1,10 @@
use actix_files::{Files, NamedFile};
use actix_web::{web, Responder};
pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(Files::new("/", "./client/build").index_file("index.html"));
}
pub async fn fallback_fn() -> impl Responder {
NamedFile::open("./client/build/index.html")
}

View File

@ -1,9 +1,11 @@
use actix_web::{middleware, App, HttpServer}; use actix_web::{middleware, web, App, HttpServer};
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
mod client;
mod note; mod note;
mod store;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -11,7 +13,10 @@ async fn main() -> std::io::Result<()> {
App::new() App::new()
.wrap(middleware::Compress::default()) .wrap(middleware::Compress::default())
.wrap(middleware::DefaultHeaders::default()) .wrap(middleware::DefaultHeaders::default())
// .wrap(middleware::NormalizePath::default())
.configure(note::init) .configure(note::init)
.configure(client::init)
.default_service(web::resource("").route(web::get().to(client::fallback_fn)))
}) })
.bind("127.0.0.1:5000")? .bind("127.0.0.1:5000")?
.run() .run()

View File

@ -1,9 +1,9 @@
use actix_web::{delete, get, post, web, HttpResponse, Responder}; use actix_web::{delete, get, post, web, HttpResponse, Responder};
use memcache;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::time::SystemTime; use std::time::SystemTime;
use crate::note::{generate_id, Note, NoteInfo, NotePublic}; use crate::note::{generate_id, Note, NoteInfo, NotePublic};
use crate::store;
fn now() -> u64 { fn now() -> u64 {
SystemTime::now() SystemTime::now()
@ -12,31 +12,6 @@ fn now() -> u64 {
.as_secs() .as_secs()
} }
lazy_static! {
static ref CLIENT: memcache::Client =
memcache::connect("memcache://127.0.0.1:11211?timeout=10&tcp_nodelay=true").unwrap();
}
fn set(id: &String, note: &Note) {
let serialized = serde_json::to_string(&note.clone()).unwrap();
CLIENT.set(id, serialized, 0).unwrap();
}
fn get(id: &String) -> Option<Note> {
let value: Option<String> = CLIENT.get(&id).unwrap();
match value {
None => return None,
Some(s) => {
let deserialize: Note = serde_json::from_str(&s).unwrap();
return Some(deserialize);
}
}
}
fn del(id: &String) {
CLIENT.delete(id).unwrap();
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct NotePath { struct NotePath {
id: String, id: String,
@ -45,7 +20,7 @@ struct NotePath {
#[get("/notes/{id}")] #[get("/notes/{id}")]
async fn one(path: web::Path<NotePath>) -> impl Responder { async fn one(path: web::Path<NotePath>) -> impl Responder {
let p = path.into_inner(); let p = path.into_inner();
let note = get(&p.id); let note = store::get(&p.id);
match note { match note {
None => return HttpResponse::NotFound().finish(), None => return HttpResponse::NotFound().finish(),
Some(note) => { Some(note) => {
@ -86,14 +61,14 @@ async fn create(note: web::Json<Note>) -> impl Responder {
} }
_ => {} _ => {}
} }
set(&id.clone(), &n.clone()); store::set(&id.clone(), &n.clone());
return HttpResponse::Ok().json(CreateResponse { id: id }); return HttpResponse::Ok().json(CreateResponse { id: id });
} }
#[delete("/notes/{id}")] #[delete("/notes/{id}")]
async fn delete(path: web::Path<NotePath>) -> impl Responder { async fn delete(path: web::Path<NotePath>) -> impl Responder {
let p = path.into_inner(); let p = path.into_inner();
let note = get(&p.id); let note = store::get(&p.id);
match note { match note {
None => return HttpResponse::NotFound().finish(), None => return HttpResponse::NotFound().finish(),
Some(note) => { Some(note) => {
@ -106,9 +81,9 @@ async fn delete(path: web::Path<NotePath>) -> impl Responder {
changed.views = Some(v - 1); changed.views = Some(v - 1);
let id = p.id.clone(); let id = p.id.clone();
if v <= 1 { if v <= 1 {
del(&id); store::del(&id);
} else { } else {
set(&id, &changed.clone()); store::set(&id, &changed.clone());
} }
} }
_ => {} _ => {}
@ -116,7 +91,7 @@ async fn delete(path: web::Path<NotePath>) -> impl Responder {
match changed.expiration { match changed.expiration {
Some(e) => { Some(e) => {
if e > now() { if e > now() {
del(&p.id.clone()); store::del(&p.id.clone());
return HttpResponse::BadRequest().finish(); return HttpResponse::BadRequest().finish();
} }
} }

28
src/store.rs Normal file
View File

@ -0,0 +1,28 @@
use memcache;
use crate::note::Note;
lazy_static! {
static ref CLIENT: memcache::Client =
memcache::connect("memcache://127.0.0.1:11211?timeout=10&tcp_nodelay=true").unwrap();
}
pub fn set(id: &String, note: &Note) {
let serialized = serde_json::to_string(&note.clone()).unwrap();
CLIENT.set(id, serialized, 0).unwrap();
}
pub fn get(id: &String) -> Option<Note> {
let value: Option<String> = CLIENT.get(&id).unwrap();
match value {
None => return None,
Some(s) => {
let deserialize: Note = serde_json::from_str(&s).unwrap();
return Some(deserialize);
}
}
}
pub fn del(id: &String) {
CLIENT.delete(id).unwrap();
}