major config rebuild using once cell. added ability to use .env.

This commit is contained in:
mtrx 2024-08-10 08:24:22 +02:00
parent 35cd018152
commit c30976f2d7
26 changed files with 207 additions and 186 deletions

View file

@ -3,7 +3,7 @@ export EMAIL=myemail@example.com
export BIND_PORT=8000
export SUBMIT_TOKEN=asdf
export GITHUB_ACCOUNT=mtrx1337
export ROOT_PATH=../content
export ROOT_PATH=./content
run:
cd site && cargo run

View file

@ -1,35 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="{{ username }}' blog" />
<meta property="og:description" content="Last 5 posts" />
<meta property="og:image" content="/static/site-image.png" />
<title> {{ username }}' blog </title>
<link rel="stylesheet" href="/static/css/blog.css">
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico"/>
</head>
<body>
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }}' blog</a></h1>
<p style="text-align: right">
<a href="/about">About</a>
<a href="/all">All Posts</a></p>
<ul>
{% for post in posts %}
<article>
<div>
<a href="/id/{{ post.id }}" class="post-link">[link]</a>
</div>
<div class="post-content">
<h2 class="post-title">{{ post.title }}</h2>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ post.body }}</p>
</div>
</article>
{% endfor %}
</ul>
</body>
</html>

154
site/Cargo.lock generated
View file

