Skip to content

Commit c7a8093

Browse files
release: v0.1.0-dev.978 from ziex-dev/ziex
0 parents  commit c7a8093

19 files changed

Lines changed: 529 additions & 0 deletions

File tree

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* text=auto eol=lf
2+
# Make .zx files be treated as .zig files on GitHub for syntax highlighting
3+
*.zx text eol=lf linguist-language=Zig

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.zig-cache/
2+
zig-out/
3+
node_modules/

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2025 Nurul Huda (Apon).
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Ziex App with Postgres
2+
3+
>This is a starter template for building web applications with [Ziex](https://ziex.dev), a full-stack web framework for Zig.
4+
5+
**[Documentation →](https://ziex.dev)**
6+
7+
## Getting Started
8+
9+
### 1. Install ZX CLI
10+
11+
#### Linux/macOS
12+
```bash
13+
curl -fsSL https://ziex.dev/install | bash
14+
```
15+
16+
#### Windows
17+
```powershell
18+
powershell -c "irm ziex.dev/install.ps1 | iex"
19+
```
20+
21+
### 2. Install Zig
22+
```bash
23+
brew install zig # macOS
24+
winget install -e --id zig.zig # Windows
25+
```
26+
[_Other platforms →_](https://ziglang.org/learn/getting-started/)
27+
28+
29+
## Project
30+
31+
```
32+
├── app/
33+
│ ├── assets/ # Static assets (CSS, images, etc)
34+
│ ├── main.zig # Zig entrypoint
35+
│ ├── pages/ # Pages (Zig/ZX)
36+
│ │ ├── layout.zx # Root layout
37+
│ │ ├── page.zx # Home page
38+
│ │ ├── client.zx # Client side counter component
39+
│ │ └── ...
40+
│ └── public/ # Public static files (favicon, etc)
41+
├── build.zig # Zig build script
42+
├── build.zig.zon # Zig package manager config
43+
└── README.md # Project info
44+
```
45+
46+
## Usage
47+
48+
### Development
49+
```bash
50+
zig build dev
51+
```
52+
App will be available at [`http://localhost:3000`](http://localhost:3000). with hot reload enabled.
53+
54+
### Serve Production Build
55+
```bash
56+
zig build serve --release=fast
57+
```
58+
59+
### Exporting as Static Site
60+
```bash
61+
zig build zx -- export
62+
```
63+
This will create a `dist/` directory with the static export of your app. You can deploy the contents of `dist/` to any static hosting provider (Netlify, Vercel, GitHub Pages, etc) or serve it with any static file server.
64+
65+
### Deployment
66+
67+
```bash
68+
zig build zx -- bundle
69+
```
70+
71+
This will create a `bundle/` directory with the binary and static assets needed to run your app. You can deploy the contents of `bundle/` to any VPS.
72+
73+
74+
### [ZX CLI](https://ziex.dev/reference#cli) Commands
75+
```bash
76+
zig build zx -- [command] [options]
77+
```
78+
79+
All ZX CLI commands are available under `zig build zx -- [command]`. For example, to run auto formatter:
80+
```bash
81+
zig build zx -- fmt .
82+
```
83+
84+
## Contributing
85+
86+
Contributions are welcome! Feel free to open issues or pull requests. For feature requests, bug reports, or questions, see the [Ziex Repo](https://github.com/ziex-dev/ziex).
87+
88+
## Links
89+
90+
- [Documentation](https://ziex.dev)
91+
- [Discord](https://ziex.dev/r/discord)
92+
- [Topic on Ziggit](https://ziex.dev/r/ziggit)
93+
- [Project on Zig Discord Community](https://ziex.dev/r/zig-discord) (Join Zig Discord first: https://discord.gg/zig)
94+
- [GitHub](https://github.com/nurulhudaapon/ziex)
95+
- [Zig Language](https://ziglang.org/)

app/Context.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Database = @import("db/Database.zig");
2+
3+
db: Database

app/assets/style.css

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
body {
2+
margin: 0;
3+
font-family: sans-serif;
4+
font-size: 1.4rem;
5+
line-height: 1.6;
6+
color: white;
7+
background-color: black;
8+
display: flex;
9+
flex-direction: column;
10+
align-items: center;
11+
justify-content: center;
12+
text-align: center;
13+
height: 100vh;
14+
}
15+
16+
a {
17+
color: #ffffff;
18+
font-size: 1rem;
19+
text-decoration: none;
20+
}
21+
22+
button {
23+
padding: 2px;
24+
min-width: 100px;
25+
font-size: 0.8rem;
26+
border-radius: 5px;
27+
margin: 3px;
28+
border: 1px solid #ffffff;
29+
background-color: transparent;
30+
color: #ffffff;
31+
cursor: pointer;
32+
transition: all 0.3s ease;
33+
&:hover {
34+
background-color: #ffffff;
35+
color: #000000;
36+
}
37+
}
38+
39+
.counter-row {
40+
height: 150px;
41+
margin: 10px;
42+
display: flex;
43+
gap: 20px;
44+
justify-content: center;
45+
align-items: center;
46+
}
47+
48+
.counter-row > * {
49+
min-width: 150px;
50+
}
51+
52+
.counter-plus {
53+
width: 70px;
54+
display: inline-block;
55+
}
56+
57+
.link-row {
58+
display: flex;
59+
gap: 10px;
60+
justify-content: center;
61+
align-items: center;
62+
margin: 20px;
63+
}

app/db/Database.zig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const std = @import("std");
2+
const pg = @import("pg");
3+
4+
const Database = @This();
5+
6+
pool: *pg.Pool,
7+
8+
pub fn getCount(self: Database) !i64 {
9+
var result = try self.pool.query("SELECT count FROM ziex", .{});
10+
defer result.deinit();
11+
12+
if (try result.next()) |row|
13+
return row.get(i64, 0);
14+
15+
_ = try self.pool.exec("INSERT INTO ziex (count) VALUES (0)", .{});
16+
return 0;
17+
}
18+
19+
pub fn setCount(self: Database, count: i64) !void {
20+
_ = try self.pool.exec("UPDATE ziex SET count = $1", .{count});
21+
}
22+
23+
pub fn setup(self: Database) !void {
24+
_ = try self.pool.exec(@embedFile("migrations/init.sql"), .{});
25+
}
26+
27+
pub fn init(allocator: std.mem.Allocator, uri: []const u8) !Database {
28+
if (uri.len == 0) return error.InvalidDatabaseUrl;
29+
30+
const pgUri = try std.Uri.parse(uri);
31+
std.log.info("DB: {s}", .{try pgUri.getHostAlloc(allocator)});
32+
const pool = try pg.Pool.initUri(allocator, pgUri, .{ .size = 4 });
33+
34+
const db: Database = .{ .pool = pool };
35+
try db.setup();
36+
return db;
37+
}
38+
39+
pub fn deinit(self: *Database) void {
40+
self.db_pool.deinit();
41+
}

app/db/migrations/init.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Initialize the database schema
2+
CREATE TABLE IF NOT EXISTS ziex (
3+
id SERIAL PRIMARY KEY,
4+
count BIGINT NOT NULL
5+
);

app/main.zig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const std = @import("std");
2+
const zx = @import("zx");
3+
const Context = @import("Context.zig");
4+
5+
pub fn main() !void {
6+
if (zx.platform == .browser) return zx.Client.run();
7+
if (zx.platform == .edge) return zx.Edge.run();
8+
9+
var gpa = std.heap.DebugAllocator(.{}){};
10+
const allocator = gpa.allocator();
11+
defer _ = gpa.deinit();
12+
13+
const db_uri = std.process.getEnvVarOwned(allocator, "DATABASE_URL") catch
14+
"postgresql://postgres:db_password@localhost:5432/ziex";
15+
16+
var ctx: Context = .{ .db = try .init(allocator, db_uri) };
17+
const app = try zx.Server(*Context).init(allocator, .{}, &ctx);
18+
defer app.deinit();
19+
20+
app.info();
21+
try app.start();
22+
}
23+
24+
pub const std_options = zx.std_options;

app/pages/actions/client/page.zx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub fn Page(ctx: zx.PageContext) zx.Component {
2+
return (
3+
<main @allocator={ctx.arena}>
4+
<EventForm @rendering={.client} />
5+
</main>
6+
);
7+
}
8+
9+
pub fn EventForm(ctx: *zx.ComponentContext) zx.Component {
10+
const count = ctx.state(i32, 1);
11+
return (
12+
<button onclick={ctx.bind(onclick)}>
13+
Click Me {count}
14+
</button>
15+
);
16+
}
17+
18+
fn onclick(e: *zx.client.Event.Stateful) void {
19+
const c = e.state(i32);
20+
c.set(c.get() + 1);
21+
zx.log.info("Count> {d}. \n", .{c.get()});
22+
}
23+
24+
pub const options: zx.PageOptions = .{
25+
.methods = &.{ .GET, .POST },
26+
};
27+
28+
const zx = @import("zx");

0 commit comments

Comments
 (0)