Aparently I haven't commited all those changes. I really should/want to do a lot
more and smaller commits.
This commit is contained in:
953
Cargo.lock
generated
953
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
@@ -1,10 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "mize-server"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
ws = "0.9.2"
|
|
||||||
|
|
||||||
|
|||||||
8
src/client-main.rs
Normal file
8
src/client-main.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
extern crate mize;
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
println!("hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
8
src/error.rs
Normal file
8
src/error.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
pub struct MizeError {
|
||||||
|
pub message: String
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn display_error_to_user(error: MizeError){
|
||||||
|
println!("{}", error.message);
|
||||||
|
}
|
||||||
34
src/hyper-server.rs
Normal file
34
src/hyper-server.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
use std::convert::Infallible;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use hyper::{Body, Request, Response, Server};
|
||||||
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
|
use hyper::body::Bytes;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn hyper_server() {
|
||||||
|
// Construct our SocketAddr to listen on...
|
||||||
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
|
||||||
|
// And a MakeService to handle each connection...
|
||||||
|
let make_service = make_service_fn(|_conn| async {
|
||||||
|
Ok::<_, Infallible>(service_fn(handle))
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then bind and serve...
|
||||||
|
let server = Server::bind(&addr).serve(make_service);
|
||||||
|
|
||||||
|
// And run forever...
|
||||||
|
if let Err(e) = server.await {
|
||||||
|
eprintln!("server error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||||
|
let (mut sender, _body) = Body::channel();
|
||||||
|
sender.send_data(Bytes::from("sending from the sender\n"));
|
||||||
|
sender.abort();
|
||||||
|
println!("{:?}", req.body());
|
||||||
|
//println!("{:?}", Body::to_bytes());
|
||||||
|
Ok(Response::new(Body::from("hi")))
|
||||||
|
}
|
||||||
6
src/lib.rs
Normal file
6
src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
pub mod server;
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
|
extern crate mize;
|
||||||
pub mod server;
|
|
||||||
|
|
||||||
static HELP_MESSAGE: &str = "\
|
static HELP_MESSAGE: &str = "\
|
||||||
Usage:
|
Usage:
|
||||||
@@ -9,20 +8,22 @@ Available Commands:
|
|||||||
run starts the server
|
run starts the server
|
||||||
help prints this help message
|
help prints this help message
|
||||||
version prints the version
|
version prints the version
|
||||||
|
import imports a file as a item of type file into the server
|
||||||
|
|
||||||
Available options
|
Available options
|
||||||
-h --help prints this help message
|
-h --help prints this help message
|
||||||
-v --version prints the version
|
-v --version prints the version
|
||||||
|
-f --folder the mize-server-folder
|
||||||
";
|
";
|
||||||
|
|
||||||
//some flags e.g: "--file /tmp" can require that the next argument belongs to them instead of being
|
//some flags e.g: "--file /tmp" can require that the next argument belongs to them instead of being
|
||||||
//the command
|
//the command
|
||||||
static FLAGS_WITH_ARGUMENTS: [&str; 0] = [];
|
static FLAGS_WITH_ARGUMENTS: [&str; 2] = ["--folder", "-f"];
|
||||||
|
|
||||||
static AVAILABLE_COMMANDS: [&str; 3] = ["run", "help", "version"];
|
static AVAILABLE_COMMANDS: [&str; 6] = ["run", "help", "version", "import-js", "import-render", "import"];
|
||||||
|
|
||||||
static AVAILABLE_FLAGS: [&str; 3] = ["--version", "--help"];
|
static AVAILABLE_FLAGS: [&str; 4] = ["--version", "--help", "--folder", "-f"];
|
||||||
static AVAILABLE_ONE_LETTER_FLAGS: [&str; 3] = ["v", "h"];
|
//static AVAILABLE_ONE_LETTER_FLAGS: [&str; 2] = ["v", "h"];
|
||||||
|
|
||||||
static VERSION_MESSAGE: &str = "\
|
static VERSION_MESSAGE: &str = "\
|
||||||
Version: 0
|
Version: 0
|
||||||
@@ -76,7 +77,8 @@ fn main() {
|
|||||||
|
|
||||||
//#### "run" the command and pass the options and subcommands to it
|
//#### "run" the command and pass the options and subcommands to it
|
||||||
match &command[..] {
|
match &command[..] {
|
||||||
"run" => crate::server::run_server(command_args),
|
"run" => mize::server::run_server(args),
|
||||||
|
"import" => mize::server::server_utils::import(args),
|
||||||
"help" => println!("{}", HELP_MESSAGE),
|
"help" => println!("{}", HELP_MESSAGE),
|
||||||
"version" => println!("{}", VERSION_MESSAGE),
|
"version" => println!("{}", VERSION_MESSAGE),
|
||||||
_ => {println!("Command not found!!\n"); println!("{}", HELP_MESSAGE);},
|
_ => {println!("Command not found!!\n"); println!("{}", HELP_MESSAGE);},
|
||||||
150
src/server.rs
150
src/server.rs
@@ -1,18 +1,148 @@
|
|||||||
|
|
||||||
use ws::listen;
|
|
||||||
|
pub mod server_utils;
|
||||||
|
|
||||||
|
use futures_util::{FutureExt, StreamExt};
|
||||||
|
use warp::Filter;
|
||||||
|
use warp::ws::{WebSocket, Message};
|
||||||
|
|
||||||
|
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
|
||||||
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
use std::alloc::handle_alloc_error;
|
||||||
|
use std::sync::{Arc};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
//static API_PATH_STRING: &str = "$api";
|
||||||
|
|
||||||
|
// max file sizez in the mize/data folder (in bytes)
|
||||||
|
static MAX_INDEX_FILE_SIZE: usize = 1_800_000;
|
||||||
|
static MAX_FIELDS_FILE_SIZE: usize = 2_600_000;
|
||||||
|
static MAX_KEYS_FILE_SIZE: usize = 30_000_000;
|
||||||
|
static MAX_DATA_FILE_SIZE: usize = 3_000_000_000;
|
||||||
|
|
||||||
|
|
||||||
|
//static HELP_MESSAGE: &str = "\
|
||||||
|
//Usage:
|
||||||
|
//mize-server server [options]
|
||||||
|
//
|
||||||
|
//Available options
|
||||||
|
//-h --help prints this help message
|
||||||
|
//-v --version prints the version
|
||||||
|
//--folder=<folder> mize-folder: where mize stores all it's stuff.
|
||||||
|
//";
|
||||||
|
|
||||||
|
//some flags e.g: "--file /tmp" can require that the next argument belongs to them instead of being
|
||||||
|
//the command
|
||||||
|
//static FLAGS_WITH_ARGUMENTS: [&str; 0] = [];
|
||||||
|
|
||||||
|
//static AVAILABLE_COMMANDS: [&str; 3] = ["run", "help", "version"];
|
||||||
|
|
||||||
|
//static AVAILABLE_FLAGS: [&str; 2] = ["--version", "--help"];
|
||||||
|
//static AVAILABLE_ONE_LETTER_FLAGS: [&str; 2] = ["v", "h"];
|
||||||
|
|
||||||
|
//static VERSION_MESSAGE: &str = "\
|
||||||
|
//Version: 0
|
||||||
|
//";
|
||||||
|
//
|
||||||
|
|
||||||
|
struct Client {
|
||||||
|
tx: UnboundedSender<Result<Message, warp::Error>>
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run_server(args: Vec<String>) {
|
pub fn run_server(args: Vec<String>) {
|
||||||
println!("running server!");
|
/*
|
||||||
|
* WHAT IT DOES
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
//### get the mize_folder
|
||||||
|
let mut mize_folder = String::new();
|
||||||
|
for i in 1..args.len() {
|
||||||
|
if args[i].contains('=') {
|
||||||
|
let split: Vec<_> = args[i].split('=').collect();
|
||||||
|
if split[0] == "--folder" {
|
||||||
|
mize_folder += split[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//### prep the mize_folder
|
||||||
|
if let Err(err) = crate::server::server_utils::init_mize_folder(mize_folder) {
|
||||||
|
println!("{}", err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
//run the webserver
|
||||||
|
//hyper_server();
|
||||||
|
warp_server();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn ws() {
|
#[tokio::main]
|
||||||
//A WebSocket echo server
|
async fn warp_server() {
|
||||||
listen("127.0.0.1:3003", |out| {
|
/*
|
||||||
move |msg| {
|
*
|
||||||
println!("recieved: {}", msg);
|
*
|
||||||
out.send(format!("You sent me: {}", msg))
|
*/
|
||||||
}
|
|
||||||
}).unwrap();
|
//### Main Endpoints:
|
||||||
|
//## any number (with "-" in them) is interpreted as an item id
|
||||||
|
//## $api/socket/id
|
||||||
|
//## $api/rest/
|
||||||
|
|
||||||
|
let mut clients: Arc<Mutex<Vec<Client>>> = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
|
||||||
|
let socket_route = warp::path!("$api" / "socket")
|
||||||
|
.and(warp::ws())
|
||||||
|
.map(move |ws: warp::ws::Ws| {
|
||||||
|
let clients_clone = Arc::clone(&clients);
|
||||||
|
ws.on_upgrade(move |socket| handle_socket_connection(socket, clients_clone) )
|
||||||
|
});
|
||||||
|
|
||||||
|
//let temp = warp::path("temp")
|
||||||
|
//.and(warp::ws())
|
||||||
|
//.map(move |ws: warp::ws::Ws| {
|
||||||
|
//ws.hi();
|
||||||
|
//});
|
||||||
|
|
||||||
|
//let routes = warp::any().map(|| "Hello, World!");
|
||||||
|
let echo = warp::path("echo").map(|| "Hello World");
|
||||||
|
let test = warp::path("test").map(|| {
|
||||||
|
|
||||||
|
let int = 4+4;
|
||||||
|
format!("{}", int)
|
||||||
|
});
|
||||||
|
|
||||||
|
let routes = warp::get().and(socket_route).or(echo).or(test);
|
||||||
|
|
||||||
|
warp::serve(routes).run(([127, 0, 0, 1], 3000)).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_socket_connection(
|
||||||
|
socket: WebSocket,
|
||||||
|
clients_clone: Arc<Mutex<Vec<Client>>>
|
||||||
|
//test: String
|
||||||
|
){
|
||||||
|
let (socket_tx, mut socket_rx) = socket.split();
|
||||||
|
let (client_tx, client_rx) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
let client_rx = UnboundedReceiverStream::new(client_rx);
|
||||||
|
tokio::spawn(client_rx.forward(socket_tx));
|
||||||
|
|
||||||
|
let mut cli = clients_clone.lock().await;
|
||||||
|
cli.push(Client{tx: client_tx});
|
||||||
|
drop(cli);
|
||||||
|
|
||||||
|
// Reading and broadcasting messages
|
||||||
|
while let Some(result) = socket_rx.next().await {
|
||||||
|
let msg = result.expect("Error when gettin message from WebSocket");
|
||||||
|
let clients = clients_clone.lock().await;
|
||||||
|
for client in &clients[..] {
|
||||||
|
client.tx.send(Ok(msg.clone()));
|
||||||
|
}
|
||||||
|
drop(clients);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
107
src/server/server_utils.rs
Normal file
107
src/server/server_utils.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
use std::io::{self, ErrorKind, SeekFrom};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::path::Path;
|
||||||
|
use crate::error::MizeError;
|
||||||
|
|
||||||
|
pub struct Fields{
|
||||||
|
key: u64,
|
||||||
|
data: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Index{
|
||||||
|
commit_number: u64,
|
||||||
|
fields: Vec<Fields>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn import(args: Vec<String>){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_item(id: u64, key: String, update_string: String){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_index(id: u64, mize_folder: &str) -> io::Result<Index>{
|
||||||
|
let mut index_folder_num: String = String::new();
|
||||||
|
let mut index_num: String = String::new();
|
||||||
|
let id_len = id.to_string().chars().count();
|
||||||
|
|
||||||
|
if id_len <= 6 {
|
||||||
|
index_folder_num = String::from("0");
|
||||||
|
index_num = id.to_string();
|
||||||
|
} else {
|
||||||
|
index_folder_num = String::from(&id.to_string()[..id_len-6]);
|
||||||
|
index_num = String::from(&id.to_string()[id_len-6..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let index_file_path = String::from(mize_folder) + "/index" + &index_folder_num + "/" + &index_num;
|
||||||
|
|
||||||
|
let mut file = File::open(index_file_path)?;
|
||||||
|
|
||||||
|
let mut commit_number_buff: [u8; 8];
|
||||||
|
file.read_exact(&mut commit_number_buff[..])?;
|
||||||
|
|
||||||
|
let fields: Vec<Fields> = Vec::new();
|
||||||
|
|
||||||
|
Ok(Index {
|
||||||
|
commit_number: u64::from_le_bytes(commit_number_buff),
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_mize_folder(mize_folder: String) -> Result<(), MizeError>{
|
||||||
|
//##check if the folder resembles a mize folder
|
||||||
|
|
||||||
|
//## check if the index0 file exists and create one if it does not
|
||||||
|
let index_path = mize_folder.clone() + "/index0";
|
||||||
|
if !Path::new(&index_path).exists() {
|
||||||
|
File::create(&index_path).unwrap();
|
||||||
|
}
|
||||||
|
let index_md = fs::metadata(&index_path).unwrap();
|
||||||
|
if index_md.is_dir(){
|
||||||
|
return Err(MizeError{message: String::from("index is a folder and not a file")})
|
||||||
|
}
|
||||||
|
|
||||||
|
let folders: [&str; 5] = ["fields0","keys0","data0","config","other"];
|
||||||
|
for folder in folders {
|
||||||
|
//## check if the folder exists and create one if it does not
|
||||||
|
let path = mize_folder.clone() + "/" + folder;
|
||||||
|
if !Path::new(&path).exists() {
|
||||||
|
fs::create_dir(&path).unwrap();
|
||||||
|
}
|
||||||
|
let index_md = fs::metadata(&path).unwrap();
|
||||||
|
if index_md.is_file(){
|
||||||
|
return Err(MizeError{message: String::from(folder) + " is a file and not a folder"});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = read_index(0, &mize_folder);
|
||||||
|
|
||||||
|
//if let Ok(i) = index {
|
||||||
|
|
||||||
|
//}
|
||||||
|
if let Err(i) = index{
|
||||||
|
println!("{:?}", i.kind());
|
||||||
|
match i.kind() {
|
||||||
|
io::ErrorKind::UnexpectedEof => {
|
||||||
|
//create the first item
|
||||||
|
|
||||||
|
//create index for first item
|
||||||
|
File::create(mize_folder.clone() + "/keys0/0");
|
||||||
|
|
||||||
|
//create keyfile in keyfile0
|
||||||
|
File::create(mize_folder.clone() + "/keys0/000_000");
|
||||||
|
},
|
||||||
|
_ => return Err(MizeError{
|
||||||
|
message: String::from("sth went wrong when reading the index of item 0")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user