@@ -20,62 +20,78 @@ lazy_static! {
2020///
2121/// The handler which handles requests on cloudflare
2222///
23- pub async fn fetch ( req : worker:: Request , env : worker:: Env , _: worker:: Context ) -> anyhow:: Result < worker:: Response > {
24- log:: info!( "{} {:?}" , req. method( ) . to_string( ) , req. url( ) . map( |u| u. to_string( ) ) ) ;
25- let req = to_request ( req) . await ?;
26- let env = Rc :: new ( env) ;
27- let app_ctx = match get_app_ctx ( env, & req) . await ? {
28- Ok ( app_ctx) => app_ctx,
29- Err ( e) => return Ok ( to_response ( e) . await ?) ,
30- } ;
31- let resp = handle_request :: < GraphQLRequest , CloudflareHttp , CloudflareEnv > ( req, app_ctx) . await ?;
32- Ok ( to_response ( resp) . await ?)
23+ pub async fn fetch (
24+ req : worker:: Request ,
25+ env : worker:: Env ,
26+ _: worker:: Context ,
27+ ) -> anyhow:: Result < worker:: Response > {
28+ log:: info!(
29+ "{} {:?}" ,
30+ req. method( ) . to_string( ) ,
31+ req. url( ) . map( |u| u. to_string( ) )
32+ ) ;
33+ let req = to_request ( req) . await ?;
34+ let env = Rc :: new ( env) ;
35+ let app_ctx = match get_app_ctx ( env, & req) . await ? {
36+ Ok ( app_ctx) => app_ctx,
37+ Err ( e) => return Ok ( to_response ( e) . await ?) ,
38+ } ;
39+ let resp =
40+ handle_request :: < GraphQLRequest , CloudflareHttp , CloudflareEnv > ( req, app_ctx) . await ?;
41+ Ok ( to_response ( resp) . await ?)
3342}
3443
3544///
3645/// Initializes the worker once and caches the app context
3746/// for future requests.
3847///
3948async fn get_app_ctx (
40- env : Rc < worker:: Env > ,
41- req : & Request < Body > ,
49+ env : Rc < worker:: Env > ,
50+ req : & Request < Body > ,
4251) -> anyhow:: Result < Result < Arc < CloudFlareAppContext > , Response < Body > > > {
43- // Read context from cache
44- let file_path = req
45- . uri ( )
46- . query ( )
47- . and_then ( |x| serde_qs:: from_str :: < HashMap < String , String > > ( x) . ok ( ) )
48- . and_then ( |x| x. get ( "config" ) . cloned ( ) ) ;
52+ // Read context from cache
53+ let file_path = req
54+ . uri ( )
55+ . query ( )
56+ . and_then ( |x| serde_qs:: from_str :: < HashMap < String , String > > ( x) . ok ( ) )
57+ . and_then ( |x| x. get ( "config" ) . cloned ( ) ) ;
4958
50- if let Some ( file_path) = & file_path {
51- if let Some ( app_ctx) = read_app_ctx ( ) {
52- if app_ctx. 0 == file_path. borrow ( ) {
53- log:: info!( "Using cached application context" ) ;
54- return Ok ( Ok ( app_ctx. clone ( ) . 1 ) ) ;
55- }
59+ if let Some ( file_path) = & file_path {
60+ if let Some ( app_ctx) = read_app_ctx ( ) {
61+ if app_ctx. 0 == file_path. borrow ( ) {
62+ log:: info!( "Using cached application context" ) ;
63+ return Ok ( Ok ( app_ctx. clone ( ) . 1 ) ) ;
64+ }
65+ }
5666 }
57- }
5867
59- // Create new context
60- let env_io = CloudflareEnv :: init ( env. clone ( ) ) ;
61- let bucket_id = env_io. get ( "BUCKET" ) . ok_or ( anyhow ! ( "CONFIG var is not set" ) ) ?;
62- log:: debug!( "R2 Bucket ID: {}" , bucket_id) ;
68+ // Create new context
69+ let env_io = CloudflareEnv :: init ( env. clone ( ) ) ;
70+ let bucket_id = env_io
71+ . get ( "BUCKET" )
72+ . ok_or ( anyhow ! ( "CONFIG var is not set" ) ) ?;
73+ log:: debug!( "R2 Bucket ID: {}" , bucket_id) ;
6374
64- let file = init_file ( env. clone ( ) , bucket_id) ?;
65- let http = init_http ( ) ;
66- let cache = init_cache ( env) ;
75+ let file = init_file ( env. clone ( ) , bucket_id) ?;
76+ let http = init_http ( ) ;
77+ let cache = init_cache ( env) ;
6778
68- match showcase_get_app_ctx :: < GraphQLRequest , _ , _ , _ > ( req, ( http, env_io, Some ( file) , Arc :: new ( cache) ) ) . await ? {
69- Ok ( app_ctx) => {
70- let app_ctx = Arc :: new ( app_ctx) ;
71- if let Some ( file_path) = file_path {
72- * APP_CTX . write ( ) . unwrap ( ) = Some ( ( file_path, app_ctx. clone ( ) ) ) ;
73- }
74- log:: info!( "Initialized new application context" ) ;
75- Ok ( Ok ( app_ctx) )
79+ match showcase_get_app_ctx :: < GraphQLRequest , _ , _ , _ > (
80+ req,
81+ ( http, env_io, Some ( file) , Arc :: new ( cache) ) ,
82+ )
83+ . await ?
84+ {
85+ Ok ( app_ctx) => {
86+ let app_ctx = Arc :: new ( app_ctx) ;
87+ if let Some ( file_path) = file_path {
88+ * APP_CTX . write ( ) . unwrap ( ) = Some ( ( file_path, app_ctx. clone ( ) ) ) ;
89+ }
90+ log:: info!( "Initialized new application context" ) ;
91+ Ok ( Ok ( app_ctx) )
92+ }
93+ Err ( e) => Ok ( Err ( e) ) ,
7694 }
77- Err ( e) => Ok ( Err ( e) ) ,
78- }
7995}
8096
8197fn read_app_ctx ( ) -> Option < ( String , Arc < AppContext > ) > {
0 commit comments