mirror of
https://github.com/13hannes11/ics-proxy.git
synced 2024-09-06 08:01:41 +02:00
add .env support and linking of full base url
This commit is contained in:
2
.env
2
.env
@@ -1 +1,3 @@
|
|||||||
DATABASE_URL=sqlite://db/db.db
|
DATABASE_URL=sqlite://db/db.db
|
||||||
|
PROTOCOL=http
|
||||||
|
BASE_URL=localhost:8080
|
||||||
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1129,6 +1129,7 @@ name = "ics-proxy"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"dotenv",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"tera",
|
"tera",
|
||||||
|
|||||||
@@ -11,5 +11,6 @@ uuid = { version = "0.8.2", features = ["v4"] }
|
|||||||
url = "2.2"
|
url = "2.2"
|
||||||
reqwest = { version = "0.11", features = ["blocking"] }
|
reqwest = { version = "0.11", features = ["blocking"] }
|
||||||
tera = "1.12.1"
|
tera = "1.12.1"
|
||||||
|
dotenv = "0.15"
|
||||||
|
|
||||||
sqlx = { version = "0.4.2", features = [ "sqlite", "runtime-actix-native-tls" ] }
|
sqlx = { version = "0.4.2", features = [ "sqlite", "runtime-actix-native-tls" ] }
|
||||||
28
src/main.rs
28
src/main.rs
@@ -5,11 +5,20 @@ use sqlx::{Pool, Sqlite, SqlitePool};
|
|||||||
use tera::Tera;
|
use tera::Tera;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
extern crate dotenv;
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
mod model;
|
mod model;
|
||||||
use model::Link;
|
use model::Link;
|
||||||
|
|
||||||
const REDIRECT_TIMEOUT_S: i32 = 2;
|
const REDIRECT_TIMEOUT_S: i32 = 2;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct CONFIG {
|
||||||
|
root: String,
|
||||||
|
}
|
||||||
|
|
||||||
async fn make_ics_request(req: HttpRequest) -> impl Responder {
|
async fn make_ics_request(req: HttpRequest) -> impl Responder {
|
||||||
let id = req.match_info().get("id").unwrap_or("");
|
let id = req.match_info().get("id").unwrap_or("");
|
||||||
|
|
||||||
@@ -46,6 +55,7 @@ async fn edit_page(
|
|||||||
tmpl: web::Data<tera::Tera>,
|
tmpl: web::Data<tera::Tera>,
|
||||||
query: web::Query<HashMap<String, String>>,
|
query: web::Query<HashMap<String, String>>,
|
||||||
db_pool: web::Data<Pool<Sqlite>>,
|
db_pool: web::Data<Pool<Sqlite>>,
|
||||||
|
conf: web::Data<CONFIG>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
// one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78
|
// one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78
|
||||||
if let Some(uuid_str) = query.get("uuid") {
|
if let Some(uuid_str) = query.get("uuid") {
|
||||||
@@ -55,6 +65,7 @@ async fn edit_page(
|
|||||||
let mut ctx = tera::Context::new();
|
let mut ctx = tera::Context::new();
|
||||||
ctx.insert("link", &link.destination);
|
ctx.insert("link", &link.destination);
|
||||||
ctx.insert("uuid", &link.uuid);
|
ctx.insert("uuid", &link.uuid);
|
||||||
|
ctx.insert("root", &conf.root);
|
||||||
let s = tmpl
|
let s = tmpl
|
||||||
.render("edit.html", &ctx)
|
.render("edit.html", &ctx)
|
||||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
||||||
@@ -212,7 +223,21 @@ async fn index(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
|
|||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
|
|
||||||
let database_url = "sqlite://db/db.db"; //env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
|
dotenv().ok();
|
||||||
|
|
||||||
|
let database_url = match std::env::var("DATABASE_URL") {
|
||||||
|
Ok(var) => var,
|
||||||
|
Err(e) => panic!("{}", e.to_string()),
|
||||||
|
};
|
||||||
|
let protocol =
|
||||||
|
std::env::var("PROTOCOL").expect("PROTOCOL environemt variable error, make sure it is set");
|
||||||
|
let base_url =
|
||||||
|
std::env::var("BASE_URL").expect("BASE_URL environemt variable error, make sure it is set");
|
||||||
|
|
||||||
|
let conf = CONFIG {
|
||||||
|
root: format!("{}://{}", protocol, base_url),
|
||||||
|
};
|
||||||
|
|
||||||
let db_pool = SqlitePool::connect(&database_url)
|
let db_pool = SqlitePool::connect(&database_url)
|
||||||
.await
|
.await
|
||||||
.expect("could not create db pool");
|
.expect("could not create db pool");
|
||||||
@@ -224,6 +249,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.data(db_pool.clone()) // pass database pool to application so we can access it inside handlers
|
.data(db_pool.clone()) // pass database pool to application so we can access it inside handlers
|
||||||
.data(tera)
|
.data(tera)
|
||||||
|
.data(conf.clone())
|
||||||
.route("/{id}/events.ics", web::get().to(make_ics_request))
|
.route("/{id}/events.ics", web::get().to(make_ics_request))
|
||||||
.service(web::resource("/").route(web::get().to(index)))
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
.service(web::resource("/edit").route(web::get().to(edit_page)))
|
.service(web::resource("/edit").route(web::get().to(edit_page)))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1>Hi, your link is <a href="/{{ uuid }}/events.ics"">/{{ uuid }}/events.ics</a>!</h1>
|
<h1>Hi, your link is <a href="{{ root }}/{{ uuid }}/events.ics"">{{ root }}/{{ uuid }}/events.ics</a>!</h1>
|
||||||
it takes the data from
|
it takes the data from
|
||||||
<p>
|
<p>
|
||||||
<form action=" edit_process">
|
<form action=" edit_process">
|
||||||
|
|||||||
Reference in New Issue
Block a user