SurrealDB

This commit is contained in:
2024-06-25 23:26:46 -04:00
Unverified
parent bf3162c590
commit b24d5b20e0
3 changed files with 22 additions and 2 deletions

View File

@@ -11,3 +11,5 @@ tokio = { version = "1.0", features = ["macros", "net", "rt-multi-thread"] }
tracing = "0.1.40"
clap = { version = "4.5.7", features = ["derive"] }
serde = { version = "1.0.203", features = ["derive"] }
emojis = "0.6.2"
surrealdb = "1.5.3"

View File

@@ -1,7 +1,11 @@
use std::future::Future;
use std::net::{IpAddr, Ipv6Addr};
use std::sync::Arc;
use futures::future::{self};
use futures::StreamExt;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::Surreal;
use tarpc::{
server::{Channel},
tokio_serde::formats::Json,
@@ -17,6 +21,18 @@ async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Connect to the server
let db = Arc::new(Surreal::new::<Ws>("127.0.0.1:8000").await?);
// Signin as a namespace, database, or root user
db.signin(Root {
username: "root",
password: "root",
}).await?;
// Select a specific namespace / database
db.use_ns("realmchat").use_db("test").await?;
let server_addr = (IpAddr::V6(Ipv6Addr::LOCALHOST), 5051);
// JSON transport is provided by the json_transport tarpc module. It makes it easy
@@ -33,7 +49,7 @@ async fn main() -> anyhow::Result<()> {
// serve is generated by the service attribute. It takes as input any type implementing
// the generated World trait.
.map(|channel| {
let server = RealmChatServer(channel.transport().peer_addr().unwrap());
let server = RealmChatServer::new(channel.transport().peer_addr().unwrap(), db.clone());
channel.execute(server.serve()).for_each(spawn)
})
// Max 10 channels.

2
server/surrealdb.sh Executable file
View File

@@ -0,0 +1,2 @@
docker run --rm --pull always --name surrealdb -p 8000:8000 surrealdb/surrealdb:latest start --log trace --user root --pass root memory