Initial database setup and flawed md parser

This commit is contained in:
Rolf Martin Glomsrud 2023-09-23 21:28:12 +02:00
parent e74990b2ac
commit 79ff1b09f3
8 changed files with 225 additions and 80 deletions

14
docker-compose.yml Normal file
View file

@ -0,0 +1,14 @@
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: localdb
PGDATA: /data/postgres
ports:
- 5432:5432

View file

@ -0,0 +1 @@
DATABASE_URL=postgres://postgres:localdb@localhost:5432/postgres

View file

@ -401,6 +401,26 @@ dependencies = [
"subtle",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "dotenvy"
version = "0.15.7"
@ -467,18 +487,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6"
[[package]]
name = "flume"
version = "0.10.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577"
dependencies = [
"futures-core",
"futures-sink",
"pin-project",
"spin 0.9.8",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -510,17 +518,6 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-executor"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-intrusive"
version = "0.4.2"
@ -885,17 +882,6 @@ version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "libsqlite3-sys"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "898745e570c7d0453cc1fbc4a701eb6c662ed54e8fec8b7d14be137ebeeb9d14"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.7"
@ -918,6 +904,15 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "md-5"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
dependencies = [
"digest",
]
[[package]]
name = "memchr"
version = "2.6.3"
@ -1085,26 +1080,6 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
[[package]]
name = "pin-project"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]]
name = "pin-project-lite"
version = "0.2.13"
@ -1117,12 +1092,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "poem"
version = "1.3.58"
@ -1315,6 +1284,17 @@ dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall 0.2.16",
"thiserror",
]
[[package]]
name = "regex"
version = "1.9.5"
@ -1556,9 +1536,6 @@ name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "sqlformat"
@ -1589,33 +1566,39 @@ checksum = "fa8241483a83a3f33aa5fff7e7d9def398ff9990b2752b6c6112b83c6d246029"
dependencies = [
"ahash 0.7.6",
"atoi",
"base64 0.13.1",
"bitflags 1.3.2",
"byteorder",
"bytes",
"crc",
"crossbeam-queue",
"dirs",
"dotenvy",
"either",
"event-listener",
"flume",
"futures-channel",
"futures-core",
"futures-executor",
"futures-intrusive",
"futures-util",
"hashlink",
"hex",
"hkdf",
"hmac",
"indexmap 1.6.2",
"itoa",
"libc",
"libsqlite3-sys",
"log",
"md-5",
"memchr",
"once_cell",
"paste",
"percent-encoding",
"rand",
"rustls",
"rustls-pemfile",
"serde",
"serde_json",
"sha1",
"sha2",
"smallvec",
"sqlformat",
@ -1625,6 +1608,7 @@ dependencies = [
"tokio-stream",
"url",
"webpki-roots",
"whoami",
]
[[package]]
@ -1978,12 +1962,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"
@ -2088,6 +2066,16 @@ dependencies = [
"webpki",
]
[[package]]
name = "whoami"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "winapi"
version = "0.3.9"

View file

@ -9,4 +9,4 @@ edition = "2021"
poem = "1.2"
poem-openapi = { version = "1.2", features = ["swagger-ui"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
sqlx = { version = "0.6.0", features = ["runtime-tokio-rustls", "sqlite"] }
sqlx = { version = "0.6.0", features = ["runtime-tokio-rustls", "postgres"] }

View file

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS articles(
id SERIAL PRIMARY KEY,
markdown TEXT,
name TEXT
);

View file

@ -1,28 +1,68 @@
use poem::{listener::TcpListener, Route};
mod mdtransform;
use mdtransform::MdTransform;
use poem::{
error::BadRequest, listener::TcpListener, middleware::AddData, web::Data, EndpointExt, Route,
};
use poem_openapi::{param::Query, payload::PlainText, OpenApi, OpenApiService};
use sqlx::SqlitePool;
use sqlx::{postgres::PgPoolOptions, Error, Pool, Postgres};
use tokio::{
fs::{self, File},
sync::Mutex,
};
struct Api;
struct Api {
pool: Mutex<Pool<Postgres>>,
}
#[OpenApi]
impl Api {
#[oai(path = "/hello", method = "get")]
async fn index(&self, name: Query<Option<String>>) -> PlainText<String> {
match name.0 {
Some(name) => PlainText(format!("hello, {}!", name)),
Some(name) => {
let pool = &self.pool.lock().await;
insertSomething(pool, &name).await;
return PlainText(format!("Inserted article with name: {}", name).to_string());
}
None => PlainText("hello!".to_string()),
}
}
}
async fn insertSomething(pool: &Pool<Postgres>, name: &String) {
sqlx::query!(
"INSERT INTO articles (markdown, name) VALUES ($1, $2);",
"ARTICLE HERE",
name
)
.execute(pool)
.await
.unwrap();
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let api_service =
OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://postgres:localdb@localhost:5432/postgres")
.await
.unwrap();
let api_service = OpenApiService::new(
Api {
pool: Mutex::new(pool),
},
"Hello World",
"1.0",
)
.server("http://localhost:3000/api");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/api", api_service).nest("/", ui);
let pool = SqlitePool::connect("db.db");
let file = fs::read_to_string("test.md").await.unwrap();
println!("{}", MdTransform::transform(file));
println!("HELLO!");
poem::Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await

View file

@ -0,0 +1,61 @@
pub struct MdTransform;
impl MdTransform {
pub fn transform(markdown: String) -> String {
let mut lines = markdown
.split("\n")
.map(|x| x.to_string().chars().collect::<Vec<char>>())
.collect::<Vec<Vec<char>>>();
Self::trans(&mut lines);
"".to_string()
}
fn trans(markdownLines: &mut Vec<Vec<char>>) {
let mut notation: Vec<Vec<char>> = Vec::new();
let mut mdLinesIter = markdownLines.into_iter();
while let Some(current) = mdLinesIter.next() {}
}
fn transformLine(line: &mut [char]) -> String {
match line {
['#', rest @ ..] => {
let mut nrOfHashtags = 0;
for i in rest {
if *i != '#' {
break;
}
nrOfHashtags += 1;
}
Self::transformLine(&mut rest[nrOfHashtags..])
}
['-', '-', '-', rest @ ..] => {
let validLine = rest.iter().fold(true, |x, y| x && *y == '-');
if validLine {
"<hr>".to_string()
} else {
let mut res = "<p>---".to_string();
res.push_str(&rest.iter().collect::<String>());
res
}
}
['`', '`', '`', languageName @ ..] => {
let mut linesInBlock = Vec::new();
for z in mdLinesIter
.by_ref()
.take_while(|x| !matches!(x.as_slice(), ['`', '`', '`', ..]))
{
linesInBlock.push(z);
}
println!("Found codeblock with contents {:?}", linesInBlock)
}
[regularText @ ..] => {}
_ => {}
}
}
}

View file

@ -0,0 +1,36 @@
# H1
## H2
### H3
#### H4
##### H5
Regular Text
> BlockQuote
1. Item 1
2. Item 2
3. Item 3
- first item
- second item
`code`
```
Code Block
Contents is here
Mega
Pog!
```
---
[Link](https://google.com)
![Image_text](image.jpg)