87 Commits

Author SHA1 Message Date
Sean M. Collins
aae890998f fix spelling mistake 2025-03-10 22:07:12 -04:00
Sean M. Collins
21bf237437 Shrink database.zig and comment 2025-03-10 21:57:02 -04:00
Sean M. Collins
9e4e8529c5 Create a template file for init
This allows us to use database.zig for CI, while still creating a blank
database.zig for new projects that are created via jetzig init
2025-03-10 21:49:36 -04:00
Sean M. Collins
70a8b0f698 Add tests for login view 2025-03-10 21:20:33 -04:00
Sean M. Collins
22688ccaa3 Set adapter to postgresql 2025-03-10 17:13:08 -04:00
Sean M. Collins
f3788567bb Fix argument to first migration 2025-03-10 17:05:09 -04:00
Sean M. Collins
58f7e0e76a Add User to schema 2025-03-10 16:50:11 -04:00
Sean M. Collins
b070d281e3 Add user table 2025-03-10 16:32:27 -04:00
Sean M. Collins
9012ec3aaf Add an example for using auth for logging in 2025-03-10 16:26:58 -04:00
Sean M. Collins
d8a992ce38 Set same_site to correct type
Fixes #170
2025-03-08 07:52:02 -05:00
Bob Farrell
06674db51f Use fork of zig-args (temporary)
Revert when [this PR](https://github.com/ikskuh/zig-args/pull/67) is
merged.
2025-03-02 13:12:29 +00:00
Bob Farrell
099a2a5349 Use Zig compiler
Default to not using LLVM for application compilation. This gives more
than 2x performance improvement for compilation stage.
2025-03-01 14:33:17 +00:00
Bob Farrell
c0d6fb162b Latest Zig build.zig.zon format compatibility 2025-03-01 12:28:08 +00:00
Bob Farrell
a5430ff380 Zig 0.14 test runner config changes
Update dependencies to match new Zig build config API for test runners,
misc. fixes needed after upgrading dependencies.
2025-02-02 11:31:40 +00:00
Bob Farrell
35adaf3009 Valkey backend for JetKV 2024-12-06 21:31:02 +00:00
Bob Farrell
f44a6b33c5 Routing updates
Implement `/foo/1/edit` route/view.

Allow setting HTTP verb by passing e.g. `/_PATCH` as the last segment of
a URL - this allows browsers to submit a `POST` request with a
pseudo-HTTP verb encoded in the URL which Jetzig can translate, i.e.
allowing forms to submit a `PATCH`.
2024-11-24 20:46:56 +00:00
Bob Farrell
1565ae3b73 Debug console
Use `jetzig server` or `zig build -Ddebug_console=true run` to launch a
development server with the debug console enabled.

When an error is encountered a formatted stack trace is dumped to the
browser along with the current response data. Both outputs are
syntax-highlighted using Prism JS.
2024-11-23 18:07:19 +00:00
Bob Farrell
8095bbcb76 Implement request.renderTemplate
Clean up request state.
2024-11-23 13:42:14 +00:00
Bob Farrell
036aec1682 Misc. tidying 2024-11-23 13:14:11 +00:00
Bob Farrell
6e6f1bec1b Closes #108: Anti-CSRF middleware
Add to middleware in app's `src/main.zig`:

```zig
pub const jetzig_options = struct {
    pub const middleware: []const type = &.{
        jetzig.middleware.AntiCsrfMiddleware,
    };
};
```

CSRF token available in Zmpl templates:

```
{{context.authenticityToken()}}
```
or render a hidden form element:
```
{{context.authenticityFormElement()}}
```

The following HTML requests are rejected (403 Forbidden) if the
submitted query param does not match the value stored in the encrypted
session (added automatically when the token is generated for a template
value):

* POST
* PUT
* PATCH
* DELETE

JSON requests are not impacted - users should either disable JSON
endpoints or implement a different authentication method to protect
them.
2024-11-23 12:49:49 +00:00
Bob Farrell
92dce21244 Database CLI improvements
Eradication of `data` arg to requests. We no longer need to pass this
value around as we have a) type inference, b) nested object insertion
via `put` and `append`.

Fix `joinPath` numeric type coercion

Detect empty string params and treat them as blank with expectParams()

Fix error logging/stack trace printing.

Update Zmpl - includes debug tokens + error tracing to source template
in debug builds.
2024-11-19 21:39:01 +00:00
Bob Farrell
b95506caf9 Params helpers
Implement `request.expectParams()` to coerce params to a given struct.
`request.paramsInfo()` provides information about each param (present,
blank, failed + original values and errors where applicable).
2024-11-17 19:07:27 +00:00
Bob Farrell
e3ab49fa5a Refactor Route
Move all route types into a single union and remove leftover junk.

Deprecate view functions that receive a `*jetzig.Data` argument (we will
stay backward-compatible until we have a valid reason to drop support
for legacy view functions - the extra code overhead is pretty minimal).
2024-11-17 15:08:54 +00:00
Bob Farrell
a6d1b92f5e Simplify DevelopmentLogger, add ProductionLogger
Add auth helper to create a user from CLI:

