generated from lucien/actix-react-template
Added database init function
This commit is contained in:
parent
6461ebc629
commit
902e96b205
4 changed files with 13 additions and 8 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
back/target/
|
back/target/
|
||||||
front/dist/
|
front/dist/
|
||||||
back/public/
|
back/public/
|
||||||
|
back/data/
|
||||||
back/Cargo.lock
|
back/Cargo.lock
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -1,15 +1,16 @@
|
||||||
use sqlite::{Connection, State};
|
use sqlite::{Connection, State, Error};
|
||||||
|
|
||||||
fn main() -> sqlite::Result<()> {
|
pub fn init() -> sqlite::Result<()> {
|
||||||
let conn = Connection::open("./data/data.db")?;
|
let conn = Connection::open("./data/data.db")?;
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE articles (
|
"CREATE TABLE IF NOT EXISTS articles (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
subTitle TEXT,
|
subTitle TEXT,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL
|
||||||
)",
|
)",
|
||||||
[],
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
|
@ -3,6 +3,9 @@ use actix_files::Files;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use sqlite::{Connection, State, Error};
|
use sqlite::{Connection, State, Error};
|
||||||
|
|
||||||
|
mod create_db;
|
||||||
|
use create_db::init;
|
||||||
|
|
||||||
#[get("/api/hello")]
|
#[get("/api/hello")]
|
||||||
async fn hello() -> impl Responder {
|
async fn hello() -> impl Responder {
|
||||||
HttpResponse::Ok().body("Hello world!")
|
HttpResponse::Ok().body("Hello world!")
|
||||||
|
@ -45,7 +48,7 @@ async fn api() -> impl Responder {
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let conn = Connection::open("./data/data.db");
|
init();
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue