Skip to content

Commit 96af9ec

Browse files
committed
update serve_a_folder example
1 parent b13124a commit 96af9ec

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
This is a dynamic file content example. <br>
3+
Count: {} <a href="dynamic.html">[Refresh]</a><br>
4+
<script src="/webui.js"></script>
5+
</html>

examples/serve_a_folder/main.zig

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//!Serve a Folder Example
22
const std = @import("std");
33
const webui = @import("webui");
4+
const test_txt = @embedFile("test.txt");
5+
const dynamic_txt = @embedFile("dynamic.txt");
46

57
var MyWindow: webui = undefined;
68
var MySecondWindow: webui = undefined;
@@ -102,30 +104,15 @@ fn my_files_handler(filename: []const u8) ?[]const u8 {
102104

103105
if (std.mem.eql(u8, filename, "/test.txt")) {
104106
// Const static file example
105-
return
106-
\\HTTP/1.1 200 OK
107-
\\Content-Type: text/html
108-
\\Content-Length: 99
109-
\\
110-
\\<html>
111-
\\ This is a static embedded file content example.
112-
\\ <script src="webui.js"></script>
113-
\\</html>
114-
;
107+
return test_txt;
115108
} else if (std.mem.eql(u8, filename, "/dynamic.html")) {
116109
const body = webui.malloc(1024);
117110
defer webui.free(body);
118111
const header_and_body = webui.malloc(1024);
119112

120113
count += 1;
121114

122-
const buf = std.fmt.bufPrint(body,
123-
\\<html>
124-
\\ This is a dynamic file content example. <br>
125-
\\ Count: {} <a href="dynamic.html">[Refresh]</a><br>
126-
\\ <script src="/webui.js"></script>
127-
\\</html>
128-
, .{count}) catch unreachable;
115+
const buf = std.fmt.bufPrint(body, dynamic_txt, .{count}) catch unreachable;
129116

130117
const content = std.fmt.bufPrint(header_and_body,
131118
\\HTTP/1.1 200 OK

examples/serve_a_folder/test.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
HTTP/1.1 200 OK
2+
Content-Type: text/html
3+
Content-Length: 99
4+
5+
<html>
6+
This is a static embedded file content example.
7+
<script src="webui.js"></script>
8+
</html>

0 commit comments

Comments
 (0)