@@ -32,6 +32,19 @@ pub struct StartNodeRequest {
3232 pub config : Option < serde_json:: Value > ,
3333}
3434
35+ /// Validate node ID format (alphanumeric, hyphens, and underscores only)
36+ fn validate_node_id ( id : & str ) -> Result < ( ) , ( StatusCode , Json < ErrorResponse > ) > {
37+ if !id. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' ) {
38+ return Err ( (
39+ StatusCode :: BAD_REQUEST ,
40+ Json ( ErrorResponse {
41+ error : "Invalid node ID format" . to_string ( ) ,
42+ } ) ,
43+ ) ) ;
44+ }
45+ Ok ( ( ) )
46+ }
47+
3548/// List all registered nodes
3649pub async fn list_nodes (
3750 State ( state) : State < Arc < AppState > > ,
@@ -47,15 +60,7 @@ pub async fn get_node(
4760 State ( state) : State < Arc < AppState > > ,
4861 Path ( id) : Path < String > ,
4962) -> Result < Json < NodeResponse > , ( StatusCode , Json < ErrorResponse > ) > {
50- // Validate node ID format (alphanumeric and hyphens only)
51- if !id. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' ) {
52- return Err ( (
53- StatusCode :: BAD_REQUEST ,
54- Json ( ErrorResponse {
55- error : "Invalid node ID format" . to_string ( ) ,
56- } ) ,
57- ) ) ;
58- }
63+ validate_node_id ( & id) ?;
5964
6065 match state. process . get_node ( & id) {
6166 Some ( node) => Ok ( Json ( NodeResponse { node } ) ) ,
@@ -74,15 +79,7 @@ pub async fn start_node(
7479 Path ( id) : Path < String > ,
7580 Json ( req) : Json < StartNodeRequest > ,
7681) -> Result < Json < NodeResponse > , ( StatusCode , Json < ErrorResponse > ) > {
77- // Validate node ID format (alphanumeric and hyphens only)
78- if !id. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' ) {
79- return Err ( (
80- StatusCode :: BAD_REQUEST ,
81- Json ( ErrorResponse {
82- error : "Invalid node ID format" . to_string ( ) ,
83- } ) ,
84- ) ) ;
85- }
82+ validate_node_id ( & id) ?;
8683
8784 // Config is not supported yet
8885 if req. config . is_some ( ) {
@@ -113,15 +110,7 @@ pub async fn stop_node(
113110 State ( state) : State < Arc < AppState > > ,
114111 Path ( id) : Path < String > ,
115112) -> Result < Json < NodeResponse > , ( StatusCode , Json < ErrorResponse > ) > {
116- // Validate node ID format (alphanumeric and hyphens only)
117- if !id. chars ( ) . all ( |c| c. is_alphanumeric ( ) || c == '-' || c == '_' ) {
118- return Err ( (
119- StatusCode :: BAD_REQUEST ,
120- Json ( ErrorResponse {
121- error : "Invalid node ID format" . to_string ( ) ,
122- } ) ,
123- ) ) ;
124- }
113+ validate_node_id ( & id) ?;
125114
126115 match state. process . stop_node ( & id) {
127116 Ok ( node) => {
0 commit comments