Skip to main content

surrealdb_server/rpc/
http.rs

1use std::sync::Arc;
2
3use surrealdb_core::dbs::Session;
4use surrealdb_core::kvs::Datastore;
5use surrealdb_core::rpc::{DbResult, Method, RpcProtocol, method_not_found};
6use surrealdb_types::{Array, Error as TypesError, HashMap, Value};
7use tokio::sync::RwLock;
8use uuid::Uuid;
9
10use crate::cnf::{PKG_NAME, PKG_VERSION};
11
12pub struct Http {
13	pub kvs: Arc<Datastore>,
14	pub sessions: HashMap<Option<Uuid>, Arc<RwLock<Session>>>,
15}
16
17impl Http {
18	pub fn new(kvs: Arc<Datastore>, session: Session) -> Self {
19		let http = Self {
20			kvs,
21			sessions: HashMap::new(),
22		};
23		// Store the default session with None key
24		http.sessions.insert(None, Arc::new(RwLock::new(session)));
25		http
26	}
27}
28
29impl RpcProtocol for Http {
30	/// The datastore for this RPC interface
31	fn kvs(&self) -> &Datastore {
32		&self.kvs
33	}
34
35	/// The version information for this RPC context
36	fn version_data(&self) -> DbResult {
37		let value = Value::String(format!("{PKG_NAME}-{}", *PKG_VERSION));
38		DbResult::Other(value)
39	}
40
41	/// A pointer to all active sessions
42	fn session_map(&self) -> &HashMap<Option<Uuid>, Arc<RwLock<Session>>> {
43		&self.sessions
44	}
45
46	// ------------------------------
47	// Realtime
48	// ------------------------------
49
50	/// Live queries are disabled on HTTP
51	const LQ_SUPPORT: bool = false;
52
53	/// Handles the cleanup of live queries
54	async fn cleanup_lqs(&self, _session_id: Option<&Uuid>) {
55		// Do nothing as HTTP is stateless
56	}
57
58	/// Handles the cleanup of live queries
59	async fn cleanup_all_lqs(&self) {
60		// Do nothing as HTTP is stateless
61	}
62
63	// ------------------------------
64	// Overrides
65	// ------------------------------
66
67	/// Transactions are not supported on HTTP RPC context
68	async fn begin(
69		&self,
70		_txn: Option<Uuid>,
71		_session_id: Option<Uuid>,
72	) -> Result<DbResult, TypesError> {
73		Err(method_not_found(Method::Begin.to_string()))
74	}
75
76	/// Transactions are not supported on HTTP RPC context
77	async fn commit(
78		&self,
79		_txn: Option<Uuid>,
80		_session_id: Option<Uuid>,
81		_params: Array,
82	) -> Result<DbResult, TypesError> {
83		Err(method_not_found(Method::Commit.to_string()))
84	}
85
86	/// Transactions are not supported on HTTP RPC context
87	async fn cancel(
88		&self,
89		_txn: Option<Uuid>,
90		_session_id: Option<Uuid>,
91		_params: Array,
92	) -> Result<DbResult, TypesError> {
93		Err(method_not_found(Method::Cancel.to_string()))
94	}
95}