robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
6e07b35
push-srxvuswpkqxx
1const std =@import ( "std" ); 2const http =std . http ; 3const Request =http . Server . Request ; 4 5const fern =@import ( "fern" ); 6const httpz =@import ( "httpz" ); 7const zlua =@import ( "zlua" ); 8const Lua =zlua . Lua ; 9 10const fragment =@import ( "fragment.zig" ); 11const handler =@import ( "handler.zig" ); 12const Connection =handler . Connection ; 13const helpers =@import ( "helpers.zig" ); 14const maivi =@import ( "root.zig" ); 15 16const Self =@This (); 17 18pub const Path =struct { 19action : Action , 20config : Config , 21 22pub fn init ( action : Action , cfg : ? Config ) Path { 23return .{ . action =action , . config =cfg orelse .{} }; 24} 25 26pub const Action =* const fn ( conn : * Connection ) anyerror ! void ; 27pub const Config =struct { 28allowed_methods : [] const http . Method =& .{. GET }, 29}; 30// pub const Error = error{} || Request.ExpectContinueError; 31}; 32 33pub const LuaPath =struct { 34f : i32 , 35props : i32 , 36}; 37 38paths : std . hash_map . StringHashMap ( Path ), 39luapaths : std . hash_map . StringHashMap ( LuaPath ), 40 41pub fn init () Self { 42return .{ 43. paths =. init ( maivi . allocator ), 44. luapaths =. init ( maivi . allocator ), 45}; 46} 47 48pub fn deinit ( self : * Self ) void { 49self . paths . deinit (); 50self . luapaths . deinit (); 51} 52 53pub fn add ( self : * Self , path : [] const u8 , action : Path . Action , cfg : ? Path . Config ) ! void { 54fern . debug (). ctx ( "router" ). msg ( "adding router path" ). str ( "path" , path ). log (); 55return try self . paths . put ( path , . init ( action , cfg )); 56} 57 58// lua ======================================================================== 59 60pub fn lua_openrouter ( l : * Lua ) ! i32 { 61l . createTable ( 0 , 1 ); 62 63l . pushFunction ( zlua . wrap ( lua_add )); 64l . setField ( - 2 , "add" ); 65 66return 1 ; 67} 68 69fn lua_add ( l : * Lua ) ! i32 { 70fern . trace (). ctx ( "router" ). msg ( "lua_add" ). log (); 71 72l . argCheck ( l . isTable ( 1 ), 1 , "table" ); 73l . argCheck ( l . isString ( 2 ), 2 , "string" ); 74l . argCheck ( l . isFunction ( 3 ), 3 , "function" ); 75const noprops =l . getTop () < 4 or l . isNil ( 4 ); 76l . argCheck ( noprops or l . isTable ( 4 ), 4 , "table?" ); 77// stack: -1 => props -2 => function -3 => string -4 => router (empty table) 78 79const path =try l . toString ( 2 ); 80 81const f =maivi . lua . toRef ( l , 3 ); 82 83const props =blk : { 84if ( noprops ) 85l . newTable () 86else 87l . pushValue ( - 1 ); 88 89// set props.url 90_ = l . pushString ( path ); 91l . setField ( - 2 , "url" ); 92 93break : blk maivi . lua . toRef ( l , null ); 94}; 95 96try maivi . router . add ( path , handle_lua , .{}); 97 98if ( maivi . router . luapaths . contains ( path )) return error . Clobber ; 99try maivi . router . luapaths . put ( path , .{ 100. f =f , 101. props =props , 102}); 103 104return 0 ; 105} 106 107// TODO: 108// - set content 109// - caching? 110pub fn handle_lua ( conn : * Connection ) ! void { 111const request =conn . request ; 112 113const content_type =helpers . content_type_for_file ( conn . path ) orelse . html ; 114 115var wbuf : [ 0x4000 ] u8 =undefined ; 116var b =try maivi . helpers . respond ( request , & wbuf , .{ 117// .content_length = size, 118. content_type =content_type , 119}); 120 121const data =maivi . router . luapaths . get ( conn . path ) orelse return error . FileNotFound ; 122try get ( conn . lua , conn , & b . writer , data ); 123try b . end (); 124} 125 126pub fn get ( l : * Lua , conn : * Connection , writer : * std . Io . Writer , data : LuaPath ) ! void { 127// call handler function 128_ = l . getIndexRaw ( zlua . registry_index , data . f ); 129if ( ! l . isFunction ( - 1 )) return error . LuaValueNotAFunction ; 130_ = l . getIndexRaw ( zlua . registry_index , data . props ); 131if ( ! l . isTable ( - 1 )) return error . LuaValueNotATable ; 132 133// read content 134try blk : { 135// stack: -1 => getIndexRaw(props) 136const stack_size_on_entry =l . getTop (); 137const filepath =filepath : { 138const ltype_path =l . getField ( - 1 , "path" ); 139defer l . pop ( 1 ); // pops "path" 140if ( ltype_path != . string ) break : blk ; 141break : filepath try l . toString ( - 1 ); 142}; 143 144try maivi . lua . read_data ( l , conn . io , filepath ); 145l . setField ( - 2 , "content" ); 146 147if ( l . getTop () != stack_size_on_entry ) break : blk error . UnexpectedStackSize ; 148}; 149 150// prepare ctx 151try blk : { 152// stack: -1 => getIndexRaw(props) 153const stack_size_on_entry =l . getTop (); 154try maivi . lua . pushCtx ( l , Connection , conn ); 155l . pushValue ( - 3 ); 156l . setField ( - 2 , "props" ); 157l . setGlobal ( "ctx" ); 158 159if ( l . getTop () != stack_size_on_entry ) break : blk error . UnexpectedStackSize ; 160}; 161 162try maivi . lua . pcall ( l , .{ . args =1 , . results =1 }); 163 164try fragment . render ( l , writer ); 165}