pakem 1.0.0

A beautiful, fast, and elegant web framework for Rust — Look like Laravel, Run at Rust Speed
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use async_trait::async_trait;
use chrono::Utc;
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
use std::time::Duration;
use super::StorageDriver;

type HmacSha256 = Hmac<Sha256>;

/// Konfigurasi koneksi ke Object Storage S3-Compatible (AWS S3, Cloudflare R2, MinIO, dll).
#[derive(Debug, Clone)]
pub struct S3Config {
    pub bucket: String,
    pub region: String,
    pub endpoint: Option<String>,
    pub access_key: String,
    pub secret_key: String,
    pub use_path_style: bool,
    pub public_url_base: Option<String>,
}

impl S3Config {
    /// Inisialisasi konfigurasi dari environment variables atau fallback.
    pub fn from_env() -> Option<Self> {
        let bucket = std::env::var("AWS_BUCKET")
            .or_else(|_| std::env::var("S3_BUCKET"))
            .ok()?;
        let access_key = std::env::var("AWS_ACCESS_KEY_ID")
            .or_else(|_| std::env::var("S3_ACCESS_KEY"))
            .unwrap_or_default();
        let secret_key = std::env::var("AWS_SECRET_ACCESS_KEY")
            .or_else(|_| std::env::var("S3_SECRET_KEY"))
            .unwrap_or_default();
        let region = std::env::var("AWS_DEFAULT_REGION")
            .or_else(|_| std::env::var("S3_REGION"))
            .unwrap_or_else(|_| "us-east-1".to_string());
        let endpoint = std::env::var("AWS_ENDPOINT")
            .or_else(|_| std::env::var("S3_ENDPOINT"))
            .ok();
        let use_path_style = std::env::var("AWS_USE_PATH_STYLE_ENDPOINT")
            .or_else(|_| std::env::var("S3_USE_PATH_STYLE"))
            .map(|v| v == "true" || v == "1")
            .unwrap_or(false);
        let public_url_base = std::env::var("AWS_URL")
            .or_else(|_| std::env::var("S3_URL"))
            .ok();

        Some(Self {
            bucket,
            region,
            endpoint,
            access_key,
            secret_key,
            use_path_style,
            public_url_base,
        })
    }
}

/// Driver S3 mandiri performa tinggi berbasis REST API dengan Signature Version 4 (SigV4).
pub struct S3Storage {
    config: S3Config,
    client: reqwest::Client,
}

impl S3Storage {
    pub fn new(config: S3Config) -> Self {
        Self {
            config,
            client: reqwest::Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .unwrap_or_default(),
        }
    }

    /// Menghitung hostname dan URL path lengkap sesuai mode virtual-host vs path-style.
    fn resolve_endpoint(&self, path: &str) -> (String, String, String) {
        let clean_path = path.trim_start_matches('/');
        if let Some(custom_endpoint) = &self.config.endpoint {
            let ep = custom_endpoint.trim_end_matches('/');
            let is_https = ep.starts_with("https://");
            let host_part = ep
                .trim_start_matches("http://")
                .trim_start_matches("https://");

            if self.config.use_path_style {
                // MinIO Style: http://127.0.0.1:9000/mybucket/file.txt
                let scheme = if is_https { "https" } else { "http" };
                let full_url = format!("{}://{}/{}/{}", scheme, host_part, self.config.bucket, clean_path);
                let uri_path = format!("/{}/{}", self.config.bucket, clean_path);
                (full_url, host_part.to_string(), uri_path)
            } else {
                // Virtual-host style: https://mybucket.r2.cloudflarestorage.com/file.txt
                let scheme = if is_https { "https" } else { "http" };
                let host = format!("{}.{}", self.config.bucket, host_part);
                let full_url = format!("{}://{}/{}", scheme, host, clean_path);
                let uri_path = format!("/{}", clean_path);
                (full_url, host, uri_path)
            }
        } else {
            // Standard AWS S3: https://mybucket.s3.us-east-1.amazonaws.com/file.txt
            let host = if self.config.region == "us-east-1" {
                format!("{}.s3.amazonaws.com", self.config.bucket)
            } else {
                format!("{}.s3.{}.amazonaws.com", self.config.bucket, self.config.region)
            };
            let full_url = format!("https://{}/{}", host, clean_path);
            let uri_path = format!("/{}", clean_path);
            (full_url, host, uri_path)
        }
    }

