robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
0520f68
push-srxvuswpkqxx
1const std =@import ( "std" ); 2 3const fern =@import ( "fern" ); 4const options =@import ( "options" ); 5 6const Self =@This (); 7 8root : [] const u8 , 9// root for html files, defaults to `root + "build"` 10buildroot : [] const u8 , 11// root for lua modules, defaults to `root + "lua"` 12luaroot : [] const u8 , 13// root for static assets, defaults to `root + "static"` 14staticroot : [] const u8 , 15// maivi runtime path 16runtime : [] const u8 , 17 18log_level : fern . Level =. info , 19base_url : [] const u8 , 20port : u16 , 21 22pub fn init ( io : std . Io , arena : * std . heap . ArenaAllocator , environ_map : * std . process . Environ . Map ) ! Self { 23const allocator =arena . allocator (); 24 25const cwd =try std . Io . Dir . cwd (). realPathFileAlloc ( io , "." , allocator ); 26 27const port_env =if ( environ_map . get ( "MAIVI_PORT" )) | p | try std . fmt . parseInt ( u16 , p , 10 ) else null ; 28const port =port_env orelse 5882 ; 29const default_base_url =try std . fmt . allocPrint ( allocator , "http://127.0.0.1: { d } " , .{ port }); 30 31const root =environ_map . get ( "MAIVI_ROOT" ) orelse cwd ; 32const default_buildroot =try std . mem . join ( allocator , "/" , & .{ root , "build" }); 33const default_luaroot =try std . mem . join ( allocator , "/" , & .{ root , "lua" }); 34const default_staticroot =try std . mem . join ( allocator , "/" , & .{ root , "static" }); 35 36return .{ 37. root =root , 38. buildroot =environ_map . get ( "MAIVI_BUILD" ) orelse default_buildroot , 39. luaroot =environ_map . get ( "MAIVI_LUA" ) orelse default_luaroot , 40. staticroot =environ_map . get ( "MAIVI_STATIC" ) orelse default_staticroot , 41. runtime =environ_map . get ( "MAIVI_RUNTIME" ) orelse options . runtimepath , 42. log_level =if ( environ_map . get ( "MAIVI_LOG" )) | env | 43if ( std . mem . eql ( u8 , env , "fatal" )) 44. fatal 45else if ( std . mem . eql ( u8 , env , "error" )) 46. err 47else if ( std . mem . eql ( u8 , env , "warn" )) 48. warn 49else if ( std . mem . eql ( u8 , env , "info" )) 50. info 51else if ( std . mem . eql ( u8 , env , "debug" )) 52. debug 53else if ( std . mem . eql ( u8 , env , "off" )) 54. off 55else 56. info 57else 58. info , 59. base_url =environ_map . get ( "MAIVI_BASE_URL" ) orelse default_base_url , 60. port =port , 61}; 62}