| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 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("/", """
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>FastResources Example</title>
- <style>
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
- max-width: 800px;
- margin: 0 auto;
- padding: 20px;
- background: #f5f5f5;
- }
- .card {
- background: white;
- border-radius: 8px;
- padding: 20px;
- margin: 10px 0;
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
- }
- h1 { color: #333; }
- code {
- background: #e8e8e8;
- padding: 2px 6px;
- border-radius: 4px;
- }
- .endpoint {
- display: flex;
- align-items: center;
- padding: 10px;
- background: #fafafa;
- margin: 5px 0;
- border-radius: 4px;
- }
- .method {
- background: #4CAF50;
- color: white;
- padding: 4px 8px;
- border-radius: 4px;
- font-weight: bold;
- margin-right: 10px;
- }
- a { color: #2196F3; text-decoration: none; }
- a:hover { text-decoration: underline; }
- </style>
- </head>
- <body>
- <div class="card">
- <h1>🚀 FastResources Example</h1>
- <p>This example demonstrates the <code>FastResource</code> endpoint which provides
- high-performance static content serving with pre-loaded data.</p>
- </div>
-
- <div class="card">
- <h2>Available Endpoints</h2>
-
- <div class="endpoint">
- <span class="method">GET</span>
- <a href="/">/</a>
- <span style="margin-left: auto; color: #666;">This page (from_string)</span>
- </div>
-
- <div class="endpoint">
- <span class="method">GET</span>
- <a href="/pixel.png">/pixel.png</a>
- <span style="margin-left: auto; color: #666;">1x1 pixel image (from_byte_array)</span>
- </div>
-
- <div class="endpoint">
- <span class="method">GET</span>
- <a href="/binary">/binary</a>
- <span style="margin-left: auto; color: #666;">Running executable (filesystem)</span>
- </div>
- </div>
-
- <div class="card">
- <h2>FastResource Constructors</h2>
- <ul>
- <li><code>FastResource.from_string(route, content)</code> - Load from string</li>
- <li><code>FastResource.from_byte_array(route, bytes)</code> - Load from byte array</li>
- <li><code>FastResource(route, path)</code> - Load from filesystem</li>
- </ul>
- </div>
- </body>
- </html>
- """).with_content_type("text/html; charset=utf-8").with_default_compressors();
-
- // 2. Image using FastResource.from_byte_array
- // This is ideal for serving binary content like images, fonts, or other
- // assets that you want to embed directly in your application binary
- var pixel_image = new FastResource.from_byte_array("/pixel.png", TRANSPARENT_PIXEL)
- .with_content_type("image/png")
- .with_default_compressors();
-
- // 3. Running binary using FastResource default constructor
- // This loads a file from the filesystem at startup
- // Using argv[0] to serve the currently running executable
- var binary_endpoint = new FastResource("/binary", binary_path)
- .with_content_type("application/octet-stream")
- .with_default_compressors();
-
- // Set up the router with all endpoints
- var router = new EndpointRouter()
- .add_endpoint(home_page)
- .add_endpoint(pixel_image)
- .add_endpoint(binary_endpoint);
-
- // Build the pipeline
- var pipeline = new Pipeline()
- .add_component(router);
-
- // Create and configure the server
- var server = new Server(port, pipeline);
-
- // Run the server
- server.run();
-
- } catch (Error e) {
- printerr("Error: %s\n", e.message);
- Process.exit(1);
- }
- }
|