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"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.7"
rocket_contrib = "0.4.7"
rusqlite = {version = "0.21", features = ["chrono", "serde_json"]}
rocket = {version = "0.5.0-rc.1", features = ["json"]}
rocket_dyn_templates = {version = "0.1.0-rc.1", features =["tera"]}
chrono = {version = "0.4.19", features = ["serde"]}
rand = "0.8.3"
tera = "1.6.1"
......@@ -20,7 +19,6 @@ toml = "0.5.8"
lazy_static = "1.4.0"
domain = "0.6.1"
rocksdb = "0.16.0"
leveldb = "0.8.6"
rust-crypto = "0.2.36"
pretty_env_logger = "0.4.0"
......
use domain::base;
use domain::base::ParsedDname;
use std::str::FromStr;
use rocket::request::{FromParam, FromFormValue};
use rocket::form::Form;
use rocket::http::RawStr;
use std::fmt;
use rocket::request::FromParam;
use rocket::form::{self, FromFormField, ValueField};
#[derive(Clone)]
......@@ -60,17 +61,15 @@ impl FromStr for Dname {
impl<'r> FromParam<'r> for Dname {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
Ok(Dname::new(param.url_decode().unwrap().to_string()))
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
Ok(Dname::new(param.to_string()))
}
}
impl<'v> FromFormValue<'v> for Dname {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<Self, &'v RawStr> {
Ok(Self::new(form_value.to_string()))
impl<'v> FromFormField<'v> for Dname {
fn from_value(form_value: ValueField) -> form::Result<'v, Self> {
Ok(Self::new(form_value.value.to_string()))
}
}
......
......@@ -24,15 +24,17 @@ use std::fmt::{self, Display};
use std::net::IpAddr;
use tera::Tera;
use tera::{self};
use rocket::response::{self, content::Plain};
#[get("/update?<token>&<domain>&<ip>")]
pub fn update(
state: State<AppState>,
state: &State<AppState>,
clientip: ClientIp,
token: String,
domain: Dname,
ip: Option<String>,
) -> Result<String, Error> {
) -> Result<Plain<String>, Status> {
// prefer the ip address from parameters
let new_ip: IpAddr = {
if let Some(iip) = ip {
......@@ -42,14 +44,14 @@ pub fn update(
}
};
state
.service
state.service
.update_domain(UpdateRequest {
addr: new_ip,
token: token,
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(
// }
#[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) {
None => return "domain not found".to_string(),
Some(r) => r,
......
......@@ -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;
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();
Outcome::Success(ClientIp(ip))
}
......@@ -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;
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") {
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()
};
......@@ -127,18 +130,14 @@ pub fn start_web(db: Database) {
#[cfg(debug_assertions)]
fn rocket_config() -> rocket::Config {
rocket::Config::build(rocket::config::Environment::Development)
.address("::")
.port(8053)
.finalize()
.unwrap()
let mut conf = rocket::Config::debug_default();
conf.port = 8053;
conf
}
#[cfg(not(debug_assertions))]
fn rocket_config() -> rocket::Config {
rocket::Config::build(rocket::config::Environment::Production)
.address("::")
.port(8053)
.finalize()
.unwrap()
let mut conf = rocket::Config::release_default();
conf.port = 8053;
conf
}
......@@ -70,7 +70,7 @@ impl<T> TemplateContext<T> {
}
#[get("/")]
pub fn index(state: State<AppState>) -> Html<String> {
pub fn index(state: &State<AppState>) -> Html<String> {
let html = TEMPLATES
.render(
"index",
......@@ -83,7 +83,7 @@ pub fn index(state: State<AppState>) -> Html<String> {
#[get("/newdomain?<domainname>&<suffix>&<tos>")]
pub fn newdomain(
state: State<'_, AppState>,
state: &State<AppState>,
domainname: Option<String>,
suffix: Option<String>,
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