Init
This commit is contained in:
commit
94fb270008
22 changed files with 2424 additions and 0 deletions
46
backend/src/bin/backend.rs
Normal file
46
backend/src/bin/backend.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
#[cfg(not(target_env = "msvc"))]
|
||||
use tikv_jemallocator::Jemalloc;
|
||||
|
||||
#[cfg(not(target_env = "msvc"))]
|
||||
#[global_allocator]
|
||||
static GLOBAL: Jemalloc = Jemalloc;
|
||||
|
||||
use actix_web::{
|
||||
http::header,
|
||||
middleware,
|
||||
web::{self, Data},
|
||||
App, Error, HttpResponse, HttpServer,
|
||||
};
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use backend::*;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
commands: Commands,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Commands {
|
||||
#[clap(about = "Starts webserver")]
|
||||
Run,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
match Cli::parse().commands {
|
||||
Commands::Run => {
|
||||
std::env::set_var("RUST_LOG", "info");
|
||||
env_logger::init();
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
});
|
||||
server.bind("0.0.0.0:80").unwrap().run().await
|
||||
}
|
||||
}
|
||||
}
|
127
backend/src/bin/blogctl.rs
Normal file
127
backend/src/bin/blogctl.rs
Normal file
|
@ -0,0 +1,127 @@
|
|||
#[cfg(not(target_env = "msvc"))]
|
||||
use tikv_jemallocator::Jemalloc;
|
||||
|
||||
#[cfg(not(target_env = "msvc"))]
|
||||
#[global_allocator]
|
||||
static GLOBAL: Jemalloc = Jemalloc;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use chrono::prelude::*;
|
||||
use clap::{Parser, Subcommand};
|
||||
use diesel::prelude::*;
|
||||
use scan_dir::ScanDir;
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
|
||||
use backend::*;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
commands: Commands,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Commands {
|
||||
#[clap(about = "Imports new posts")]
|
||||
Import,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Blog {
|
||||
name: String,
|
||||
description: String,
|
||||
copyright: String,
|
||||
owner: BlogOwner,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct BlogOwner {
|
||||
name: String,
|
||||
email: String,
|
||||
website: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct PostFrontmatter {
|
||||
name: String,
|
||||
slug: String,
|
||||
description: String,
|
||||
published_at: NaiveDate,
|
||||
edited_at: Option<NaiveDate>,
|
||||
active: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Post {
|
||||
frontmatter: PostFrontmatter,
|
||||
content: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
match Cli::parse().commands {
|
||||
Commands::Import => {
|
||||
let conn = &mut db::establish_connection()?;
|
||||
|
||||
let blog: Blog = serde_yaml::from_str(&fs::read_to_string("/blog/blog.yml")?)?;
|
||||
|
||||
diesel::delete(db::schema::configs::table)
|
||||
.filter(db::schema::configs::active.eq(true))
|
||||
.execute(conn)?;
|
||||
diesel::insert_into(db::schema::configs::table)
|
||||
.values(db::models::NewConfig {
|
||||
active: true,
|
||||
name: &blog.name,
|
||||
description: &blog.description,
|
||||
copyright: &blog.copyright,
|
||||
owner_name: &blog.owner.name,
|
||||
owner_email: &blog.owner.email,
|
||||
owner_website: blog.owner.website.as_deref(),
|
||||
})
|
||||
.execute(conn)?;
|
||||
|
||||
let posts = ScanDir::dirs().read("/blog/posts", |iter| {
|
||||
iter.map(|(entry, name)| {
|
||||
dbg!(&entry, &name);
|
||||
let src = fs::read_to_string(entry.path().join("post.md"))?;
|
||||
let frontmatter = match fronma::parser::parse::<PostFrontmatter>(&src) {
|
||||
Ok(x) => x,
|
||||
Err(x) => bail!("Error parsing frontmatter: {:?}", x),
|
||||
};
|
||||
|
||||
Ok(Post {
|
||||
frontmatter: PostFrontmatter {
|
||||
name: frontmatter.headers.name.trim().to_string(),
|
||||
slug: frontmatter.headers.slug.trim().to_string(),
|
||||
description: frontmatter.headers.description.trim().to_string(),
|
||||
..frontmatter.headers
|
||||
},
|
||||
content: frontmatter.body.trim().to_string(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})??;
|
||||
dbg!(&posts);
|
||||
|
||||
for post in posts {
|
||||
diesel::delete(db::schema::posts::table)
|
||||
.filter(db::schema::posts::slug.eq(&post.frontmatter.slug))
|
||||
.execute(conn)?;
|
||||
diesel::insert_into(db::schema::posts::table)
|
||||
.values(db::models::NewPost {
|
||||
name: &post.frontmatter.name,
|
||||
slug: &post.frontmatter.slug,
|
||||
description: &post.frontmatter.description,
|
||||
content: &post.content,
|
||||
published_at: post.frontmatter.published_at,
|
||||
edited_at: post.frontmatter.edited_at,
|
||||
active: post.frontmatter.active,
|
||||
})
|
||||
.execute(conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
13
backend/src/config.rs
Normal file
13
backend/src/config.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
|
||||
use envconfig::Envconfig;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
#[derive(Envconfig, Debug)]
|
||||
pub struct Config {
|
||||
#[envconfig(from = "BACKEND_DB_URL")]
|
||||
pub db_url: String,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref CONFIG: Config = Config::init_from_env().unwrap();
|
||||
}
|
28
backend/src/db/mod.rs
Normal file
28
backend/src/db/mod.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use anyhow::Result;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::config;
|
||||
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
|
||||
|
||||
pub fn establish_connection() -> ConnectionResult<PgConnection> {
|
||||
PgConnection::establish(&config::CONFIG.db_url)
|
||||
}
|
||||
|
||||
pub fn pool() -> Result<DbPool> {
|
||||
Ok(
|
||||
Pool::builder().build(ConnectionManager::<PgConnection>::new(
|
||||
&config::CONFIG.db_url,
|
||||
))?,
|
||||
)
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref POOL: DbPool = pool().unwrap();
|
||||
}
|
67
backend/src/db/models.rs
Normal file
67
backend/src/db/models.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use chrono::prelude::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::db::schema;
|
||||
|
||||
#[derive(Identifiable, Queryable, Debug)]
|
||||
#[diesel(table_name = schema::configs)]
|
||||
pub struct Config {
|
||||
pub id: i32,
|
||||
pub active: bool,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub copyright: String,
|
||||
pub owner_name: String,
|
||||
pub owner_email: String,
|
||||
pub owner_website: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Insertable, Debug)]
|
||||
#[diesel(table_name = schema::configs)]
|
||||
pub struct NewConfig<'a> {
|
||||
pub active: bool,
|
||||
pub name: &'a str,
|
||||
pub description: &'a str,
|
||||
pub copyright: &'a str,
|
||||
pub owner_name: &'a str,
|
||||
pub owner_email: &'a str,
|
||||
pub owner_website: Option<&'a str>,
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Debug)]
|
||||
#[diesel(table_name = schema::tags)]
|
||||
pub struct Tag {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable, Debug)]
|
||||
#[diesel(table_name = schema::tags)]
|
||||
pub struct NewTag<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Identifiable, Queryable, Debug)]
|
||||
#[diesel(table_name = schema::posts)]
|
||||
pub struct Post {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub slug: String,
|
||||
pub description: String,
|
||||
pub content: String,
|
||||
pub published_at: NaiveDate,
|
||||
pub edited_at: Option<NaiveDate>,
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
#[derive(Insertable, Debug)]
|
||||
#[diesel(table_name = schema::posts)]
|
||||
pub struct NewPost<'a> {
|
||||
pub name: &'a str,
|
||||
pub slug: &'a str,
|
||||
pub description: &'a str,
|
||||
pub content: &'a str,
|
||||
pub published_at: NaiveDate,
|
||||
pub edited_at: Option<NaiveDate>,
|
||||
pub active: bool,
|
||||
}
|
32
backend/src/db/schema.rs
Normal file
32
backend/src/db/schema.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
diesel::table! {
|
||||
configs {
|
||||
id -> Integer,
|
||||
active -> Bool,
|
||||
name -> Text,
|
||||
description -> Text,
|
||||
copyright -> Text,
|
||||
owner_name -> Text,
|
||||
owner_email -> Text,
|
||||
owner_website -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
tags {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
posts {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
slug -> Text,
|
||||
description -> Text,
|
||||
content -> Text,
|
||||
published_at -> Date,
|
||||
edited_at -> Nullable<Date>,
|
||||
active -> Bool,
|
||||
}
|
||||
}
|
2
backend/src/lib.rs
Normal file
2
backend/src/lib.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod config;
|
||||
pub mod db;
|
Loading…
Add table
Add a link
Reference in a new issue