Day 9: Replace bash script with zig application
Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
This commit is contained in:
parent
3ef14ce028
commit
d645fa7b4d
7 changed files with 52 additions and 120 deletions
25
build.zig
25
build.zig
|
@ -15,20 +15,6 @@ pub fn build(b: *std.Build) void {
|
|||
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const lib = b.addStaticLibrary(.{
|
||||
.name = "december-adventure-2024",
|
||||
// In this case the main source file is merely a path, however, in more
|
||||
// complicated build scripts, this could be a generated file.
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
// This declares intent for the library to be installed into the standard
|
||||
// location when the user invokes the "install" step (the default step when
|
||||
// running `zig build`).
|
||||
b.installArtifact(lib);
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "december-adventure-2024",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
|
@ -64,16 +50,6 @@ pub fn build(b: *std.Build) void {
|
|||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
// Creates a step for unit testing. This only builds the test executable
|
||||
// but does not run it.
|
||||
const lib_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
|
@ -86,6 +62,5 @@ pub fn build(b: *std.Build) void {
|
|||
// the `zig build --help` menu, providing a way for the user to request
|
||||
// running the unit tests.
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_lib_unit_tests.step);
|
||||
test_step.dependOn(&run_exe_unit_tests.step);
|
||||
}
|
||||
|
|
|
@ -23,44 +23,7 @@
|
|||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
|
|
|
@ -4,16 +4,9 @@ services:
|
|||
restart: unless-stopped
|
||||
ports:
|
||||
- 1965:1965
|
||||
working_dir: /app/bin
|
||||
volumes:
|
||||
- ./config:/app/config
|
||||
- ./public_gmi:/public_gmi
|
||||
- ./cmd:/app/cmd
|
||||
|
||||
# december-adventure:
|
||||
# image: alpine:latest
|
||||
# restart: unless-stopped
|
||||
# ports:
|
||||
# - 9001:9001
|
||||
# volumes:
|
||||
# - ../zig-out/bin/:/run
|
||||
# command: ["sh", "-c", "cd /run && ./december-adventure-2024"]
|
||||
- ../zig-out/bin:/app/bin
|
||||
|
|
|
@ -8,9 +8,9 @@ hosts:
|
|||
-
|
||||
path: /create
|
||||
input: Provide new Log entry
|
||||
command: /bin/sh /app/cmd/run.sh create "$USERINPUT"
|
||||
command: /app/bin/december-adventure-2024 create "$USERINPUT"
|
||||
cache: 0
|
||||
-
|
||||
path: /
|
||||
command: /bin/sh /app/cmd/run.sh list
|
||||
command: /app/bin/december-adventure-2024 list
|
||||
cache: 0
|
73
src/main.zig
73
src/main.zig
|
@ -1,44 +1,45 @@
|
|||
const std = @import("std");
|
||||
const net = std.net;
|
||||
const posix = std.posix;
|
||||
const builtin = @import("builtin");
|
||||
const eql = @import("std").mem.eql;
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
pub fn main() !void {
|
||||
const address = try std.net.Address.parseIp("0.0.0.0", 9001);
|
||||
// Read the first argument to determine the subcommand
|
||||
const allocator = std.heap.page_allocator;
|
||||
var args = try std.process.argsWithAllocator(allocator);
|
||||
defer args.deinit();
|
||||
|
||||
const tpe: u32 = posix.SOCK.STREAM;
|
||||
const protocol = posix.IPPROTO.TCP;
|
||||
const listener = try posix.socket(address.any.family, tpe, protocol);
|
||||
defer posix.close(listener);
|
||||
_ = args.next(); // Skip the first argument, which is the program name
|
||||
|
||||
try posix.setsockopt(listener, posix.SOL.SOCKET, posix.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));
|
||||
try posix.bind(listener, &address.any, address.getOsSockLen());
|
||||
try posix.listen(listener, 128);
|
||||
const firstArg = args.next() orelse {
|
||||
try stdout.print("No subcommand provided\n", .{});
|
||||
return;
|
||||
};
|
||||
|
||||
var buf: [128]u8 = undefined;
|
||||
while (true) {
|
||||
var client_address: net.Address = undefined;
|
||||
var client_address_len: posix.socklen_t = @sizeOf(net.Address);
|
||||
const socket = posix.accept(listener, &client_address.any, &client_address_len, 0) catch |err| {
|
||||
// Rare that this happens, but in later parts we'll
|
||||
// see examples where it does.
|
||||
std.debug.print("error accept: {}\n", .{err});
|
||||
continue;
|
||||
};
|
||||
defer posix.close(socket);
|
||||
|
||||
std.debug.print("{} connected\n", .{client_address});
|
||||
|
||||
const timeout = posix.timeval{.tv_sec = 2, .tv_usec = 500_000};
|
||||
try posix.setsockopt(socket, posix.SOL.SOCKET, posix.SO.RCVTIMEO, &std.mem.toBytes(timeout));
|
||||
try posix.setsockopt(socket, posix.SOL.SOCKET, posix.SO.SNDTIMEO, &std.mem.toBytes(timeout));
|
||||
|
||||
// we've changed everything from this point on
|
||||
const stream = std.net.Stream{.handle = socket};
|
||||
|
||||
const read = try stream.read(&buf);
|
||||
if (read == 0) {
|
||||
continue;
|
||||
}
|
||||
try stream.writeAll(buf[0..read]);
|
||||
if (eql(u8, firstArg, "list")) {
|
||||
try listEntries();
|
||||
} else if (eql(u8, firstArg, "create")) {
|
||||
try createNewEntry();
|
||||
} else {
|
||||
try stdout.print("Unknown subcommand: {s}\n", .{firstArg});
|
||||
}
|
||||
}
|
||||
|
||||
fn createNewEntry() !void {
|
||||
// TODO
|
||||
}
|
||||
|
||||
fn listEntries() !void {
|
||||
const file = try std.fs.cwd().openFile("entries.log.gmi", .{});
|
||||
defer file.close();
|
||||
|
||||
var bufReader = std.io.bufferedReader(file.reader());
|
||||
var inStream = bufReader.reader();
|
||||
|
||||
var buf: [1024]u8 = undefined;
|
||||
while (try inStream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
|
||||
try stdout.print("{s}\n", .{ line });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
src/root.zig
10
src/root.zig
|
@ -1,10 +0,0 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
10
zig-out/bin/entries.log.gmi
Normal file
10
zig-out/bin/entries.log.gmi
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Server log
|
||||
|
||||
## 2024-12-04 23:34 UTC
|
||||
This is a simple entry, with multiple lines of text. This should showcase how this is intended.
|
||||
|
||||
## 2024-12-04 23:36 UTC
|
||||
Another entry. This is a good start and template what to build with Zig. Sadly, twins can not redirect the path to the command, so I can not do the routing in the command and it need to be inside the twins config file. But I am fine with that for now.
|
||||
|
||||
## 2024-12-04 23:38 UTC
|
||||
What's missing is checking for a client certificate. I am not sure if twins is supporting this. Maybe open up an issue in the bugtracker?
|
Loading…
Reference in a new issue