base code

This commit is contained in:
blek 2023-09-29 20:15:07 +10:00
commit bc067bca0c
Signed by: blek
GPG Key ID: 14546221E3595D0C
11 changed files with 1374 additions and 0 deletions

20
README.md Normal file
View File

@ -0,0 +1,20 @@
# blek! File
blek! File is a free service that would help you with file sharing.
The principle is very simple: you upload a file, then download it from another device. The file will be deleted after 1 download or 30 minutes.
## Licensing
This software is released under GPL3 license, a copyleft license that protects users' freedom by ensuring that all future copies of this software are open source as well.
## Deploying
Simply copy the `docker-compose.yml.example` to `docker-compose.yml`, and `.env.example` to `.env` and edit them if necessary.
The following could be done with these bash commands:
```bash
$ # Notice that those are just for reference; you may not want to 100% copy them
$ cp docker-compose.yml.example docker-compose.yml
$ cp .env.example .env
$ nvim .env # you need to edit this file
# docker-compose up -d # "#" at the start means that the command must be run as root/sudo
$ # It all should me up and running at this point
```

1
filed/.env Normal file
View File

@ -0,0 +1 @@
APP_LOGGING=true

1
filed/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1249
filed/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
filed/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "filed"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenvy = "0.15.7"
femme = "2.2.1"
log = "0.4.20"
tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] }
warp = "0.3.6"

0
filed/Dockerfile Normal file
View File

2
filed/README.md Normal file
View File

@ -0,0 +1,2 @@
# fileD - file daemon
This is a part of blek! File that is responsible for serving and uploading files.

27
filed/src/env.rs Normal file
View File

@ -0,0 +1,27 @@
/*
env.rs - The environment loader. It loads all the .env config to a convenient `Env` struct.
This file provides the `loadenv` function that will do just that.
*/
use std::env::var;
pub struct Env {
pub logging: bool
}
fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> {
let name: String = name.into();
let v = var(name.clone());
if v.is_err() {
return Err(format!("Variable {name} does not exist!"));
}
return Ok(v.unwrap().into())
}
pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
Ok(
Env {
logging: get_var::<&str, String>("APP_LOGGING")?.to_lowercase() == "true",
}
)
}

26
filed/src/main.rs Normal file
View File

@ -0,0 +1,26 @@
#![forbid(unsafe_code)]
#![warn(clippy::suspicious)]
#![warn(clippy::correctness)]
mod env;
mod web;
#[tokio::main]
async fn main() {
dotenvy::dotenv().unwrap();
let envy = env::loadenv().unwrap();
// set up logging
if envy.logging {
#[cfg(debug_assertions)] {
femme::with_level(femme::LevelFilter::Debug)
}
#[cfg(not(debug_assertions))] {
femme::with_level(femme::LevelFilter::Info)
}
} else {
femme::with_level(femme::LevelFilter::Off);
}
web::serve().await;
}

19
filed/src/web/mod.rs Normal file
View File

@ -0,0 +1,19 @@
/*
web - The part of filed that handles everything related to HTTP
*/
use warp::Filter;
mod pages;
/*
Serve the HTTP server
*/
pub async fn serve() {
log::info!("Listening on 0.0.0.0:80");
// let hello = warp::any().map(|| "Hi");
warp::serve(pages::get_routes()).run(([0,0,0,0], 80)).await;
}

16
filed/src/web/pages.rs Normal file
View File

@ -0,0 +1,16 @@
/*
pages.rs - All the HTML pages
*/
use warp::{reply::{Reply, Html}, Filter, reject::Rejection};
pub fn index() -> Html<String> {
warp::reply::html("<b>hiiii ^^</b>".into())
}
pub fn get_routes() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let index_r = warp::path::end().map(index);
warp::any().and(index_r)
}