Skip to content
Snippets Groups Projects
Commit 862750be authored by Paul's avatar Paul :turtle:
Browse files

update to rocket v5.0-rc1

parent c5ae6244
No related branches found
No related tags found
No related merge requests found
Pipeline #4576 failed
...@@ -7,9 +7,8 @@ edition = "2018" ...@@ -7,9 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rocket = "0.4.7" rocket = {version = "0.5.0-rc.1", features = ["json"]}
rocket_contrib = "0.4.7" rocket_dyn_templates = {version = "0.1.0-rc.1", features =["tera"]}
rusqlite = {version = "0.21", features = ["chrono", "serde_json"]}
chrono = {version = "0.4.19", features = ["serde"]} chrono = {version = "0.4.19", features = ["serde"]}
rand = "0.8.3" rand = "0.8.3"
tera = "1.6.1" tera = "1.6.1"
...@@ -20,7 +19,6 @@ toml = "0.5.8" ...@@ -20,7 +19,6 @@ toml = "0.5.8"
lazy_static = "1.4.0" lazy_static = "1.4.0"
domain = "0.6.1" domain = "0.6.1"
rocksdb = "0.16.0" rocksdb = "0.16.0"
leveldb = "0.8.6"
rust-crypto = "0.2.36" rust-crypto = "0.2.36"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
......
use domain::base;
use domain::base::ParsedDname; use domain::base::ParsedDname;
use std::str::FromStr; use std::str::FromStr;
use rocket::request::{FromParam, FromFormValue}; use rocket::form::Form;
use rocket::http::RawStr; use rocket::http::RawStr;
use std::fmt; use std::fmt;
use rocket::request::FromParam;
use rocket::form::{self, FromFormField, ValueField};
#[derive(Clone)] #[derive(Clone)]
...@@ -60,17 +61,15 @@ impl FromStr for Dname { ...@@ -60,17 +61,15 @@ impl FromStr for Dname {
impl<'r> FromParam<'r> for Dname { impl<'r> FromParam<'r> for Dname {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { fn from_param(param: &'r str) -> Result<Self, Self::Error> {
Ok(Dname::new(param.url_decode().unwrap().to_string())) Ok(Dname::new(param.to_string()))
} }
} }
impl<'v> FromFormValue<'v> for Dname { impl<'v> FromFormField<'v> for Dname {
type Error = &'v RawStr; fn from_value(form_value: ValueField) -> form::Result<'v, Self> {
Ok(Self::new(form_value.value.to_string()))
fn from_form_value(form_value: &'v RawStr) -> Result<Self, &'v RawStr> {
Ok(Self::new(form_value.to_string()))
} }
} }
......
...@@ -24,15 +24,17 @@ use std::fmt::{self, Display}; ...@@ -24,15 +24,17 @@ use std::fmt::{self, Display};
use std::net::IpAddr; use std::net::IpAddr;
use tera::Tera; use tera::Tera;
use tera::{self}; use tera::{self};
use rocket::response::{self, content::Plain};
#[get("/update?<token>&<domain>&<ip>")] #[get("/update?<token>&<domain>&<ip>")]
pub fn update( pub fn update(
state: State<AppState>, state: &State<AppState>,
clientip: ClientIp, clientip: ClientIp,
token: String, token: String,
domain: Dname, domain: Dname,
ip: Option<String>, ip: Option<String>,
) -> Result<String, Error> { ) -> Result<Plain<String>, Status> {
// prefer the ip address from parameters // prefer the ip address from parameters
let new_ip: IpAddr = { let new_ip: IpAddr = {
if let Some(iip) = ip { if let Some(iip) = ip {
...@@ -42,14 +44,14 @@ pub fn update( ...@@ -42,14 +44,14 @@ pub fn update(
} }
}; };
state state.service
.service
.update_domain(UpdateRequest { .update_domain(UpdateRequest {
addr: new_ip, addr: new_ip,
token: token, token: token,
domain: domain.to_string(), domain: domain.to_string(),
}) })
.map(|_| "Update successful\n".to_string()) .map(|_| Plain("Update successful\n".to_string()))
.map_err(|_| Status::BadRequest)
} }
...@@ -96,7 +98,7 @@ pub fn update( ...@@ -96,7 +98,7 @@ pub fn update(
// } // }
#[get("/status?<domain>")] #[get("/status?<domain>")]
fn status(db: State<AppState>, domain: String) -> String { fn status(db: &State<AppState>, domain: String) -> String {
let domaininfo = match db.db.get_domain(&domain) { let domaininfo = match db.db.get_domain(&domain) {
None => return "domain not found".to_string(), None => return "domain not found".to_string(),
Some(r) => r, Some(r) => r,
......
...@@ -54,10 +54,12 @@ impl Display for ClientIp { ...@@ -54,10 +54,12 @@ impl Display for ClientIp {
} }
} }
impl<'a, 'r> FromRequest<'a, 'r> for ClientIp {
#[rocket::async_trait]
impl<'r> FromRequest<'r> for ClientIp {
type Error = String; type Error = String;
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> { async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let ip = request.client_ip().unwrap(); let ip = request.client_ip().unwrap();
Outcome::Success(ClientIp(ip)) Outcome::Success(ClientIp(ip))
} }
...@@ -85,12 +87,13 @@ impl Display for AuthorizationToken { ...@@ -85,12 +87,13 @@ impl Display for AuthorizationToken {
} }
impl<'a, 'r> FromRequest<'a, 'r> for AuthorizationToken { #[rocket::async_trait]
impl<'r> FromRequest<'r> for AuthorizationToken {
type Error = String; type Error = String;
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> { async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let header = match request.headers().get_one("Authorization") { let header = match request.headers().get_one("Authorization") {
None => return Outcome::Failure((Status::raw(401), "Authorization header is missing".to_string())), None => return Outcome::Failure((Status::Unauthorized, "Authorization header is missing".to_string())),
Some(x) => x.to_string() Some(x) => x.to_string()
}; };
...@@ -127,18 +130,14 @@ pub fn start_web(db: Database) { ...@@ -127,18 +130,14 @@ pub fn start_web(db: Database) {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
fn rocket_config() -> rocket::Config { fn rocket_config() -> rocket::Config {
rocket::Config::build(rocket::config::Environment::Development) let mut conf = rocket::Config::debug_default();
.address("::") conf.port = 8053;
.port(8053) conf
.finalize()
.unwrap()
} }
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
fn rocket_config() -> rocket::Config { fn rocket_config() -> rocket::Config {
rocket::Config::build(rocket::config::Environment::Production) let mut conf = rocket::Config::release_default();
.address("::") conf.port = 8053;
.port(8053) conf
.finalize()
.unwrap()
} }
...@@ -70,7 +70,7 @@ impl<T> TemplateContext<T> { ...@@ -70,7 +70,7 @@ impl<T> TemplateContext<T> {
} }
#[get("/")] #[get("/")]
pub fn index(state: State<AppState>) -> Html<String> { pub fn index(state: &State<AppState>) -> Html<String> {
let html = TEMPLATES let html = TEMPLATES
.render( .render(
"index", "index",
...@@ -83,7 +83,7 @@ pub fn index(state: State<AppState>) -> Html<String> { ...@@ -83,7 +83,7 @@ pub fn index(state: State<AppState>) -> Html<String> {
#[get("/newdomain?<domainname>&<suffix>&<tos>")] #[get("/newdomain?<domainname>&<suffix>&<tos>")]
pub fn newdomain( pub fn newdomain(
state: State<'_, AppState>, state: &State<AppState>,
domainname: Option<String>, domainname: Option<String>,
suffix: Option<String>, suffix: Option<String>,
tos: Option<bool>, tos: Option<bool>,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment