srctree

Jeremy Hertel parent 8e93e4f1 fa39ddb7
add basic example

build.zig added: 57, removed: 4, total 53
@@ -1,6 +1,6 @@
const std = @import("std");
 
pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const module = b.addModule("verse", .{
@@ -8,7 +8,6 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
_ = module;
 
const t_compiler = b.addExecutable(.{
.name = "template-compiler",
@@ -35,6 +34,31 @@ pub fn build(b: *std.Build) void {
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
 
const examples = [_][]const u8{"basic"};
for (examples) |example| {
const path = try std.fmt.allocPrint(b.allocator, "examples/{s}.zig", .{example});
const example_exe = b.addExecutable(.{
.name = example,
.root_source_file = b.path(path),
.target = target,
.optimize = optimize,
});
 
example_exe.root_module.addImport("verse", module);
 
const run_example = b.addRunArtifact(example_exe);
run_example.step.dependOn(b.getInstallStep());
 
if (b.args) |args| {
run_example.addArgs(args);
}
 
const run_name = try std.fmt.allocPrint(b.allocator, "run-{s}", .{example});
const run_description = try std.fmt.allocPrint(b.allocator, "Run example: {s}", .{example});
const run_step = b.step(run_name, run_description);
run_step.dependOn(&run_example.step);
}
}
 
pub const Compiler = struct {
 
filename was Deleted added: 57, removed: 4, total 53
@@ -0,0 +1,29 @@
const std = @import("std");
const Verse = @import("verse");
const Router = Verse.Router;
const BuildFn = Router.BuildFn;
 
const routes = [_]Router.Match{
Router.GET("", index),
};
 
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
 
var server = try Verse.Server.init(alloc, .http, .{ .routefn = route }, .{ .http = .{ .port = 8080 } });
 
server.serve() catch |err| {
std.debug.print("error: {any}", .{err});
std.posix.exit(1);
};
}
 
fn route(verse: *Verse) Router.Error!BuildFn {
return Verse.Router.router(verse, &routes);
}
 
fn index(verse: *Verse) Router.Error!void {
try verse.sendRawSlice("hello world");
}