first for re-cms

This commit is contained in:
EvilMuffinHa 2021-08-18 18:41:20 -04:00
parent 6fd0056c89
commit 2afba79e02
17 changed files with 1962 additions and 0 deletions

6
.gitignore vendored
View File

@ -57,3 +57,9 @@ buck-out/
# CocoaPods
/ios/Pods/
# Rust
/target/
# Environment
*.env

1782
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

5
Cargo.toml Normal file
View File

@ -0,0 +1,5 @@
[workspace]
members = [
"cms",
]

16
cms/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "cms"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.5.0-rc.1"
dotenv = "0.15.0"
oauth2 = { version = "4.1.0", default-features = false }
clap = "2.33.3"
ansi_term = "0.12.1"
chrono = "0.4.19"
diesel = { version = "1.4.4", features = ["postgres", "extras"] }

0
cms/auth.rs Normal file
View File

0
cms/migrations/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
DROP TABLE events;

View File

@ -0,0 +1,7 @@
CREATE TABLE events (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
text VARCHAR,
location VARCHAR NOT NULL,
event_date DATE
);

3
cms/src/auth.rs Normal file
View File

@ -0,0 +1,3 @@
pub fn auth() {
}

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
cms/src/data/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod events;
pub mod schema;
pub mod models;

22
cms/src/data/models.rs Normal file
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
cms/src/data/schema.rs Normal file
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
cms/src/main.rs Normal file
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
cms/src/utils.rs Normal file
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)
}

BIN
cred.db Normal file

Binary file not shown.

10
login.html Normal file
View File

@ -0,0 +1,10 @@
<html>
<head>
</head>
<body>
<form method="post">
<input type="text" id="token" name="token">
<input type="submit" name="Submit">
</form>
</body>
</html>