-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathlib.rs
More file actions
114 lines (101 loc) · 2.67 KB
/
Copy pathlib.rs
File metadata and controls
114 lines (101 loc) · 2.67 KB
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
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::{
atomic::{AtomicBool, Ordering},
LazyLock, Mutex,
};
#[cfg(feature = "http")]
use tower_service::Service;
#[cfg(feature = "http")]
use worker::HttpRequest;
use worker::{console_log, event, js_sys, wasm_bindgen, Env, Result};
#[cfg(not(feature = "http"))]
use worker::{Request, Response};
mod alarm;
mod analytics_engine;
mod assets;
mod auto_response;
mod cache;
mod container;
mod counter;
mod d1;
mod durable;
mod fetch;
mod flagship;
mod form;
mod js_snippets;
mod kv;
mod put_raw;
mod queue;
mod r2;
mod rate_limit;
mod request;
mod router;
mod secret_store;
mod send_email;
mod service;
mod signal;
mod socket;
mod sql_counter;
mod sql_iterator;
mod user;
mod ws;
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiData {
user_id: i32,
title: String,
completed: bool,
}
#[derive(Clone)]
pub struct SomeSharedData {
regex: &'static Regex,
}
static GLOBAL_STATE: AtomicBool = AtomicBool::new(false);
static GLOBAL_SECOND_START: AtomicBool = AtomicBool::new(false);
static GLOBAL_QUEUE_STATE: Mutex<Vec<queue::QueueBody>> = Mutex::new(Vec::new());
static DATA_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap());
// We're able to specify a start event that is called when the WASM is initialized before any
// requests. This is useful if you have some global state or setup code, like a logger. This is
// only called once for the entire lifetime of the worker.
#[event(start)]
pub fn start() {
// Change some global state so we know that we ran our setup function.
GLOBAL_STATE.store(true, Ordering::SeqCst);
}
#[event(start)]
pub fn second_start() {
GLOBAL_SECOND_START.store(true, Ordering::SeqCst);
}
#[cfg(feature = "http")]
type HandlerRequest = HttpRequest;
#[cfg(not(feature = "http"))]
type HandlerRequest = Request;
#[cfg(feature = "http")]
type HandlerResponse = http::Response<axum::body::Body>;
#[cfg(not(feature = "http"))]
type HandlerResponse = Response;
/// Entrypoint to the worker for handling fetch requests.
///
/// # Errors
///
/// Returns the same error as the underlying router implementation.
#[event(fetch, respond_with_errors)]
pub async fn main(
request: HandlerRequest,
env: Env,
_ctx: worker::Context,
) -> Result<HandlerResponse> {
let data = SomeSharedData { regex: &DATA_REGEX };
#[cfg(feature = "http")]
let res = {
let mut router = router::make_router(data, env);
Ok(Service::call(&mut router, request).await?)
};
#[cfg(not(feature = "http"))]
let res = {
let router = router::make_router(data);
router.run(request, env).await
};
res
}