```
jetzig auth user:create user@example.com
```
2024-11-11 22:25:35 +00:00
Bob Farrell
b0613aab10 Import Schema in demo/init config 2024-11-09 18:36:48 +00:00
Bob Farrell
78a9ccaaa4 Fix database config path 2024-11-09 18:06:16 +00:00
Bob Farrell
d27907a1c5 WIP 2024-11-09 17:13:32 +00:00
Bob Farrell
1c2cbaca77 WIP 2024-11-09 17:12:49 +00:00
Bob Farrell
c7b79f144a Fixes for https://github.com/ziglang/zig/pull/21817 2024-11-06 08:44:07 +00:00
IbrahimOuhamou
bd2761b12f added session.remove to demo thanks to Allah 2024-11-03 13:10:47 +01:00
Bob Farrell
f971f18a60 Global data
Define `pub const Global = SomeType` at top level in `src/main.zig`,
then create a pointer to `SomeType` and pass to `app.start`:

```
app.start(routes, .{ .global = global });
```

Then access in a view as `request.global`.
2024-10-28 09:07:06 +00:00
Bob Farrell
9e4a81aa19 Closes #105: Configure SMTP from environment variables
Fall back to hardcoded values if each `JETZIG_SMTP_*` variable is not
present.
2024-10-17 22:09:04 +01:00
Bob Farrell
e98c5ec3df Update http.zig
Refactor routes generation to standalone exe (fixes some build-time vs.
run-time issues).
2024-08-24 11:18:08 +01:00
Bob Farrell
1406447f24 Implement response format constraints
Define `pub const formats` in a view to specify which formats are
available. Use this to e.g. disable JSON responses.
2024-08-04 16:06:39 +01:00
Bob Farrell
9971cde875 Close #89: Implement file upload support
Use `request.file("form-field-name")` to try to find a multipart-encoded
form value for the given name. Returns `jetzig.http.File` if a match is
found which provides `content` (uploaded file content) and `filename`
(filename as passed by browser).
2024-06-19 20:47:05 +01:00
Bob Farrell
179b5e77c3 Comment custom route - use as example only 2024-06-09 11:18:14 +01:00
Bob Farrell
3882eba2f3 Clean up static params and init script 2024-06-09 11:13:33 +01:00
Bob Farrell
e30d340a7a Embed static routes in compiled exe
Remove need for static routes output files to be copied/generated in
deployment.
2024-06-08 19:37:41 +01:00
Bob Farrell
2d921a1c27 Add jetzig routes command
Output all current routes. Invokes `zig build jetzig:routes`.
2024-06-05 21:26:23 +01:00
Bob Farrell
06e13b3752 Add missing (accidentally .gitignored) templates 2024-06-03 22:15:31 +01:00
Bob Farrell
a46bc0ed19 Test helpers
Add `jetzig test` command which runs build step `jetzig:test`.

Add `jetzig.testing` namespace which provides test helpers and a test
app.

Add tests to view generator (i.e. include tests for generated routes).
2024-06-03 21:56:32 +01:00
Bob Farrell
7339641401 Refactoring 2024-06-01 16:20:16 +01:00
Froxcey
f538b0ddce Fix Compression Middleware
Checks "Accept-Encoding" header, add deflate encoding, rename the
middleware, and avoid unnecessary encoding.
2024-06-01 16:17:45 +01:00
Froxcey
4ea023ee5a Use gzip in demo 2024-06-01 16:17:45 +01:00
Bob Farrell
887e4e551b Middleware request resolution
Allow middleware to resolve a request by calling `request.redirect` or
`request.render` - after this point, the request stops processing and
renders immediately.
2024-05-27 16:38:11 +01:00
Bob Farrell
b6b67f8dd6 Use http.zig arena pool 2024-05-26 21:37:48 +01:00
Bob Farrell
80ca764c0f Update Zmpl for template inheritance
Permit setting template during view render with `request.setTemplate()`

Permit middleware to define custom routes to static content with
`pub const Routes` (implemented for something no longer needed but seems
useful anyway).

Implement globbing on custom routes, `/foo/:bar*` will glob all path
segments including and after `/foo/...`, e.g. `/foo/bar/baz/qux` will
pass invoke the custom view function with an array of `bar`, `baz`,
`qux` as first argument (instead of typical resource ID).
2024-05-26 17:20:21 +01:00
Bob Farrell
77794cd34d Implement custom routes
In `main()`:

```zig
    app.route(.GET, "/custom/:id/foo/bar", @import("app/views/custom/foo.zig"), .bar);
```

Routes `GET` request with path (e.g.) `/custom/1234/foo/bar` to `bar()`
defined in `src/app/views/custom/foo.zig`.

Routes with an `:id` segment expect a function with three parameters,
routes without an `:id` segment expect a function with two parameters
(i.e. the same as `get` vs `index`).
2024-05-22 20:49:41 +01:00
Bob Farrell
3b30ab4625 Update build.zig and deps for 0.13 build system 2024-05-21 19:42:56 +01:00
Bob Farrell
3519483f34 Switch to http.zig
Use Karl Seguin's http.zig as HTTP server backend:
https://github.com/karlseguin/http.zig

Update loggers to use new `jetzig.loggers.LogQueue` to offload logging
to a background thread.

Numerous other optimizations to remove unneeded allocs.

Performance jump on a simple request from approx. 2k requests/second to
approx. 40k requests/second (Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz).

Color support for Windows

Zmpl partial arg type coercion
2024-05-12 21:33:26 +01:00