@ -8,7 +8,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f7b0a21988c1bf877cf4759ef5ddaac04c1c9fe808c9142ecb78ba97d97a28a"
dependencies = [
"bitflags 2.5.0",
"bitflags",
"bytes",
"futures-core",
"futures-sink",
@ -29,7 +29,7 @@ dependencies = [
"actix-service",
"actix-utils",
"actix-web",
"bitflags 2.5.0",
"bitflags",
"bytes",
"derive_more",
"futures-core",
@ -44,9 +44,9 @@ dependencies = [
[[package]]
name = "actix-http"
version = "3.6.0"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d223b13fd481fc0d1f83bb12659ae774d9e3601814c68a0bc539731698cca743"
checksum = "4eb9843d84c775696c37d9a418bbb01b932629d01870722c0f13eb3f95e2536d"
dependencies = [
"actix-codec",
"actix-rt",
@ -54,7 +54,7 @@ dependencies = [
"actix-utils",
"ahash",
"base64",
"bitflags 2.5.0",
"bitflags",
"brotli",
"bytes",
"bytestring",
@ -88,18 +88,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
name = "actix-router"
version = "0.5.2"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d22475596539443685426b6bdadb926ad0ecaefdfc5fb05e5e3441f15463c511"
checksum = "13d324164c51f63867b57e73ba5936ea151b8a41a1d23d1031eeb9f70d0236f8"
dependencies = [
"bytestring",
"cfg-if",
"http",
"regex",
"regex-lite",
"serde",
"tracing",
]
@ -154,9 +156,9 @@ dependencies = [
[[package]]
name = "actix-web"
version = "4.5.1"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a6556ddebb638c2358714d853257ed226ece6023ef9364f23f0c70737ea984"
checksum = "b1cf67dadb19d7c95e5a299e2dda24193b89d5d4f33a3b9800888ede9e19aa32"
dependencies = [
"actix-codec",
"actix-http",
@ -183,6 +185,7 @@ dependencies = [
"once_cell",
"pin-project-lite",
"regex",
"regex-lite",
"serde",
"serde_json",
"serde_urlencoded",
@ -201,14 +204,14 @@ dependencies = [
"actix-router",
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
name = "addr2line"
version = "0.21.0"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
dependencies = [
"gimli",
]
@ -328,9 +331,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "backtrace"
version = "0.3.71"
version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11"
dependencies = [
"addr2line",
"cc",
@ -343,15 +346,9 @@ dependencies = [
[[package]]
name = "base64"
version = "0.21.7"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
@ -370,9 +367,9 @@ dependencies = [
[[package]]
name = "brotli"
version = "3.5.0"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d640d25bc63c50fb1f0b545ffd80207d2e10a4c965530809b40ba3386825c391"
checksum = "74f7971dbd9326d58187408ab83117d8ac1bb9c17b085fdacd1cf2f598719b6b"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
@ -381,9 +378,9 @@ dependencies = [
[[package]]
name = "brotli-decompressor"
version = "2.5.1"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e2e4afe60d7dd600fdd3de8d0f08c2b7ec039712e3b6137ff98b7004e82de4f"
checksum = "9a45bd2e4095a8b518033b128020dd4a55aab1c0a381ba4404a472630f4bc362"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
@ -422,9 +419,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.97"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f"
dependencies = [
"jobserver",
"libc",
@ -454,9 +451,9 @@ dependencies = [
[[package]]
name = "chrono-tz"
version = "0.8.6"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59ae0466b83e838b81a54256c39d5d7c20b9d7daa10510a242d9b75abd5936e"
checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb"
dependencies = [
"chrono",
"chrono-tz-build",
@ -465,9 +462,9 @@ dependencies = [
[[package]]
name = "chrono-tz-build"
version = "0.2.1"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "433e39f13c9a060046954e0592a8d0a4bcb1040125cbf91cb8ee58964cfb350f"
checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1"
dependencies = [
"parse-zoneinfo",
"phf",
@ -532,9 +529,9 @@ dependencies = [
[[package]]
name = "crc32fast"
version = "1.4.0"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
@ -560,9 +557,9 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.19"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crypto-common"
@ -608,6 +605,7 @@ version = "2.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff236accb9a5069572099f0b350a92e9560e8e63a9b8d546162f4a5e03026bb2"
dependencies = [
"chrono",
"diesel_derives",
"libsqlite3-sys",
"time",
@ -622,7 +620,7 @@ dependencies = [
"diesel_table_macro_syntax",
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
@ -631,7 +629,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
dependencies = [
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
@ -766,9 +764,9 @@ dependencies = [
[[package]]
name = "gimli"
version = "0.28.1"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "globset"
@ -785,11 +783,11 @@ dependencies = [
[[package]]
name = "globwalk"
version = "0.8.1"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc"
checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"ignore",
"walkdir",
]
@ -966,9 +964,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.154"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "libm"
@ -978,9 +976,9 @@ checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]]
name = "libsqlite3-sys"
version = "0.22.2"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290b64917f8b0cb885d9de0f9959fe1f775d7fa12f1da2db9001c1c8ab60f89d"
checksum = "0c10584274047cb335c23d3e61bcef8e323adae7c5c8c760540f73610177fc3f"
dependencies = [
"pkg-config",
"vcpkg",
@ -1043,9 +1041,9 @@ dependencies = [
[[package]]
name = "miniz_oxide"
version = "0.7.2"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae"
dependencies = [
"adler",
]
@ -1079,9 +1077,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.32.2"
version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e"
dependencies = [
"memchr",
]
@ -1094,9 +1092,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "parking_lot"
version = "0.12.2"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
@ -1167,7 +1165,7 @@ dependencies = [
"pest_meta",
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
@ -1251,9 +1249,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.82"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6"
dependencies = [
"unicode-ident",
]
@ -1303,7 +1301,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e"
dependencies = [
"bitflags 2.5.0",
"bitflags",
]
[[package]]
@ -1329,6 +1327,12 @@ dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-lite"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b661b2f27137bdbc16f00eda72866a92bb28af1753ffbd56744fb6e2e9cd8e"
[[package]]
name = "regex-syntax"
version = "0.8.3"
@ -1379,22 +1383,22 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]]
name = "serde"
version = "1.0.201"
version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c"
checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.201"
version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865"
checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
@ -1511,9 +1515,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.63"
version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",
@ -1522,9 +1526,9 @@ dependencies = [
[[package]]
name = "tera"
version = "1.19.1"
version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "970dff17c11e884a4a09bc76e3a17ef71e01bb13447a11e85226e254fe6d10b8"
checksum = "ab9d851b45e865f178319da0abdbfe6acbc4328759ff18dafc3a41c16b4cd2ee"
dependencies = [
"chrono",
"chrono-tz",
@ -1544,22 +1548,22 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.60"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18"
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.60"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524"
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]
@ -1832,7 +1836,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
"wasm-bindgen-shared",
]
@ -1854,7 +1858,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -2039,7 +2043,7 @@ checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
"syn 2.0.66",
]
[[package]]

View file

@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = { version = "*", features = ["serde"] }
chrono = { version = "0.4", features = ["serde"] }
actix-web = "4.5.1"
actix-files = "0.6.5"
@ -15,7 +15,7 @@ serde = { version = "1.0.201", features = ["derive"] }
serde_json = "*"
serde_derive = "*"
diesel = { version = "2.1.0", default-features = false, features = ["sqlite"] }
diesel = { version = "2.1.0", default-features = false, features = ["sqlite", "chrono"] }
dotenvy = "0.15"
uuid = { version = "1.8.0", features = ["serde", "v5"] }

View file

Before

Width:  |  Height:  |  Size: 318 B

After

Width:  |  Height:  |  Size: 318 B

View file

Before

Width:  |  Height:  |  Size: 614 B

After

Width:  |  Height:  |  Size: 614 B

View file

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

View file

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 8.9 KiB

View file

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="{{ username }}' blog" />
<meta property="og:description" content="Last 5 posts" />
<meta property="og:image" content="/static/site-image.png" />
<title> {{ username }}' blog </title>
<link rel="stylesheet" href="/static/css/blog.css">
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" />
</head>
<body>
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1>
<p style="text-align: right">
<a href="/about">About</a>
<a href="/all">All Posts</a>
</p>
<ul>
{% for post in posts %}
<article>
<div>
<a href="/id/{{ post.id }}" class="post-link">[link]</a>
</div>
<div class="post-content">
<h2 class="post-title">{{ post.title }}</h2>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ post.body }}</p>
</div>
</article>
{% endfor %}
</ul>
</body>
</html>

View file

@ -1,72 +1,83 @@
use std::sync::Mutex;
use dotenvy::dotenv;
use once_cell::sync::Lazy;
struct Config {
pub const ENV_PREFIX: &str = "CL_";
pub struct Config {
pub submit_token: String,
pub root_path: String,
pub username: String,
pub email: String,
pub bind_port: String,
pub accounts: Accounts
pub accounts: Accounts,
}
struct Accounts {
pub github: Option<String>,
pub twitter: Option<String>,
pub mastodon: Option<String>,
pub discord: Option<String>,
pub reddit: Option<String>,
pub struct Accounts {
pub github: Option<String>,
pub twitter: Option<String>,
pub mastodon: Option<String>,
pub discord: Option<String>,
pub reddit: Option<String>,
}
fn load_config() -> Config {
dotenv().expect(".env file not found");
// return config value or panic if not set
let eval_required_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
fn eval_required_conf(variable_name: String) -> String {
match std::env::var(variable_name.clone()) {
Ok(value) => {
println!("{}: {}", variable_name, value);
return value;
},
}
Err(_) => {
panic!("{} not set!", variable_name)
}
}
};
}
// return optional value
let eval_optional_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
fn eval_optional_conf(variable_name: String, default_value: Option<&str>) -> Option<String> {
match std::env::var(variable_name.clone()) {
Ok(value) => {
println!("{}: {}", variable_name, value);
return Some(value);
},
Err(_) => {
return None
}
Err(_) => match default_value {
Some(val) => {
println!("Variable {variable_name} not set. Using default value: {val}.");
return Some(String::from(val));
}
None => {
println!("Variable {variable_name} not set. No default, leaving this empty.");
None
}
},
}
};
}
fn eval_conf_var(name: &str, required_var: bool, default: Option<&str>) -> Option<String> {
if required_var {
Some(eval_required_conf(format!("{ENV_PREFIX}{name}")))
} else {
eval_optional_conf(format!("{ENV_PREFIX}{name}"), default)
}
}
Config {
submit_token: eval_required_conf("SUBMIT_TOKEN"),
root_path: eval_required_conf("ROOT_PATH"),
username: eval_required_conf("USERNAME"),
email: eval_required_conf("EMAIL"),
bind_port: eval_required_conf("BIND_PORT"),
submit_token: eval_conf_var("SUBMIT_TOKEN", true, None).unwrap(),
root_path: eval_conf_var("ROOT_PATH", false, Some("./content")).unwrap(),
username: eval_conf_var("USERNAME", true, None).unwrap(),
email: eval_conf_var("EMAIL", false, None).unwrap(),
bind_port: eval_conf_var("BIND_PORT", false, Some("8000")).unwrap(),
accounts: Accounts {
github: eval_optional_conf("GITHUB_ACCOUNT"),
discord: eval_optional_conf("DISCORD_ACCOUNT"),
twitter: eval_optional_conf("TWITTER_ACCOUNT"),
mastodon: eval_optional_conf("MASTODON_ACCOUNT"),
reddit: eval_optional_conf("REDDIT_ACCOUNT"),
}
github: eval_conf_var("GITHUB_ACCOUNT", false, None),
discord: eval_conf_var("DISCORD_ACCOUNT", false, None),
twitter: eval_conf_var("TWITTER_ACCOUNT", false, None),
mastodon: eval_conf_var("MASTODON_ACCOUNT", false, None),
reddit: eval_conf_var("REDDIT_ACCOUNT", false, None),
},
}
}
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
load_config()
});
pub static CONFIG: Lazy<Config> = Lazy::new(|| load_config());

View file

@ -4,11 +4,12 @@ mod schema;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use models::*;
use std::env;
use crate::config::CONFIG;
/// Returns an SqliteConnection if connection successful.
fn establish_connection() -> SqliteConnection {
let db_path = env::var("ROOT_PATH").unwrap() + "/db.sqlite3";
let db_path = CONFIG.root_path.clone() + "/db.sqlite3";
SqliteConnection::establish(&db_path)
.unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path))
}
@ -16,33 +17,33 @@ fn establish_connection() -> SqliteConnection {
/// Returns all posts
pub fn get_all_posts() -> std::vec::Vec<Post> {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
posts
.filter(published.eq(true))
.order(id.desc())
.load::<Post>(&connection)
.load::<Post>(&mut connection)
.expect("Error, couldn't load posts.")
}
/// Returns the last five posts.
pub fn get_last_five_posts() -> std::vec::Vec<Post> {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
posts
.filter(published.eq(true))
.order(id.desc())
.limit(5)
.load::<Post>(&connection)
.load::<Post>(&mut connection)
.expect("Error, couldn't load posts.")
}
/// Returns the post with the given ID.
pub fn get_post_by_id(post_id: i32) -> Post {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
posts
.find(post_id)
.get_result(&connection)
.get_result(&mut connection)
.expect("Error, couldn't find post.")
}
@ -51,7 +52,7 @@ pub fn create_post(title: &str, body: &str) {
use chrono::prelude::*;
use schema::posts;
let connection = establish_connection();
let mut connection = establish_connection();
let new_post = NewPost {
title,
@ -62,40 +63,40 @@ pub fn create_post(title: &str, body: &str) {
diesel::insert_into(posts::table)
.values(&new_post)
.execute(&connection)
.execute(&mut connection)
.unwrap_or_else(|_| panic!("Error, couldn't insert new Post."));
}
/// Updates a post with the new title and body.
pub fn edit_post_by_id(post_id: i32, new_title: &str, new_body: &str) {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
diesel::update(posts)
.filter(id.eq(post_id))
.set((title.eq(new_title), body.eq(new_body)))
.execute(&connection)
.execute(&mut connection)
.expect("Error, couldn't update post.");
}
/// Deletes a post by id.
pub fn delete_post_by_id(post_id: i32) {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
diesel::delete(posts.filter(id.eq(post_id)))
.execute(&connection)
.execute(&mut connection)
.expect("Error, couldn't update post.");
}
/// Sets the published bool of a post to false.
pub fn hide_post_by_id(post_id: i32) {
use schema::posts::dsl::*;
let connection = establish_connection();
let mut connection = establish_connection();
diesel::update(posts)
.filter(id.eq(post_id))
.set(published.eq(false))
.execute(&connection)
.execute(&mut connection)
.expect("Error, couldn't update post.");
}

View file

@ -1,6 +1,7 @@
use super::schema::posts;
use crate::db::schema::posts;
use serde::{Deserialize, Serialize};
#[derive(Queryable, Serialize, Deserialize)]
pub struct Post {
pub id: i32,
@ -11,7 +12,7 @@ pub struct Post {
}
#[derive(Insertable)]
#[table_name = "posts"]
#[diesel(table_name = posts)]
pub struct NewPost<'a> {
pub title: &'a str,
pub body: &'a str,

View file

@ -1,7 +1,7 @@
mod api;
mod config;
mod db;
mod routes;
mod config;
#[macro_use]
extern crate diesel;
@ -11,23 +11,15 @@ extern crate tera;
use actix_files as fs;
use actix_web::{middleware::Logger, web::Data, App, HttpServer};
use std::env;
use config::CONFIG;
use env_logger::Env;
use tera::Tera;
use config::CONFIG;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let mut tera = Tera::new(
format!(
"{}{}",
env::var("ROOT_PATH").unwrap(),
"/templates/*"
)
.as_str(),
)
.unwrap();
let mut tera =
Tera::new(format!("{}{}", CONFIG.root_path, "/templates/*").as_str()).unwrap();
tera.autoescape_on(vec![".sql"]);
env_logger::Builder::from_env(Env::default().default_filter_or("info"));
@ -48,11 +40,7 @@ async fn main() -> std::io::Result<()> {
.service(api::blog_delete_post)
.service(fs::Files::new(
"/static",
format!(
"{}{}",
CONFIG.root_path,
"/static"
),
format!("{}{}", CONFIG.root_path, "/static"),
))
.wrap(Logger::new("%a %r %t"))
})

13
site/src/models.rs Normal file
View file

@ -0,0 +1,13 @@
use diesel::prelude::*;
use chrono;
#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::posts)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Post {
pub id: i32,
pub title: String,
pub body: String,
pub published: bool,
pub publish_date: chrono::Timestamp
}