-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.html
173 lines (147 loc) · 8.04 KB
/
learn.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<title>⚡ZAP⚡</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="./css/output.css" rel="stylesheet">
<script src="./language-highlight/highlight.min.js"></script>
<link rel="stylesheet" href="./language-highlight/styles/atom-one-dark.css">
<script>hljs.highlightAll();</script>
<!-- Hamburger menu icon -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script>
// Toggle between showing and hiding the navigation menu links.
function toggleMenu() {
const x = document.getElementById("links");
x.classList.toggle("hidden");
}
</script>
</head>
<body class="h-screen">
<div id="header"
class="flex justify-between items-center py-2 px-4 md:px-10 border-b-zap-yellow border-b-solid border-b-2">
<span class="text-3xl font-bold hover:underline decoration-zap-yellow"><a href="index.html">⚡ZAP⚡</a></span>
<div class="hidden sm:flex gap-5">
<span class="text-xl hover:underline decoration-zap-yellow"><a href="learn.html">Learn</a></span>
<span class="text-xl hover:underline decoration-zap-yellow"><a href="https://zigzap.org/zap" target="_blank" rel="noopener noreferrer">Docs</a></span>
<span class="text-xl hover:underline decoration-zap-yellow"><a href="https://github.com/zigzap/zap" target="_blank" rel="noopener noreferrer">GitHub</a></span>
</div>
<button class="block sm:hidden" onclick="toggleMenu()">
<i class="fa fa-bars fa-lg"></i>
</button>
</div>
<div id="links" class="hidden sm:hidden py-2 flex flex-col gap-2 justify-center items-center border-b-zap-yellow border-b-solid border-b-2">
<span class="text-xl hover:underline decoration-zap-yellow"><a href="learn.html">Learn</a></span>
<span class="text-xl hover:underline decoration-zap-yellow"><a href="https://zigzap.org/zap" target="_blank" rel="noopener noreferrer">Docs</a></span>
<span class="text-xl hover:underline decoration-zap-yellow"><a href="https://github.com/zigzap/zap" target="_blank" rel="noopener noreferrer">GitHub</a></span>
</div>
<div class="my-2 sm:my-6" />
<div class="pt-5 mb-10 mx-auto px-5 w-full lg:max-w-[120ch]">
<section>
<div class="w-full lg:w-auto">
<div class="mb-5">
<h3 class="text-3xl font-semibold underline decoration-zap-yellow mb-2">Getting Started</h3>
<p class="text-lg mb-2">Start by creating a new 0.13.0 Zig project and changing into the new
directory.</p>
<pre><code class="language-bash">$ mkdir hello-world
$ cd hello-world
$ zig init
</code></pre>
<div class="my-6" />
<p class="text-lg mb-2">Add Zap as a dependency to <code>build.zig.zon</code></p>
<pre><code class="language-bash">$ mkdir hello-world
$ zig fetch --save "git+https://github.com/zigzap/zap#v0.9.0"
</code></pre>
<div class="my-6" />
<p class="text-lg mb-2">Add Zap to <code>build.zig</code></p>
<pre><code class="language-zig">const zap = b.dependency("zap", .{
.target = target,
.optimize = optimize,
.openssl = false, // set to true to enable TLS support
});
exe.root_module.addImport("zap", zap.module("zap"));
</code></pre>
<div class="my-6" />
<p class="text-lg mb-2">Replace the contents of <code>src/main.zig</code> with the following.
<span class="opacity-65"><i>Note: The complete source code for this example can be found <a href="https://github.com/zigzap/hello-world" class="underline decoration-zap-yellow" target="_blank" rel="noopener noreferrer">here</a>.</i></span></p>
<pre><code class="language-zig">const std = @import("std");
const zap = @import("zap");
fn on_request(r: zap.Request) void {
if (r.path) |the_path| {
std.debug.print("PATH: {s}\n", .{the_path});
}
if (r.query) |the_query| {
std.debug.print("QUERY: {s}\n", .{the_query});
}
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
}
pub fn main() !void {
var listener = zap.HttpListener.init(.{
.port = 3000,
.on_request = on_request,
.log = true,
.max_clients = 100000,
});
try listener.listen();
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
zap.start(.{
.threads = 2,
.workers = 1, // 1 worker enables sharing state between threads
});
}</code></pre>
<div class="my-6" />
<p class="text-lg mb-2">That's it! Build and run the program, then go to
<code>http://127.0.0.1:3000/</code>
</p>
<pre><code class="language-bash">$ zig build run</code></pre>
<div class="my-6" />
<p class="text-lg mb-2">For more examples, check out the repo
<a href="https://github.com/zigzap/zap?tab=readme-ov-file#heres-what-works"
class="underline decoration-zap-yellow" target="_blank" rel="noopener noreferrer">here</a>.
</p>
</div>
</div>
</section>
<div class="my-16" />
<section>
<div class="w-full lg:w-auto">
<div class="mb-5">
<h3 class="text-3xl font-semibold underline decoration-zap-yellow mb-2">FAQ</h3>
<ul class="list-disc list-inside">
<li class="text-lg">Q: Does Zap support Zig master?</li>
<ul class="list-disc list-inside ml-4 mb-2">
<li>A: See the zig-master branch. Please note that the zig-master branch is
not the official master branch of Zap until Zig 0.14.0 is released.</li>
</ul>
<li class="text-lg">Q: Does ZAP work on Windows?</li>
<ul class="list-disc list-inside ml-4 mb-2">
<li>A: No. This is due to the underlying facil.io C
library. Future versions of facil.io might support Windows but there is no timeline yet.
Your best options on Windows are WSL2 or a docker container.</li>
</ul>
<li class="text-lg">Q: Does ZAP support TLS / HTTPS?</li>
<ul class="list-disc list-inside ml-4 mb-2">
<li>A: Yes, ZAP supports using the system's openssl. See the <a
class="underline decoration-zap-yellow" target="_blank" rel="noopener noreferrer"
href="https://github.com/zigzap/zap/blob/master/examples/https/https.zig" class="underline decoration-zap-yellow"
target="_blank" rel="noopener noreferrer">https</a>
example for more information. You must set <code>.openssl = true</code> within the
<code>build.zig</code> dependency like shown above.
</li>
</ul>
<li class="text-lg">Q: Where is the API documentation?</li>
<ul class="list-disc list-inside ml-4 mb-2">
<li>Docs are a work in progress. You can check them out <a
class="underline decoration-zap-yellow" target="_blank" rel="noopener noreferrer"
href="https://zigzap.org/zap" class="underline decoration-zap-yellow"
target="_blank" rel="noopener noreferrer">here</a>.
</li>
</ul>
</ul>
</div>
</div>
</section>
</div>
</body>
</html>