first for re-cms

This commit is contained in:
2021-08-18 18:41:20 -04:00
parent 6fd0056c89
commit 2afba79e02
17 changed files with 1962 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
pub fn auth() {
}
+20
View File
@@ -0,0 +1,20 @@
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)
}
+3
View File
@@ -0,0 +1,3 @@
pub mod events;
pub mod schema;
pub mod models;
+22
View File
@@ -0,0 +1,22 @@
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
}
+12
View File
@@ -0,0 +1,12 @@
table! {
use diesel::sql_types::*;
events (id) {
id -> Integer,
title -> Text,
text -> Text,
location -> Text,
event_date -> Date,
}
}
+53
View File
@@ -0,0 +1,53 @@
#[macro_use] extern crate diesel;
#[macro_use] extern crate rocket;
mod utils;
mod data;
use clap::{App, Arg};
use rocket::{Build, Rocket};
use utils::{exit_with_error, db_conn};
use dotenv::dotenv;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn rocket(port: u32) -> Rocket<Build> {
let figment = rocket::Config::figment()
.merge(("port", port));
rocket::custom(figment)
.mount("/", routes![index])
}
#[rocket::main]
async fn main() {
dotenv().ok();
let postgres = db_conn("/postgres")
.unwrap_or_else(|_| exit_with_error("Error connecting to database. "));
let matches = App::new("blazercms")
.version("1.0")
.arg(Arg::with_name("port")
.short("p")
.long("port")
.default_value("8080"))
.get_matches();
let port = matches
.value_of("port")
.unwrap()
.parse::<u32>()
.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));
}
}
+21
View File
@@ -0,0 +1,21 @@
use ansi_term::Colour::Red;
use diesel::prelude::*;
use diesel::pg::{PgConnection};
use std::{env};
pub fn exit_with_error(msg: &str) -> ! {
eprintln!("{}", Red.paint(msg));
std::process::exit(1);
}
pub fn db_conn(dbname: &str) -> Result<PgConnection, ConnectionError> {
let mut database_url = env::var("BLAZERCMS_DATABASE_URL")
.unwrap_or_else(|_| exit_with_error("BLAZERCMS_DATABASE_URL must be set"));
database_url.push_str(dbname);
PgConnection::establish(&database_url)
}