diff --git a/src/bytecode_compiler.rs b/src/bytecode_compiler.rs index 92146ec..0b2930d 100644 --- a/src/bytecode_compiler.rs +++ b/src/bytecode_compiler.rs @@ -26,14 +26,6 @@ pub(crate) fn compile_function( ) -> Result { let fn_name = &function.name.lexeme; let mut compiler = Compiler::new(fn_name); - if is_http_method(fn_name) { - compiler.chunk.add_var(&TokenType::StringType, "path"); - compiler.chunk.add_var(&TokenType::MapType, "query"); - compiler.chunk.add_var(&TokenType::MapType, "headers"); - compiler.vars.insert("path".to_string(), 0); - compiler.vars.insert("query".to_string(), 1); - compiler.vars.insert("headers".to_string(), 2); - } for parm in &function.parameters { let name = parm.name.lexeme.clone(); let var_index = compiler.chunk.add_var(&parm.var_type, &parm.name.lexeme); @@ -44,10 +36,6 @@ pub(crate) fn compile_function( Ok(compiler.compile(&function.body, registry, namespace)?) } -fn is_http_method(name: &str) -> bool { - vec!["get", "post", "put", "delete", "patch"].contains(&name) -} - pub(crate) fn compile_in_namespace( ast: &Vec, namespace: Option<&str>, diff --git a/src/main.rs b/src/main.rs index 94b044f..a52c66e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,9 @@ async fn main() -> Result<(), crudlang::errors::Error> { axum::serve(listener, app).await.map_err(map_underlying())?; Ok(()) } else { - Err(Platform("No source files found or compilation error".to_string())) + Err(Platform( + "No source files found or compilation error".to_string(), + )) } } @@ -62,16 +64,31 @@ async fn handle_any( .collect() }) .unwrap_or_default(); - let component = format!("{}/web.{}", &uri.path()[1..], method); + let component = format!("{}/web", &uri.path()[1..]); + let function_qname = format!("{}.{}", component, method); let mut headers = HashMap::new(); - for (k,v) in req.headers().iter(){ + for (k, v) in req.headers().iter() { headers.insert(k.to_string(), v.to_str().unwrap().to_string()); } - Ok(Json( - interpret_async(&state.registry, &component, &req.uri().to_string(), query_params, headers) - .await - .unwrap() - .to_string(), - )) + let path = &req.uri().to_string(); + match interpret_async( + &state.registry, + &function_qname, + path, + query_params, + headers, + ) + .await + { + Ok(value) => Ok(Json(value.to_string())), + Err(e) => { + // url checks out but function for method not found + if state.registry.get(&format!("{}.main",component)).is_some() { + Err(StatusCode::METHOD_NOT_ALLOWED) + } else { + Err(StatusCode::NOT_FOUND) + } + } + } }