    /// Menghitung AWS Signature Version 4 Signing Key.
    fn get_signing_key(&self, date_stamp: &str) -> Vec<u8> {
        let k_date = hmac_sha256(format!("AWS4{}", self.config.secret_key).as_bytes(), date_stamp.as_bytes());
        let k_region = hmac_sha256(&k_date, self.config.region.as_bytes());
        let k_service = hmac_sha256(&k_region, b"s3");
        hmac_sha256(&k_service, b"aws4_request")
    }
}

/// Helper fungsi HMAC-SHA256
fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
    let mut mac = HmacSha256::new_from_slice(key).expect("HMAC can take key of any size");
    mac.update(data);
    mac.finalize().into_bytes().to_vec()
}

/// Helper fungsi SHA256 Hex
fn sha256_hex(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    hex::encode(hasher.finalize())
}

/// Helper URL encode untuk query parameter SigV4
fn url_encode(input: &str) -> String {
    let mut encoded = String::new();
    for b in input.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                encoded.push(b as char);
            }
            _ => {
                encoded.push_str(&format!("%{:02X}", b));
            }
        }
    }
    encoded
}

#[async_trait]
impl StorageDriver for S3Storage {
    async fn put(&self, path: &str, contents: &[u8]) -> Result<(), String> {
        let (full_url, host, uri_path) = self.resolve_endpoint(path);
        let now = Utc::now();
        let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = now.format("%Y%m%d").to_string();
        let payload_hash = sha256_hex(contents);

        let canonical_headers = format!("host:{}\nx-amz-content-sha256:{}\nx-amz-date:{}\n", host, payload_hash, amz_date);
        let signed_headers = "host;x-amz-content-sha256;x-amz-date";

        let canonical_request = format!(
            "PUT\n{}\n\n{}\n{}\n{}",
            uri_path, canonical_headers, signed_headers, payload_hash
        );

        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, sha256_hex(canonical_request.as_bytes())
        );

