@@ -2,11 +2,13 @@ use std::{
22 borrow:: Borrow ,
33 convert:: Infallible ,
44 error:: Error ,
5+ path:: Path ,
56 str:: { FromStr , Utf8Error } ,
67 sync:: Arc ,
78} ;
89
9- use assembly_data:: { fdb:: mem:: Database , xml:: localization:: LocaleNode } ;
10+ use assembly_fdb:: mem:: Database ;
11+ use assembly_xml:: localization:: LocaleNode ;
1012use paradox_typed_db:: TypedDatabase ;
1113use percent_encoding:: percent_decode_str;
1214use warp:: {
@@ -16,16 +18,18 @@ use warp::{
1618 Filter , Reply ,
1719} ;
1820
19- use crate :: auth:: AuthKind ;
21+ use crate :: { auth:: AuthKind , data :: locale :: LocaleRoot } ;
2022
2123use self :: {
2224 adapter:: { LocaleAll , LocalePod } ,
25+ files:: make_crc_lookup_filter,
2326 rev:: { make_api_rev, ReverseLookup } ,
2427 tables:: { make_api_tables, tables_api} ,
2528} ;
2629
2730pub mod adapter;
2831mod docs;
32+ mod files;
2933pub mod rev;
3034pub mod tables;
3135
@@ -149,45 +153,57 @@ pub fn locale_api(lr: Arc<LocaleNode>) -> impl Fn(Tail) -> Option<warp::reply::J
149153 }
150154}
151155
152- pub ( crate ) fn make_api (
153- url : String ,
154- auth_kind : AuthKind ,
155- db : Database < ' static > ,
156- tydb : & ' static TypedDatabase < ' static > ,
157- rev : & ' static ReverseLookup ,
158- lr : Arc < LocaleNode > ,
159- ) -> BoxedFilter < ( WithStatus < Json > , ) > {
160- let v0_base = warp:: path ( "v0" ) ;
161- let v0_tables = warp:: path ( "tables" ) . and ( make_api_tables ( db) ) ;
162- let v0_locale = warp:: path ( "locale" )
163- . and ( warp:: path:: tail ( ) )
164- . map ( locale_api ( lr) )
165- . map ( map_opt) ;
166-
167- let v0_rev = warp:: path ( "rev" ) . and ( make_api_rev ( tydb, rev) ) ;
168- let v0_openapi = docs:: openapi ( url, auth_kind) . unwrap ( ) ;
169- let v0 = v0_base. and (
170- v0_tables
171- . or ( v0_locale)
172- . unify ( )
173- . or ( v0_rev)
174- . unify ( )
175- . or ( v0_openapi)
176- . unify ( ) ,
177- ) ;
178-
179- // v1
180- let dbf = db_filter ( db) ;
181- let v1_base = warp:: path ( "v1" ) ;
182- let v1_tables_base = dbf. and ( warp:: path ( "tables" ) ) ;
183- let v1_tables = v1_tables_base
184- . and ( warp:: path:: end ( ) )
185- . map ( tables_api)
186- . map ( map_res) ;
187- let v1 = v1_base. and ( v1_tables) ;
188-
189- // catch all
190- let catch_all = make_api_catch_all ( ) ;
191-
192- v0. or ( v1) . unify ( ) . or ( catch_all) . unify ( ) . boxed ( )
156+ pub ( crate ) struct ApiFactory < ' a > {
157+ pub url : String ,
158+ pub auth_kind : AuthKind ,
159+ pub db : Database < ' static > ,
160+ pub tydb : & ' static TypedDatabase < ' static > ,
161+ pub rev : & ' static ReverseLookup ,
162+ pub lr : Arc < LocaleNode > ,
163+ pub res_path : & ' a Path ,
164+ pub pki_path : Option < & ' a Path > ,
165+ }
166+
167+ impl < ' a > ApiFactory < ' a > {
168+ pub ( crate ) fn make_api ( self ) -> BoxedFilter < ( WithStatus < Json > , ) > {
169+ let loc = LocaleRoot :: new ( self . lr . clone ( ) ) ;
170+
171+ let v0_base = warp:: path ( "v0" ) ;
172+ let v0_tables = warp:: path ( "tables" ) . and ( make_api_tables ( self . db ) ) ;
173+ let v0_locale = warp:: path ( "locale" )
174+ . and ( warp:: path:: tail ( ) )
175+ . map ( locale_api ( self . lr ) )
176+ . map ( map_opt) ;
177+
178+ let v0_crc = warp:: path ( "crc" ) . and ( make_crc_lookup_filter ( self . res_path , self . pki_path ) ) ;
179+
180+ let v0_rev = warp:: path ( "rev" ) . and ( make_api_rev ( self . tydb , loc, self . rev ) ) ;
181+ let v0_openapi = docs:: openapi ( self . url , self . auth_kind ) . unwrap ( ) ;
182+ let v0 = v0_base. and (
183+ v0_tables
184+ . or ( v0_crc)
185+ . unify ( )
186+ . or ( v0_locale)
187+ . unify ( )
188+ . or ( v0_rev)
189+ . unify ( )
190+ . or ( v0_openapi)
191+ . unify ( ) ,
192+ ) ;
193+
194+ // v1
195+ let dbf = db_filter ( self . db ) ;
196+ let v1_base = warp:: path ( "v1" ) ;
197+ let v1_tables_base = dbf. and ( warp:: path ( "tables" ) ) ;
198+ let v1_tables = v1_tables_base
199+ . and ( warp:: path:: end ( ) )
200+ . map ( tables_api)
201+ . map ( map_res) ;
202+ let v1 = v1_base. and ( v1_tables) ;
203+
204+ // catch all
205+ let catch_all = make_api_catch_all ( ) ;
206+
207+ v0. or ( v1) . unify ( ) . or ( catch_all) . unify ( ) . boxed ( )
208+ }
193209}
0 commit comments