titan-sdk 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+
2
+ [package]
3
+ name = "titan-server"
4
+ version = "0.1.0"
5
+ edition = "2024"
6
+
7
+ [dependencies]
8
+ axum = "0.8.7"
9
+ dotenv = "0.15.0"
10
+ reqwest = { version = "0.12.24", features = ["json", "rustls-tls", "gzip", "brotli", "blocking"] }
11
+ serde = { version = "1.0.228", features = ["derive"] }
12
+ serde_json = "1.0.145"
13
+ thiserror = "2.0.17"
14
+ tokio = { version = "1.48.0", features = ["rt-multi-thread", "macros", "process"] }
15
+ tower-http = { version = "0.6.7", features = ["cors"] }
16
+ tracing = "0.1.43"
17
+ tracing-subscriber = "0.3.22"
18
+ anyhow = "1"
19
+ v8 = "0.106.0"
20
+ dotenvy = "0.15"
21
+ base64 = "0.21"
22
+ regex = "1.10"
23
+ bcrypt = "0.15"
24
+ jsonwebtoken = "9"
25
+ postgres = { version = "0.19", features = ["with-serde_json-1"] }
26
+ libloading = "0.8"
27
+ walkdir = "2"
@@ -0,0 +1,131 @@
1
+ use std::collections::HashMap;
2
+ use std::env;
3
+ use std::path::{Path, PathBuf};
4
+ use serde::Deserialize;
5
+ use serde_json::Value;
6
+
7
+ /// Route configuration (loaded from routes.json)
8
+ #[derive(Debug, Deserialize, Clone)]
9
+ pub struct RouteVal {
10
+ pub r#type: String,
11
+ pub value: Value,
12
+ }
13
+
14
+ #[derive(Debug, Deserialize, Clone)]
15
+ pub struct DynamicRoute {
16
+ pub method: String,
17
+ pub pattern: String,
18
+ pub action: String,
19
+ }
20
+
21
+ // -------------------------
22
+ // ACTION DIRECTORY RESOLUTION
23
+ // -------------------------
24
+
25
+ pub fn resolve_actions_dir() -> PathBuf {
26
+ // Respect explicit override first
27
+ if let Ok(override_dir) = env::var("TITAN_ACTIONS_DIR") {
28
+ return PathBuf::from(override_dir);
29
+ }
30
+
31
+ // Production container layout
32
+ if Path::new("/app/actions").exists() {
33
+ return PathBuf::from("/app/actions");
34
+ }
35
+
36
+ // Try to walk up from the executing binary to discover `<...>/server/actions`
37
+ if let Ok(exe) = std::env::current_exe() {
38
+ if let Some(parent) = exe.parent() {
39
+ if let Some(target_dir) = parent.parent() {
40
+ if let Some(server_dir) = target_dir.parent() {
41
+ let candidate = server_dir.join("actions");
42
+ if candidate.exists() {
43
+ return candidate;
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ // Fall back to local ./actions
51
+ PathBuf::from("./actions")
52
+ }
53
+
54
+ /// Try to find the directory that contains compiled action bundles.
55
+ pub fn find_actions_dir(project_root: &PathBuf) -> Option<PathBuf> {
56
+ let candidates = [
57
+ project_root.join("server").join("actions"),
58
+ project_root.join("actions"),
59
+ project_root.join("..").join("server").join("actions"),
60
+ PathBuf::from("/app").join("actions"),
61
+ PathBuf::from("actions"),
62
+ ];
63
+
64
+ for p in &candidates {
65
+ if p.exists() && p.is_dir() {
66
+ return Some(p.clone());
67
+ }
68
+ }
69
+
70
+ None
71
+ }
72
+
73
+ // Dynamic Matcher (Core Logic)
74
+
75
+ pub fn match_dynamic_route(
76
+ method: &str,
77
+ path: &str,
78
+ routes: &[DynamicRoute],
79
+ ) -> Option<(String, HashMap<String, String>)> {
80
+ let path_segments: Vec<&str> =
81
+ path.trim_matches('/').split('/').collect();
82
+
83
+ for route in routes {
84
+ if route.method != method {
85
+ continue;
86
+ }
87
+
88
+ let pattern_segments: Vec<&str> =
89
+ route.pattern.trim_matches('/').split('/').collect();
90
+
91
+ if pattern_segments.len() != path_segments.len() {
92
+ continue;
93
+ }
94
+
95
+ let mut params = HashMap::new();
96
+ let mut matched = true;
97
+
98
+ for (pat, val) in pattern_segments.iter().zip(path_segments.iter()) {
99
+ if pat.starts_with(':') {
100
+ let inner = &pat[1..];
101
+
102
+ let (name, ty) = inner
103
+ .split_once('<')
104
+ .map(|(n, t)| (n, t.trim_end_matches('>')))
105
+ .unwrap_or((inner, "string"));
106
+
107
+ let valid = match ty {
108
+ "number" => val.parse::<i64>().is_ok(),
109
+ "string" => true,
110
+ _ => false,
111
+ };
112
+
113
+ if !valid {
114
+ matched = false;
115
+ break;
116
+ }
117
+
118
+ params.insert(name.to_string(), (*val).to_string());
119
+ } else if pat != val {
120
+ matched = false;
121
+ break;
122
+ }
123
+ }
124
+
125
+ if matched {
126
+ return Some((route.action.clone(), params));
127
+ }
128
+ }
129
+
130
+ None
131
+ }
@@ -0,0 +1,10 @@
1
+ use v8::JsError;
2
+
3
+ // A helper to Format v8 Errors
4
+ pub fn format_js_error(err: JsError, action: &str) -> String {
5
+ format!(
6
+ "Action: {}\n{}",
7
+ action,
8
+ err.to_string()
9
+ )
10
+ }