basic SQLite database setup
This commit is contained in:
parent
8162f81a57
commit
5548e37234
5 changed files with 47 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
||||||
/target
|
/target
|
||||||
|
gunnhildr.db
|
||||||
|
gunnhildr.db-shm
|
||||||
|
gunnhildr.db-wal
|
|
@ -9,5 +9,5 @@ env_logger = "0.11.5"
|
||||||
figment = {version="0.10.19", features=["env"]}
|
figment = {version="0.10.19", features=["env"]}
|
||||||
log = "0.4.22"
|
log = "0.4.22"
|
||||||
serde = {version="1.0.215", features=["derive"]}
|
serde = {version="1.0.215", features=["derive"]}
|
||||||
sqlx = {version="0.8.2", features=["runtime-tokio"]}
|
sqlx = {version="0.8.2", features=["runtime-tokio","postgres", "sqlite"]}
|
||||||
tokio = "1.42.0"
|
tokio = "1.42.0"
|
||||||
|
|
|
@ -5,7 +5,7 @@ use figment::{
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize, Clone, Copy)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub binding_ip: IpAddr,
|
pub binding_ip: IpAddr,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
|
|
31
src/db/mod.rs
Normal file
31
src/db/mod.rs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use log::{info, warn};
|
||||||
|
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum DataBase {
|
||||||
|
Sqlite(sqlx::Pool<Sqlite>),
|
||||||
|
//Postgres(sqlx::Pool<Postgres>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DataBase {
|
||||||
|
pub async fn sqlite() -> Self {
|
||||||
|
if !sqlx::Sqlite::database_exists("sqlite:gunnhildr.db")
|
||||||
|
.await
|
||||||
|
.expect("failed to connect to db")
|
||||||
|
{
|
||||||
|
warn!("No SQLite database found, if this is the first time you are starting Gunnhildr, you can safely ignore this.");
|
||||||
|
sqlx::Sqlite::create_database("sqlite:gunnhildr.db")
|
||||||
|
.await
|
||||||
|
.expect("failed to create SQLite Database");
|
||||||
|
info!("Created new SQLite Database");
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = SqlitePool::connect("sqlite:gunnhildr.db").await.unwrap();
|
||||||
|
sqlx::migrate!("migrations/sqlite")
|
||||||
|
.run(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to apply migration!");
|
||||||
|
|
||||||
|
Self::Sqlite(pool)
|
||||||
|
}
|
||||||
|
}
|
15
src/main.rs
15
src/main.rs
|
@ -2,18 +2,25 @@ use actix_web::{get, App, HttpServer};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod db;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let config = config::parse_config();
|
let config = config::parse_config();
|
||||||
|
let db = db::DataBase::sqlite().await;
|
||||||
|
|
||||||
info!("Server starting...");
|
info!("Server starting...");
|
||||||
HttpServer::new(|| App::new().service(hello))
|
HttpServer::new(move || {
|
||||||
.bind((config.binding_ip, config.port))?
|
App::new()
|
||||||
.run()
|
.app_data(config)
|
||||||
.await
|
.app_data(db.clone())
|
||||||
|
.service(hello)
|
||||||
|
})
|
||||||
|
.bind((config.binding_ip, config.port))?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/hello")]
|
#[get("/hello")]
|
||||||
|
|
Loading…
Add table
Reference in a new issue