using Astralis; using Invercargill; using Invercargill.DataStructures; /** * FastResources Example * * Demonstrates the FastResource endpoint which provides high-performance * static content serving with pre-loaded data. FastResource loads content * into memory at startup, making it ideal for small static files that * need to be served quickly. * * This example shows three ways to create FastResource endpoints: * 1. from_string - For serving string content (like HTML) * 2. from_byte_array - For serving binary data (like images) * 3. Default constructor - For loading files from the filesystem * * Usage: fast-resources [port] * * Examples: * fast-resources * fast-resources 8080 */ // Simple 1x1 pixel PNG image (transparent) as a byte array private const uint8[] TRANSPARENT_PIXEL = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, // IHDR chunk 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xCA, 0x4B, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, // IDAT chunk 0x54, 0x78, 0x9C, 0x63, 0x60, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0xE5, 0x27, 0xDE, 0xFC, 0x00, // IEND chunk 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 }; void main(string[] args) { int port = args.length > 1 ? int.parse(args[1]) : 8080; // Get the path to the currently running binary (argv[0]) string binary_path = args[0]; // Resolve to absolute path if needed var binary_file = File.new_for_path(binary_path); if (!binary_file.is_native() || !Path.is_absolute(binary_path)) { binary_path = binary_file.get_path(); } print("╔══════════════════════════════════════════════════════════════╗\n"); print("║ Astralis FastResources Example ║\n"); print("╠══════════════════════════════════════════════════════════════╣\n"); print(@"║ Port: $port"); for (int i = 0; i < 50 - port.to_string().length - 7; i++) print(" "); print(" ║\n"); print("╠══════════════════════════════════════════════════════════════╣\n"); print("║ Endpoints: ║\n"); print("║ / - Home page (from_string) ║\n"); print("║ /pixel.png - 1x1 transparent pixel (from_byte_array) ║\n"); print("║ /binary - This executable (filesystem load) ║\n"); print("╠══════════════════════════════════════════════════════════════╣\n"); print(@"║ Binary: $(binary_path)"); int path_len = binary_path.length; if (path_len < 54) { for (int i = 0; i < 54 - path_len; i++) print(" "); } print(" ║\n"); print("╚══════════════════════════════════════════════════════════════╝\n"); print("\nPress Ctrl+C to stop the server\n\n"); // Create endpoints try { // 1. Home page using FastResource.from_string // This is ideal for serving static HTML, CSS, or other text content // that you want to embed directly in your application var home_page = new FastResource.from_string("/", """
This example demonstrates the FastResource endpoint which provides
high-performance static content serving with pre-loaded data.
FastResource.from_string(route, content) - Load from stringFastResource.from_byte_array(route, bytes) - Load from byte arrayFastResource(route, path) - Load from filesystem