FastResources.vala 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Astralis;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. /**
  5. * FastResources Example
  6. *
  7. * Demonstrates the FastResource endpoint which provides high-performance
  8. * static content serving with pre-loaded data. FastResource loads content
  9. * into memory at startup, making it ideal for small static files that
  10. * need to be served quickly.
  11. *
  12. * This example shows three ways to create FastResource endpoints:
  13. * 1. from_string - For serving string content (like HTML)
  14. * 2. from_byte_array - For serving binary data (like images)
  15. * 3. Default constructor - For loading files from the filesystem
  16. *
  17. * Usage: fast-resources [port]
  18. *
  19. * Examples:
  20. * fast-resources
  21. * fast-resources 8080
  22. */
  23. // Simple 1x1 pixel PNG image (transparent) as a byte array
  24. private const uint8[] TRANSPARENT_PIXEL = {
  25. 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
  26. 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, // IHDR chunk
  27. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  28. 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xCA,
  29. 0x4B, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, // IDAT chunk
  30. 0x54, 0x78, 0x9C, 0x63, 0x60, 0x00, 0x00, 0x00,
  31. 0x02, 0x00, 0x01, 0xE5, 0x27, 0xDE, 0xFC, 0x00, // IEND chunk
  32. 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44,
  33. 0xAE, 0x42, 0x60, 0x82
  34. };
  35. void main(string[] args) {
  36. int port = args.length > 1 ? int.parse(args[1]) : 8080;
  37. // Get the path to the currently running binary (argv[0])
  38. string binary_path = args[0];
  39. // Resolve to absolute path if needed
  40. var binary_file = File.new_for_path(binary_path);
  41. if (!binary_file.is_native() || !Path.is_absolute(binary_path)) {
  42. binary_path = binary_file.get_path();
  43. }
  44. print("╔══════════════════════════════════════════════════════════════╗\n");
  45. print("║ Astralis FastResources Example ║\n");
  46. print("╠══════════════════════════════════════════════════════════════╣\n");
  47. print(@"║ Port: $port");
  48. for (int i = 0; i < 50 - port.to_string().length - 7; i++) print(" ");
  49. print(" ║\n");
  50. print("╠══════════════════════════════════════════════════════════════╣\n");
  51. print("║ Endpoints: ║\n");
  52. print("║ / - Home page (from_string) ║\n");
  53. print("║ /pixel.png - 1x1 transparent pixel (from_byte_array) ║\n");
  54. print("║ /binary - This executable (filesystem load) ║\n");
  55. print("╠══════════════════════════════════════════════════════════════╣\n");
  56. print(@"║ Binary: $(binary_path)");
  57. int path_len = binary_path.length;
  58. if (path_len < 54) {
  59. for (int i = 0; i < 54 - path_len; i++) print(" ");
  60. }
  61. print(" ║\n");
  62. print("╚══════════════════════════════════════════════════════════════╝\n");
  63. print("\nPress Ctrl+C to stop the server\n\n");
  64. // Create endpoints
  65. try {
  66. // 1. Home page using FastResource.from_string
  67. // This is ideal for serving static HTML, CSS, or other text content
  68. // that you want to embed directly in your application
  69. var home_page = new FastResource.from_string("/", """
  70. <!DOCTYPE html>
  71. <html lang="en">
  72. <head>
  73. <meta charset="UTF-8">
  74. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  75. <title>FastResources Example</title>
  76. <style>
  77. body {
  78. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  79. max-width: 800px;
  80. margin: 0 auto;
  81. padding: 20px;
  82. background: #f5f5f5;
  83. }
  84. .card {
  85. background: white;
  86. border-radius: 8px;
  87. padding: 20px;
  88. margin: 10px 0;
  89. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  90. }
  91. h1 { color: #333; }
  92. code {
  93. background: #e8e8e8;
  94. padding: 2px 6px;
  95. border-radius: 4px;
  96. }
  97. .endpoint {
  98. display: flex;
  99. align-items: center;
  100. padding: 10px;
  101. background: #fafafa;
  102. margin: 5px 0;
  103. border-radius: 4px;
  104. }
  105. .method {
  106. background: #4CAF50;
  107. color: white;
  108. padding: 4px 8px;
  109. border-radius: 4px;
  110. font-weight: bold;
  111. margin-right: 10px;
  112. }
  113. a { color: #2196F3; text-decoration: none; }
  114. a:hover { text-decoration: underline; }
  115. </style>
  116. </head>
  117. <body>
  118. <div class="card">
  119. <h1>🚀 FastResources Example</h1>
  120. <p>This example demonstrates the <code>FastResource</code> endpoint which provides
  121. high-performance static content serving with pre-loaded data.</p>
  122. </div>
  123. <div class="card">
  124. <h2>Available Endpoints</h2>
  125. <div class="endpoint">
  126. <span class="method">GET</span>
  127. <a href="/">/</a>
  128. <span style="margin-left: auto; color: #666;">This page (from_string)</span>
  129. </div>
  130. <div class="endpoint">
  131. <span class="method">GET</span>
  132. <a href="/pixel.png">/pixel.png</a>
  133. <span style="margin-left: auto; color: #666;">1x1 pixel image (from_byte_array)</span>
  134. </div>
  135. <div class="endpoint">
  136. <span class="method">GET</span>
  137. <a href="/binary">/binary</a>
  138. <span style="margin-left: auto; color: #666;">Running executable (filesystem)</span>
  139. </div>
  140. </div>
  141. <div class="card">
  142. <h2>FastResource Constructors</h2>
  143. <ul>
  144. <li><code>FastResource.from_string(route, content)</code> - Load from string</li>
  145. <li><code>FastResource.from_byte_array(route, bytes)</code> - Load from byte array</li>
  146. <li><code>FastResource(route, path)</code> - Load from filesystem</li>
  147. </ul>
  148. </div>
  149. </body>
  150. </html>
  151. """).with_content_type("text/html; charset=utf-8").with_default_compressors();
  152. // 2. Image using FastResource.from_byte_array
  153. // This is ideal for serving binary content like images, fonts, or other
  154. // assets that you want to embed directly in your application binary
  155. var pixel_image = new FastResource.from_byte_array("/pixel.png", TRANSPARENT_PIXEL)
  156. .with_content_type("image/png")
  157. .with_default_compressors();
  158. // 3. Running binary using FastResource default constructor
  159. // This loads a file from the filesystem at startup
  160. // Using argv[0] to serve the currently running executable
  161. var binary_endpoint = new FastResource("/binary", binary_path)
  162. .with_content_type("application/octet-stream")
  163. .with_default_compressors();
  164. // Set up the router with all endpoints
  165. var router = new EndpointRouter()
  166. .add_endpoint(home_page)
  167. .add_endpoint(pixel_image)
  168. .add_endpoint(binary_endpoint);
  169. // Build the pipeline
  170. var pipeline = new Pipeline()
  171. .add_component(router);
  172. // Create and configure the server
  173. var server = new Server(port, pipeline);
  174. // Run the server
  175. server.run();
  176. } catch (Error e) {
  177. printerr("Error: %s\n", e.message);
  178. Process.exit(1);
  179. }
  180. }