mirror of
https://github.com/Blair-SGA-Dev-Team/blazerapp.git
synced 2026-07-16 06:50:22 -04:00
why does proc macro not work
This commit is contained in:
+207
-1
@@ -1,3 +1,209 @@
|
||||
pub fn auth() {
|
||||
use diesel::{prelude::*, Queryable};
|
||||
use oauth2::{
|
||||
basic::BasicClient, reqwest::http_client, AuthUrl, AuthorizationCode, ClientId, ClientSecret,
|
||||
CsrfToken, RedirectUrl, RevocationUrl, Scope, TokenResponse, TokenUrl,
|
||||
};
|
||||
use reqwest::blocking::Client;
|
||||
use rocket::{
|
||||
http::{Cookie, Cookies, SameSite, Status},
|
||||
request,
|
||||
request::FromRequest,
|
||||
response::Redirect,
|
||||
Outcome, Request, State,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::{fmt::Debug, sync::Mutex};
|
||||
|
||||
mod schema {
|
||||
table! {
|
||||
use diesel::sql_types::*;
|
||||
|
||||
auth_val (id) {
|
||||
id -> Integer,
|
||||
email -> Text,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Host(String);
|
||||
pub struct Token(String);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Settings {
|
||||
pub id: String,
|
||||
pub secret: String,
|
||||
pub auth_url: AuthUrl,
|
||||
pub token_url: TokenUrl,
|
||||
}
|
||||
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
struct Auth {
|
||||
pub id: i32,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
fn get_auth(conn: &PgConnection) -> Result<Vec<Auth>, diesel::result::Error> {
|
||||
use schema::auth_val::dsl::*;
|
||||
auth_val.load::<Auth>(conn)
|
||||
}
|
||||
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Host {
|
||||
type Error = ();
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let host = request.headers().get_one("Host");
|
||||
match host {
|
||||
Some(host) => Outcome::Success(Host(host.to_string())),
|
||||
None => Outcome::Failure((Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Token {
|
||||
type Error = ();
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
match request.cookies().get("token") {
|
||||
Some(token) => {
|
||||
let resp: Value = Client::new()
|
||||
.get("https://www.googleapis.com/userinfo/v2/me")
|
||||
.bearer_auth(token.name_value().1)
|
||||
.send()
|
||||
.unwrap()
|
||||
.json()
|
||||
.unwrap();
|
||||
|
||||
if resp["error"] != Value::Null {
|
||||
return Outcome::Failure((Status::Forbidden, ()));
|
||||
} else {
|
||||
let email = resp["email"].clone();
|
||||
let pg = request.guard::<State<Mutex<PgConnection>>>()?;
|
||||
let diesel_op = get_auth(&*(pg.lock().unwrap()));
|
||||
let auths: Vec<String> = match diesel_op {
|
||||
Ok(n) => n.into_iter().map(|x| x.email).collect::<Vec<String>>(),
|
||||
Err(_) => vec![],
|
||||
};
|
||||
|
||||
if auths.into_iter().any(|x| x == email.as_str().unwrap_or("")) {
|
||||
return Outcome::Success(Token(String::from(email.as_str().unwrap_or(""))));
|
||||
} else {
|
||||
return Outcome::Failure((Status::Forbidden, ()));
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Outcome::Failure((Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/oauth")]
|
||||
pub fn oauth(
|
||||
mut cookies: Cookies,
|
||||
settings: State<Settings>,
|
||||
host: Host,
|
||||
) -> Result<Redirect, Status> {
|
||||
let client = get_client(settings.inner().clone(), host);
|
||||
let csrf_token = CsrfToken::new_random();
|
||||
let csrf: String = csrf_token.secret().into();
|
||||
cookies.add(Cookie::new("state", csrf));
|
||||
let (authorize_url, _csrf_state) = client
|
||||
.authorize_url(|| csrf_token.clone())
|
||||
.add_scope(Scope::new(
|
||||
"https://www.googleapis.com/auth/userinfo.email".to_owned(),
|
||||
))
|
||||
.url();
|
||||
let auth = authorize_url.to_string();
|
||||
Ok(Redirect::to(auth))
|
||||
}
|
||||
|
||||
#[get("/logout")]
|
||||
pub fn logout(mut cookies: Cookies) -> Redirect {
|
||||
match cookies.get("token") {
|
||||
Some(_) => {
|
||||
cookies.remove(Cookie::named("token"));
|
||||
Redirect::to("/")
|
||||
}
|
||||
None => Redirect::to("/"),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/callback?<state>&<code>")]
|
||||
pub fn callback(
|
||||
state: String,
|
||||
code: String,
|
||||
pg: State<Mutex<PgConnection>>,
|
||||
mut cookies: Cookies,
|
||||
host: Host,
|
||||
sa: State<Settings>,
|
||||
) -> Result<Redirect, Status> {
|
||||
let sc = cookies.get("state");
|
||||
match sc {
|
||||
Some(c) => {
|
||||
if state != c.value() {
|
||||
return Err(Status::Forbidden);
|
||||
} else {
|
||||
cookies.remove(Cookie::named("state"));
|
||||
let client = get_client(sa.inner().clone(), host);
|
||||
let token_result = client
|
||||
.exchange_code(AuthorizationCode::new(code))
|
||||
.request(http_client);
|
||||
match token_result {
|
||||
Ok(n) => {
|
||||
let secret = n.access_token().secret();
|
||||
|
||||
let resp: Value = Client::new()
|
||||
.get("https://www.googleapis.com/userinfo/v2/me")
|
||||
.bearer_auth(secret)
|
||||
.send()
|
||||
.unwrap()
|
||||
.json()
|
||||
.unwrap();
|
||||
if resp["error"] != Value::Null {
|
||||
return Err(Status::BadRequest);
|
||||
} else {
|
||||
let email = resp["email"].clone();
|
||||
let diesel_op = get_auth(&*(pg.lock().unwrap()));
|
||||
let auths: Vec<String> = match diesel_op {
|
||||
Ok(n) => n.into_iter().map(|x| x.email).collect::<Vec<String>>(),
|
||||
Err(_) => vec![],
|
||||
};
|
||||
if auths.into_iter().any(|x| x == email.as_str().unwrap_or("")) {
|
||||
let mut cook = Cookie::new("token", secret.to_string());
|
||||
cook.set_same_site(SameSite::Strict);
|
||||
cook.set_http_only(true);
|
||||
cook.set_secure(true);
|
||||
cookies.add(cook);
|
||||
return Ok(Redirect::to("/"));
|
||||
} else {
|
||||
return Err(Status::Forbidden);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Err(Status::BadRequest),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_client(settings: Settings, host: Host) -> BasicClient {
|
||||
let gcid = ClientId::new(settings.id);
|
||||
|
||||
let gcs = ClientSecret::new(settings.secret);
|
||||
|
||||
let auth_url = settings.auth_url;
|
||||
let token_url = settings.token_url;
|
||||
|
||||
let base: String = host.0.to_owned();
|
||||
|
||||
BasicClient::new(gcid, Some(gcs), auth_url, Some(token_url))
|
||||
.set_redirect_uri(
|
||||
RedirectUrl::new(format!("http://{}/callback", base)).expect("Invalid redirect URL"),
|
||||
)
|
||||
.set_revocation_uri(
|
||||
RevocationUrl::new("https://oauth2.googleapis.com/revoke".to_owned())
|
||||
.expect("Invalid revocation endpoint URL"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
use diesel::prelude::*;
|
||||
use super::models::{Event, NewEvent};
|
||||
use super::super::utils::{exit_with_error};
|
||||
|
||||
pub fn create_event(conn: &PgConnection, event: NewEvent) -> Event {
|
||||
|
||||
use super::schema::events;
|
||||
diesel::insert_into(events::table)
|
||||
.values(&event)
|
||||
.get_result(conn)
|
||||
.unwrap_or_else(|_| exit_with_error("Error saving new post"))
|
||||
|
||||
}
|
||||
|
||||
pub fn get_all(conn: &PgConnection) -> Result<Vec<Event>, diesel::result::Error> {
|
||||
|
||||
use super::schema::events::dsl::*;
|
||||
|
||||
events.load::<Event>(conn)
|
||||
}
|
||||
+238
-3
@@ -1,3 +1,238 @@
|
||||
pub mod events;
|
||||
pub mod schema;
|
||||
pub mod models;
|
||||
#[macro_use]
|
||||
use cms_macro::api_route;
|
||||
use rocket::{
|
||||
http::{RawStr, Status},
|
||||
request::FromParam,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub struct Lang<'a>(Cow<'a, str>);
|
||||
|
||||
fn valid_lang(lang: &str) -> bool {
|
||||
lang.chars().all(|c| (c >= 'a' && c <= 'z')) && lang.chars().count() == 2
|
||||
}
|
||||
|
||||
impl<'a> FromParam<'a> for Lang<'a> {
|
||||
type Error = Status;
|
||||
fn from_param(param: &'a RawStr) -> Result<Lang<'a>, Status> {
|
||||
match valid_lang(param) {
|
||||
true => Ok(Lang(Cow::Borrowed(param))),
|
||||
false => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod defs {
|
||||
use chrono::naive::NaiveDate;
|
||||
use rocket::{http::RawStr, request::FromFormValue};
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DateForm(NaiveDate);
|
||||
|
||||
impl Deref for DateForm {
|
||||
type Target = NaiveDate;
|
||||
|
||||
fn deref(&self) -> &NaiveDate {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'v> FromFormValue<'v> for DateForm {
|
||||
type Error = ();
|
||||
|
||||
fn from_form_value(value: &'v RawStr) -> Result<DateForm, ()> {
|
||||
let value_uri = match value.url_decode() {
|
||||
Ok(n) => n,
|
||||
Err(_) => return Err(()),
|
||||
};
|
||||
let naivedate = NaiveDate::parse_from_str(&value_uri[..], "%m/%d/%Y");
|
||||
match naivedate {
|
||||
Ok(n) => Ok(DateForm(n)),
|
||||
Err(_) => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
api_route! {
|
||||
events {
|
||||
title: (Text, String, String),
|
||||
location: (Text, String, String),
|
||||
text: (Text, String, String),
|
||||
event_date: (Text, NaiveDate, DateForm),
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub mod events {
|
||||
|
||||
use crate::data::{defs::*, Lang};
|
||||
use crate::auth::Token;
|
||||
use ::chrono::naive::*;
|
||||
use ::diesel::{prelude::*, Insertable, Queryable};
|
||||
use ::rocket::{http::Status, request::Form, response::Redirect, State};
|
||||
use ::rocket_contrib::{json::Json, templates::Template};
|
||||
use ::serde::Serialize;
|
||||
use ::std::{collections::*, sync::Mutex};
|
||||
|
||||
pub mod schema {
|
||||
table! {
|
||||
use diesel::sql_types::*;
|
||||
|
||||
events (id) {
|
||||
id -> Integer,
|
||||
lang -> Text,
|
||||
title -> Text,
|
||||
location -> Text,
|
||||
text -> Text,
|
||||
event_date -> Date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use schema::events;
|
||||
|
||||
#[derive(Debug, Clone, Queryable, Serialize)]
|
||||
pub struct Get {
|
||||
pub id: i32,
|
||||
pub lang: String,
|
||||
pub title: String,
|
||||
pub location: String,
|
||||
pub text: String,
|
||||
pub event_date: NaiveDate,
|
||||
}
|
||||
|
||||
#[derive(Debug, AsChangeset, Insertable)]
|
||||
#[table_name = "events"]
|
||||
pub struct Create {
|
||||
pub lang: String,
|
||||
pub title: String,
|
||||
pub location: String,
|
||||
pub text: String,
|
||||
pub event_date: NaiveDate,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct Post {
|
||||
pub lang: String,
|
||||
pub title: String,
|
||||
pub location: String,
|
||||
pub text: String,
|
||||
pub event_date: DateForm,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct Update {
|
||||
pub id: i32,
|
||||
pub lang: String,
|
||||
pub title: String,
|
||||
pub location: String,
|
||||
pub text: String,
|
||||
pub event_date: DateForm,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct Delete {
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
fn convert(self) -> Create {
|
||||
Create {
|
||||
lang: self.lang,
|
||||
title: self.title,
|
||||
location: self.location,
|
||||
text: self.text,
|
||||
event_date: *self.event_date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Update {
|
||||
fn convert(self) -> Create {
|
||||
Create {
|
||||
lang: self.lang,
|
||||
title: self.title,
|
||||
location: self.location,
|
||||
text: self.text,
|
||||
event_date: *self.event_date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create(conn: &PgConnection, event: Create) -> Result<Get, diesel::result::Error> {
|
||||
diesel::insert_into(events::table)
|
||||
.values(&event)
|
||||
.get_result(conn)
|
||||
}
|
||||
|
||||
pub fn get(conn: &PgConnection, lg: Lang) -> Result<Vec<Get>, diesel::result::Error> {
|
||||
use schema::events::dsl::*;
|
||||
|
||||
events.filter(lang.eq(lg.0)).load::<Get>(conn)
|
||||
}
|
||||
|
||||
pub fn get_all(conn: &PgConnection) -> Result<Vec<Get>, diesel::result::Error> {
|
||||
use schema::events::dsl::*;
|
||||
events.load::<Get>(conn)
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
conn: &PgConnection,
|
||||
idn: i32,
|
||||
event: Create,
|
||||
) -> Result<Get, diesel::result::Error> {
|
||||
use schema::events::dsl::*;
|
||||
diesel::update(events.find(idn))
|
||||
.set(&event)
|
||||
.get_result::<Get>(conn)
|
||||
}
|
||||
|
||||
pub fn delete(conn: &PgConnection, idn: i32) -> Result<usize, diesel::result::Error> {
|
||||
use schema::events::dsl::*;
|
||||
diesel::delete(events.find(idn)).execute(conn)
|
||||
}
|
||||
|
||||
#[get("/<lang>/events")]
|
||||
pub fn api(pg: State<Mutex<PgConnection>>, lang: Lang) -> Result<Json<Vec<Get>>, Status> {
|
||||
Ok(Json(
|
||||
get(&*(pg.lock().unwrap()), lang).map_err(|_| Status::InternalServerError)?,
|
||||
))
|
||||
}
|
||||
|
||||
#[post("/events/add", data = "<form>")]
|
||||
pub fn add(pg: State<Mutex<PgConnection>>, form: Form<Post>) -> Result<Redirect, Status> {
|
||||
match create(&*(pg.lock().unwrap()), form.into_inner().convert()) {
|
||||
Ok(_) => Ok(Redirect::to("/ui/events")),
|
||||
Err(_) => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/events/del", data = "<form>")]
|
||||
pub fn del(pg: State<Mutex<PgConnection>>, form: Form<Delete>) -> Result<Redirect, Status> {
|
||||
match delete(&*(pg.lock().unwrap()), form.id) {
|
||||
Ok(_) => Ok(Redirect::to("/ui/events")),
|
||||
Err(_) => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/events/upd", data = "<form>")]
|
||||
pub fn upd(pg: State<Mutex<PgConnection>>, form: Form<Update>) -> Result<Redirect, Status> {
|
||||
match update(&*(pg.lock().unwrap()), form.id, form.into_inner().convert()) {
|
||||
Ok(_) => Ok(Redirect::to("/ui/events")),
|
||||
Err(_) => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/events")]
|
||||
pub fn ui(_token: Token, pg: State<Mutex<PgConnection>>) -> Result<Template, Status> {
|
||||
let ctx = get_all(&*(pg.lock().unwrap()))
|
||||
.map_err(|_| Status::InternalServerError)?
|
||||
.iter()
|
||||
.map(|x| (x.id, x.clone()))
|
||||
.collect::<HashMap<i32, Get>>();
|
||||
Ok(Template::render("events", &ctx))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
use super::schema::events;
|
||||
use diesel::Insertable;
|
||||
use diesel::Queryable;
|
||||
use chrono::naive::NaiveDate;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Event {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub location: String,
|
||||
pub text: String,
|
||||
pub event_date: NaiveDate,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name="events"]
|
||||
pub struct NewEvent<'a> {
|
||||
pub title: &'a str,
|
||||
pub location: &'a str,
|
||||
pub text: &'a str,
|
||||
pub event_date: &'a NaiveDate
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
table! {
|
||||
use diesel::sql_types::*;
|
||||
|
||||
events (id) {
|
||||
id -> Integer,
|
||||
title -> Text,
|
||||
text -> Text,
|
||||
location -> Text,
|
||||
event_date -> Date,
|
||||
}
|
||||
}
|
||||
+115
-27
@@ -1,53 +1,141 @@
|
||||
#[macro_use] extern crate diesel;
|
||||
#[macro_use] extern crate rocket;
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
mod utils;
|
||||
mod auth;
|
||||
mod data;
|
||||
mod secret;
|
||||
mod utils;
|
||||
|
||||
use auth::Settings;
|
||||
use clap::{App, Arg};
|
||||
use rocket::{Build, Rocket};
|
||||
use utils::{exit_with_error, db_conn};
|
||||
use diesel::prelude::*;
|
||||
use dotenv::dotenv;
|
||||
use oauth2::{AuthUrl, TokenUrl};
|
||||
use rocket::{
|
||||
config::{Config, Environment},
|
||||
Rocket,
|
||||
};
|
||||
use rocket_contrib::templates::Template;
|
||||
use std::{
|
||||
collections::*,
|
||||
env,
|
||||
net::IpAddr,
|
||||
path::{Path, PathBuf},
|
||||
sync::Mutex,
|
||||
};
|
||||
use utils::{db_conn, exit_with_error};
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> &'static str {
|
||||
"Hello, world!"
|
||||
fn index() -> Template {
|
||||
let context: HashMap<&str, &str> = [("oauth", "/oauth")].iter().cloned().collect();
|
||||
Template::render("index", &context)
|
||||
}
|
||||
|
||||
|
||||
fn rocket(port: u32) -> Rocket<Build> {
|
||||
let figment = rocket::Config::figment()
|
||||
.merge(("port", port));
|
||||
rocket::custom(figment)
|
||||
.mount("/", routes![index])
|
||||
|
||||
#[get("/static/<path..>")]
|
||||
fn static_files(path: PathBuf) -> Option<rocket::response::NamedFile> {
|
||||
rocket::response::NamedFile::open(Path::new("cms/static/").join(path)).ok()
|
||||
}
|
||||
|
||||
fn rocket(port: u16, address: String, env: Environment, pg: PgConnection, sa: Settings) -> Rocket {
|
||||
let mut config = Config::build(env)
|
||||
.port(port)
|
||||
.address(address)
|
||||
.secret_key(secret::create_secret())
|
||||
.unwrap();
|
||||
let mut extras = HashMap::new();
|
||||
extras.insert("template_dir".to_string(), "cms/templates/".into());
|
||||
config.set_extras(extras);
|
||||
rocket::custom(config)
|
||||
.attach(Template::fairing())
|
||||
.manage(Mutex::new(pg))
|
||||
.manage(sa)
|
||||
.mount(
|
||||
"/",
|
||||
routes![index, auth::callback, auth::oauth, static_files],
|
||||
)
|
||||
.mount("/api", routes![data::events::api])
|
||||
.mount(
|
||||
"/ui",
|
||||
routes![
|
||||
data::events::ui,
|
||||
data::events::add,
|
||||
data::events::del,
|
||||
data::events::upd
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() {
|
||||
fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let postgres = db_conn("/postgres")
|
||||
.unwrap_or_else(|_| exit_with_error("Error connecting to database. "));
|
||||
let gcid = env::var("BLAZERCMS_CLIENT_ID")
|
||||
.unwrap_or_else(|_| exit_with_error("BLAZERCMS_CLIENT_ID must be set"));
|
||||
|
||||
let gcs = env::var("BLAZERCMS_SECRET")
|
||||
.unwrap_or_else(|_| exit_with_error("BLAZERCMS_SECRET must be set"));
|
||||
|
||||
let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth".to_owned())
|
||||
.expect("Invalid authorization endpoint.");
|
||||
|
||||
let token_url = TokenUrl::new("https://www.googleapis.com/oauth2/v3/token".to_owned())
|
||||
.expect("Invalid token endpoint. ");
|
||||
|
||||
let postgres = db_conn();
|
||||
|
||||
let matches = App::new("blazercms")
|
||||
.version("1.0")
|
||||
.arg(Arg::with_name("port")
|
||||
.short("p")
|
||||
.long("port")
|
||||
.default_value("8080"))
|
||||
.arg(
|
||||
Arg::with_name("port")
|
||||
.short("p")
|
||||
.long("port")
|
||||
.default_value("8080"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("prod")
|
||||
.short("r")
|
||||
.long("prod")
|
||||
.takes_value(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("addr")
|
||||
.short("a")
|
||||
.long("addr")
|
||||
.default_value("127.0.0.1"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let port = matches
|
||||
.value_of("port")
|
||||
.unwrap()
|
||||
.parse::<u32>()
|
||||
.parse::<u16>()
|
||||
.unwrap_or_else(|_| exit_with_error("Port must be an integer! "));
|
||||
|
||||
if let Err(_) = rocket(port).launch().await {
|
||||
exit_with_error(&format!("Error binding port {}. ", port));
|
||||
}
|
||||
}
|
||||
let addr = matches
|
||||
.value_of("addr")
|
||||
.unwrap()
|
||||
.parse::<IpAddr>()
|
||||
.unwrap_or_else(|_| exit_with_error("Address must be a valid IP Address! "))
|
||||
.to_string();
|
||||
|
||||
let env;
|
||||
if matches.is_present("prod") {
|
||||
env = Environment::Production;
|
||||
} else {
|
||||
env = Environment::Development;
|
||||
}
|
||||
|
||||
let auth_settings = Settings {
|
||||
id: gcid,
|
||||
secret: gcs,
|
||||
auth_url: auth_url,
|
||||
token_url: token_url,
|
||||
};
|
||||
|
||||
let err = rocket(port, addr, env, postgres, auth_settings).launch();
|
||||
exit_with_error(&format!("Error: {}", err));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
use rand::{rngs::OsRng, RngCore};
|
||||
|
||||
pub fn create_secret() -> String {
|
||||
let mut secret = [0u8; 32];
|
||||
OsRng.fill_bytes(&mut secret);
|
||||
base64::encode(&secret)
|
||||
}
|
||||
+9
-9
@@ -1,21 +1,21 @@
|
||||
use ansi_term::Colour::Red;
|
||||
use diesel::prelude::*;
|
||||
use diesel::pg::{PgConnection};
|
||||
use std::{env};
|
||||
use diesel::{pg::PgConnection, prelude::*};
|
||||
use std::env;
|
||||
|
||||
pub fn exit_with_error(msg: &str) -> ! {
|
||||
eprintln!("{}", Red.paint(msg));
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
pub fn db_conn() -> PgConnection {
|
||||
embed_migrations!("migrations");
|
||||
|
||||
|
||||
pub fn db_conn(dbname: &str) -> Result<PgConnection, ConnectionError> {
|
||||
|
||||
let mut database_url = env::var("BLAZERCMS_DATABASE_URL")
|
||||
let database_url = env::var("BLAZERCMS_DATABASE_URL")
|
||||
.unwrap_or_else(|_| exit_with_error("BLAZERCMS_DATABASE_URL must be set"));
|
||||
|
||||
database_url.push_str(dbname);
|
||||
let db = PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| exit_with_error("Error connecting to database. "));
|
||||
|
||||
PgConnection::establish(&database_url)
|
||||
embedded_migrations::run(&db).unwrap_or_else(|_| exit_with_error("Error migrating database. "));
|
||||
db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user