diff options
Diffstat (limited to 'src/bin/server')
| -rw-r--r-- | src/bin/server/database.rs | 5 | ||||
| -rw-r--r-- | src/bin/server/handler.rs | 6 |
2 files changed, 6 insertions, 5 deletions
diff --git a/src/bin/server/database.rs b/src/bin/server/database.rs index 1653071..c73aec5 100644 --- a/src/bin/server/database.rs +++ b/src/bin/server/database.rs | |||
| @@ -1,8 +1,6 @@ | |||
| 1 | use rusqlite::Connection; | 1 | use rusqlite::Connection; |
| 2 | 2 | ||
| 3 | pub fn init_db() -> Connection { | 3 | pub fn init_db(conn: &Connection) { |
| 4 | let db_path = std::env::var("SERVER_DB").unwrap_or_else(|_| "server.db".to_string()); | ||
| 5 | let conn = Connection::open(&db_path).unwrap(); | ||
| 6 | conn.execute( | 4 | conn.execute( |
| 7 | "CREATE TABLE IF NOT EXISTS users ( | 5 | "CREATE TABLE IF NOT EXISTS users ( |
| 8 | username TEXT PRIMARY KEY, | 6 | username TEXT PRIMARY KEY, |
| @@ -19,7 +17,6 @@ pub fn init_db() -> Connection { | |||
| 19 | "ALTER TABLE users ADD COLUMN balance INTEGER DEFAULT 1000", | 17 | "ALTER TABLE users ADD COLUMN balance INTEGER DEFAULT 1000", |
| 20 | [], | 18 | [], |
| 21 | ); | 19 | ); |
| 22 | conn | ||
| 23 | } | 20 | } |
| 24 | 21 | ||
| 25 | pub fn add_user( | 22 | pub fn add_user( |
diff --git a/src/bin/server/handler.rs b/src/bin/server/handler.rs index 867910e..48e9d3a 100644 --- a/src/bin/server/handler.rs +++ b/src/bin/server/handler.rs | |||
| @@ -8,7 +8,11 @@ use std::io::{BufRead, BufReader, Write}; | |||
| 8 | use std::net::TcpStream; | 8 | use std::net::TcpStream; |
| 9 | use std::sync::Mutex; | 9 | use std::sync::Mutex; |
| 10 | 10 | ||
| 11 | static DB_CONN: Lazy<Mutex<Connection>> = Lazy::new(|| Mutex::new(database::init_db())); | 11 | static DB_CONN: Lazy<Mutex<Connection>> = Lazy::new(|| { |
| 12 | let conn = Connection::open(std::env::var("SERVER_DB").unwrap_or_else(|_| "server.db".to_string())).unwrap(); | ||
| 13 | database::init_db(&conn); | ||
| 14 | Mutex::new(conn) | ||
| 15 | }); | ||
| 12 | 16 | ||
| 13 | #[allow(dead_code)] | 17 | #[allow(dead_code)] |
| 14 | pub fn handle_client(stream: TcpStream) { | 18 | pub fn handle_client(stream: TcpStream) { |