        let signing_key = self.get_signing_key(&date_stamp);
        let signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));

        let authorization = format!(
            "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
            self.config.access_key, credential_scope, signed_headers, signature
        );

        let res = self
            .client
            .put(&full_url)
            .header("Host", &host)
            .header("x-amz-date", &amz_date)
            .header("x-amz-content-sha256", &payload_hash)
            .header("Authorization", &authorization)
            .body(contents.to_vec())
            .send()
            .await
            .map_err(|e| format!("HTTP request error: {}", e))?;

        if res.status().is_success() {
            Ok(())
        } else {
            let status = res.status();
            let body = res.text().await.unwrap_or_default();
            Err(format!("S3 PUT failed with HTTP {}: {}", status, body))
        }
    }

    async fn get(&self, path: &str) -> Result<Vec<u8>, String> {
        let (full_url, host, uri_path) = self.resolve_endpoint(path);
        let now = Utc::now();
        let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = now.format("%Y%m%d").to_string();
        let payload_hash = sha256_hex(b"");

        let canonical_headers = format!("host:{}\nx-amz-content-sha256:{}\nx-amz-date:{}\n", host, payload_hash, amz_date);
        let signed_headers = "host;x-amz-content-sha256;x-amz-date";

        let canonical_request = format!(
            "GET\n{}\n\n{}\n{}\n{}",
            uri_path, canonical_headers, signed_headers, payload_hash
        );

        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, sha256_hex(canonical_request.as_bytes())
        );

        let signing_key = self.get_signing_key(&date_stamp);
        let signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));

        let authorization = format!(
            "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
            self.config.access_key, credential_scope, signed_headers, signature
        );

        let res = self
            .client
            .get(&full_url)
            .header("Host", &host)
            .header("x-amz-date", &amz_date)
            .header("x-amz-content-sha256", &payload_hash)
            .header("Authorization", &authorization)
            .send()
            .await
            .map_err(|e| format!("HTTP request error: {}", e))?;

        if res.status().is_success() {
            res.bytes()
                .await
                .map(|b| b.to_vec())
                .map_err(|e| format!("Failed to read S3 body: {}", e))
        } else {
            let status = res.status();
            let body = res.text().await.unwrap_or_default();
            Err(format!("S3 GET failed with HTTP {}: {}", status, body))
        }
    }

    async fn exists(&self, path: &str) -> bool {
        let (full_url, host, uri_path) = self.resolve_endpoint(path);
        let now = Utc::now();
        let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = now.format("%Y%m%d").to_string();
        let payload_hash = sha256_hex(b"");

        let canonical_headers = format!("host:{}\nx-amz-content-sha256:{}\nx-amz-date:{}\n", host, payload_hash, amz_date);
        let signed_headers = "host;x-amz-content-sha256;x-amz-date";

        let canonical_request = format!(
            "HEAD\n{}\n\n{}\n{}\n{}",
            uri_path, canonical_headers, signed_headers, payload_hash
        );

        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, sha256_hex(canonical_request.as_bytes())
        );

        let signing_key = self.get_signing_key(&date_stamp);
        let signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));

        let authorization = format!(
            "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
            self.config.access_key, credential_scope, signed_headers, signature
        );

        match self
            .client
            .head(&full_url)
            .header("Host", &host)
            .header("x-amz-date", &amz_date)
            .header("x-amz-content-sha256", &payload_hash)
            .header("Authorization", &authorization)
            .send()
            .await
        {
            Ok(res) => res.status().is_success(),
            Err(_) => false,
        }
    }

    async fn delete(&self, path: &str) -> Result<(), String> {
        let (full_url, host, uri_path) = self.resolve_endpoint(path);
        let now = Utc::now();
        let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = now.format("%Y%m%d").to_string();
        let payload_hash = sha256_hex(b"");

        let canonical_headers = format!("host:{}\nx-amz-content-sha256:{}\nx-amz-date:{}\n", host, payload_hash, amz_date);
        let signed_headers = "host;x-amz-content-sha256;x-amz-date";

        let canonical_request = format!(
            "DELETE\n{}\n\n{}\n{}\n{}",
            uri_path, canonical_headers, signed_headers, payload_hash
        );

        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, sha256_hex(canonical_request.as_bytes())
        );

        let signing_key = self.get_signing_key(&date_stamp);
        let signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));

        let authorization = format!(
            "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
            self.config.access_key, credential_scope, signed_headers, signature
        );

        let res = self
            .client
            .delete(&full_url)
            .header("Host", &host)
            .header("x-amz-date", &amz_date)
            .header("x-amz-content-sha256", &payload_hash)
            .header("Authorization", &authorization)
            .send()
            .await
            .map_err(|e| format!("HTTP request error: {}", e))?;

        if res.status().is_success() || res.status() == reqwest::StatusCode::NO_CONTENT {
            Ok(())
        } else {
            let status = res.status();
            let body = res.text().await.unwrap_or_default();
            Err(format!("S3 DELETE failed with HTTP {}: {}", status, body))
        }
    }

    fn url(&self, path: &str) -> String {
        let clean_path = path.trim_start_matches('/');
        if let Some(base) = &self.config.public_url_base {
            format!("{}/{}", base.trim_end_matches('/'), clean_path)
        } else {
            let (full_url, _, _) = self.resolve_endpoint(clean_path);
            full_url
        }
    }

    fn temporary_url(&self, path: &str, ttl: Duration) -> Result<String, String> {
        let clean_path = path.trim_start_matches('/');
        let (full_url, host, uri_path) = self.resolve_endpoint(clean_path);
        let now = Utc::now();
        let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = now.format("%Y%m%d").to_string();
        let expires = ttl.as_secs().to_string();

        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let credential = format!("{}/{}", self.config.access_key, credential_scope);

        let query_params = vec![
            ("X-Amz-Algorithm", "AWS4-HMAC-SHA256".to_string()),
            ("X-Amz-Credential", credential),
            ("X-Amz-Date", amz_date.clone()),
            ("X-Amz-Expires", expires),
            ("X-Amz-SignedHeaders", "host".to_string()),
        ];

        let mut sorted_params = query_params.clone();
        sorted_params.sort_by(|a, b| a.0.cmp(b.0));

        let canonical_query = sorted_params
            .iter()
            .map(|(k, v)| format!("{}={}", url_encode(k), url_encode(v)))
            .collect::<Vec<_>>()
            .join("&");

        let canonical_headers = format!("host:{}\n", host);
        let signed_headers = "host";
        let payload_hash = "UNSIGNED-PAYLOAD";

        let canonical_request = format!(
            "GET\n{}\n{}\n{}\n{}\n{}",
            uri_path, canonical_query, canonical_headers, signed_headers, payload_hash
        );

        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, sha256_hex(canonical_request.as_bytes())
        );

        let signing_key = self.get_signing_key(&date_stamp);
        let signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));

        let separator = if full_url.contains('?') { "&" } else { "?" };
        Ok(format!("{}{}X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential={}&X-Amz-Date={}&X-Amz-Expires={}&X-Amz-SignedHeaders=host&X-Amz-Signature={}",
            full_url, separator, url_encode(&sorted_params[1].1), amz_date, sorted_params[3].1, signature
        ))
    }
}