-
Notifications
You must be signed in to change notification settings - Fork 0
[TS-2073] Server implementation for tooling interop #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a18a6b
feat: impl json-rpc types, routing, deserializers
ethangreen-dev 09d8942
refactor methods into submodule, impl simple msg loop
ethangreen-dev 074526b
implement primitive file-based project lock
ethangreen-dev 6d985ae
Merge pull request #12 from thunderstore-io/TS-2073/server/lock
ethangreen-dev f4fbf19
refactor error variants to be more module-specific
ethangreen-dev 9300694
Merge pull request #13 from thunderstore-io/error-refactor
ethangreen-dev 55c2648
implement minimal suite of server methods
ethangreen-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ mod error; | |
mod game; | ||
mod package; | ||
mod project; | ||
mod server; | ||
mod ts; | ||
mod ui; | ||
mod util; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::fs::{self, File}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
/// Only one server process can "own" a project at a single time. | ||
/// We enforce this exclusive access through the creation and deletion of this lockfile. | ||
const LOCKFILE: &'static str = ".server-lock"; | ||
|
||
pub struct ProjectLock { | ||
file: File, | ||
path: PathBuf, | ||
} | ||
|
||
impl ProjectLock { | ||
/// Attempt to acquire a lock on the provided directory. | ||
pub fn lock(project_path: &Path) -> Option<Self> { | ||
let lock = project_path.join(LOCKFILE); | ||
match lock.is_file() { | ||
true => None, | ||
false => { | ||
let file = File::create(&lock).ok()?; | ||
Some(Self { file, path: lock }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Drop for ProjectLock { | ||
fn drop(&mut self) { | ||
// The file handle is dropped before this, so we can safely delete it. | ||
fs::remove_file(&self.path).unwrap(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use tempfile::TempDir; | ||
|
||
/// Test that project locks behave in the following way: | ||
/// - Attempting to lock an already locked project MUST return None. | ||
/// - Attempting to lock a project that is not locked will return a ProjectLock. | ||
/// - Dropping a ProjectLock will remove the lockfile from the project. | ||
#[test] | ||
fn test_project_lock() { | ||
let temp = TempDir::new().unwrap(); | ||
let root = temp.path(); | ||
|
||
// Acquire a lock on the directory. | ||
{ | ||
let _lock = | ||
ProjectLock::lock(root).expect("Failed to acquire lock on empty directory."); | ||
|
||
// Attempting to acquire it again will return None. | ||
let relock = ProjectLock::lock(root); | ||
assert!(relock.is_none()); | ||
} | ||
|
||
// Now that the previous lock is dropped we *should* be able to re-acquire it. | ||
let _lock = ProjectLock::lock(root).expect("Failed to acquire lock on empty directory."); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
pub mod package; | ||
pub mod project; | ||
|
||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use self::package::PackageMethod; | ||
use self::project::ProjectMethod; | ||
use super::Error; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub enum Method { | ||
Exit, | ||
Project(ProjectMethod), | ||
Package(PackageMethod), | ||
} | ||
|
||
/// Method namespace registration. | ||
impl Method { | ||
pub fn from_value(method: &str, value: serde_json::Value) -> Result<Self, Error> { | ||
let mut split = method.split('/'); | ||
let (namespace, name) = ( | ||
split | ||
.next() | ||
.ok_or_else(|| Error::InvalidMethod(method.into()))?, | ||
split | ||
.next() | ||
.ok_or_else(|| Error::InvalidMethod(method.into()))?, | ||
); | ||
|
||
// Route namespaces to the appropriate enum variants for construction. | ||
Ok(match namespace { | ||
"exit" => Self::Exit, | ||
"project" => Self::Project(ProjectMethod::from_value(name, value)?), | ||
"package" => Self::Package(PackageMethod::from_value(name, value)?), | ||
x => Err(Error::InvalidMethod(x.into()))?, | ||
}) | ||
} | ||
} | ||
|
||
pub fn parse_value<T: DeserializeOwned>(value: serde_json::Value) -> Result<T, serde_json::Error> { | ||
serde_json::from_value(value) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::Error; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub enum PackageMethod { | ||
/// Get metadata about this package. | ||
GetMetadata, | ||
/// Determine if the package exists within the cache. | ||
IsCached, | ||
} | ||
|
||
impl PackageMethod { | ||
pub fn from_value(method: &str, value: serde_json::Value) -> Result<Self, Error> { | ||
|
||
Ok(match method { | ||
"get_metadata" => Self::GetMetadata, | ||
"is_cached" => Self::IsCached, | ||
x => Err(Error::InvalidMethod(x.into()))?, | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.