commit a2d683ec75c7202fd29dbc893f05a5bec22b0d38 Author: mskorczak Date: Fri Sep 13 17:48:02 2024 +0100 Initial commit/theft of web diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..5fcb78c --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +zig build diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..d1eca1b --- /dev/null +++ b/build.zig @@ -0,0 +1,99 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "jungle_zig", + // 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 = "jungle_zig", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + 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")); + exe.linkLibrary(zap.artifact("facil.io")); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + 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, + .optimize = optimize, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // 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); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..87d9ad1 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,71 @@ +.{ + .name = "jungle_zig", + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` 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, + //}, + .zap = .{ + .url = "https://github.com/zigzap/zap/archive/v0.8.0.tar.gz", + .hash = "12209936c3333b53b53edcf453b1670babb9ae8c2197b1ca627c01e72670e20c1a21", + }, + }, + + // 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. + // 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 = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..df78098 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh +./build.sh +./zig-out/bin/jungle_zig diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..2ac6ea1 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,98 @@ +const std = @import("std"); +const zap = @import("zap"); +const Allocator = std.mem.Allocator; +//fn dispatch_routes(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}); +// } +// if (r.path) |path| { +// if (routes.get(path)) |method| { +// method(r); +// return; +// } +// } +// r.setStatus(.not_found); +// r.sendBody("404 - File not found") catch return; +//} + +pub const JungleRouter = struct { + const Self = @This(); + allocator: Allocator, + + pub fn init(allocator: Allocator) Self { + return .{ .allocator = allocator }; + } + pub fn index(self: *Self, req: zap.Request) void { + std.log.warn("index", .{}); + + const string = std.fmt.allocPrint( + self.allocator, + "Test", + .{}, + ) catch return; + defer self.allocator.free(string); + req.sendFile("src/public/index.html") catch return; + } + + pub fn home(self: *Self, req: zap.Request) void { + std.log.warn("home", .{}); + + const string = std.fmt.allocPrint( + self.allocator, + "HOME!!!", + .{}, + ) catch return; + defer self.allocator.free(string); + req.sendBody(string) catch return; + } + + //pub fn blog(self: *Self, req: zap.Request) void { + // std.log.warn("blog", .{}); + // const template = + // \\ {{=<< >>=}} + // \\ * Files: + // \\ <<#files>> + // \\ <<<& name>> (<>) + // \\ <> + // ; + // var mustache = Mustache.fromData(template) catch return; + // defer mustache.deinit(); + //} + +}; + +//fn route_git() void {} +//fn route_blog() void {} +//fn route_resume() void {} +//fn route_contact() void {} + +fn not_found(req: zap.Request) void { + req.sendBody("404 - Not Found") catch return; +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{ + .thread_safe = true, + }){}; + const allocator = gpa.allocator(); + var router = zap.Router.init(allocator, .{ + .not_found = not_found, + }); + defer router.deinit(); + var jungle_router = JungleRouter.init(allocator); + try router.handle_func("/", &jungle_router, &JungleRouter.index); + try router.handle_func("/home", &jungle_router, &JungleRouter.home); + var listener = zap.HttpListener.init(.{ .port = 3000, .on_request = router.on_request_handler(), .log = true, .max_clients = 100000, .public_folder = "src/public" }); + 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 + }); +} diff --git a/src/public/blog/blog_1.html b/src/public/blog/blog_1.html new file mode 100644 index 0000000..281bbd1 --- /dev/null +++ b/src/public/blog/blog_1.html @@ -0,0 +1,3 @@ +

blog 1

+back +heres some test text for blog 1 diff --git a/src/public/blog/blog_2.html b/src/public/blog/blog_2.html new file mode 100644 index 0000000..2293ad1 --- /dev/null +++ b/src/public/blog/blog_2.html @@ -0,0 +1,3 @@ +

blog 2

+back +heres some test text for blog 2 diff --git a/src/public/blog/blog_template.html b/src/public/blog/blog_template.html new file mode 100644 index 0000000..0819927 --- /dev/null +++ b/src/public/blog/blog_template.html @@ -0,0 +1,3 @@ +

blog title

+back +heres some test text diff --git a/src/public/home.html b/src/public/home.html new file mode 100644 index 0000000..3fe86ba --- /dev/null +++ b/src/public/home.html @@ -0,0 +1 @@ +

HOME

diff --git a/src/public/index.html b/src/public/index.html new file mode 100644 index 0000000..9d8467a --- /dev/null +++ b/src/public/index.html @@ -0,0 +1,29 @@ + + + + + mskor + + + + + +
+

mskor

+
+ +
+
+ + +
+
+ + + diff --git a/src/public/style.css b/src/public/style.css new file mode 100644 index 0000000..139f60b --- /dev/null +++ b/src/public/style.css @@ -0,0 +1,48 @@ +html { + max-width:70%; + margin:40px auto; + font-size:18px; + line-height:1.4; + padding:0 10px; + font-family: monospace; +} + +a { + text-decoration:none; + color:MediumSeaGreen; +} + +td { + font-size:14px; + padding:0 5px; +} + +h1 { + padding:0 0px; + margin:0px auto; +} + +img { + width:100%; + height:100%; +} + +@media (prefers-color-scheme:dark) { + body { + background-color:#222; + color:#fff; + } + tr:hover { + background-color:#333; + } +} + +@media (prefers-color-scheme: light) { + body { + background-color:#fff; + color:#444; + } + tr:hover { + background-color:#eee; + } +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..ecfeade --- /dev/null +++ b/src/root.zig @@ -0,0 +1,10 @@ +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); +} diff --git a/tags b/tags new file mode 100644 index 0000000..d14b772 --- /dev/null +++ b/tags @@ -0,0 +1,61 @@ +!_TAG_EXTRA_DESCRIPTION anonymous /Include tags for non-named objects like lambda/ +!_TAG_EXTRA_DESCRIPTION fileScope /Include tags of file scope/ +!_TAG_EXTRA_DESCRIPTION pseudo /Include pseudo tags/ +!_TAG_EXTRA_DESCRIPTION subparser /Include tags generated by subparsers/ +!_TAG_FIELD_DESCRIPTION epoch /the last modified time of the input file (only for F\/file kind tag)/ +!_TAG_FIELD_DESCRIPTION file /File-restricted scoping/ +!_TAG_FIELD_DESCRIPTION input /input file/ +!_TAG_FIELD_DESCRIPTION name /tag name/ +!_TAG_FIELD_DESCRIPTION pattern /pattern/ +!_TAG_FIELD_DESCRIPTION typeref /Type and name of a variable or typedef/ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_KIND_DESCRIPTION!CSS c,class /classes/ +!_TAG_KIND_DESCRIPTION!CSS i,id /identities/ +!_TAG_KIND_DESCRIPTION!CSS s,selector /selectors/ +!_TAG_KIND_DESCRIPTION!HTML C,stylesheet /stylesheets/ +!_TAG_KIND_DESCRIPTION!HTML I,id /identifiers/ +!_TAG_KIND_DESCRIPTION!HTML J,script /scripts/ +!_TAG_KIND_DESCRIPTION!HTML a,anchor /named anchors/ +!_TAG_KIND_DESCRIPTION!HTML c,class /classes/ +!_TAG_KIND_DESCRIPTION!HTML h,heading1 /H1 headings/ +!_TAG_KIND_DESCRIPTION!HTML i,heading2 /H2 headings/ +!_TAG_KIND_DESCRIPTION!HTML j,heading3 /H3 headings/ +!_TAG_KIND_DESCRIPTION!HTML t,title /titles/ +!_TAG_KIND_DESCRIPTION!Sh a,alias /aliases/ +!_TAG_KIND_DESCRIPTION!Sh f,function /functions/ +!_TAG_KIND_DESCRIPTION!Sh h,heredoc /label for here document/ +!_TAG_KIND_DESCRIPTION!Sh s,script /script files/ +!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/ +!_TAG_OUTPUT_FILESEP slash /slash or backslash/ +!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/ +!_TAG_OUTPUT_VERSION 0.0 /current.age/ +!_TAG_PARSER_VERSION!CSS 0.0 /current.age/ +!_TAG_PARSER_VERSION!HTML 0.0 /current.age/ +!_TAG_PARSER_VERSION!Sh 0.0 /current.age/ +!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/ +!_TAG_PROC_CWD /home/mike/prog/web/ // +!_TAG_PROGRAM_AUTHOR Universal Ctags Team // +!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ +!_TAG_PROGRAM_URL https://ctags.io/ /official site/ +!_TAG_PROGRAM_VERSION 6.1.0 /v6.1.0/ +!_TAG_ROLE_DESCRIPTION!HTML!class attribute /assigned as attributes/ +!_TAG_ROLE_DESCRIPTION!HTML!script extFile /referenced as external files/ +!_TAG_ROLE_DESCRIPTION!HTML!stylesheet extFile /referenced as external files/ +!_TAG_ROLE_DESCRIPTION!Sh!heredoc endmarker /end marker/ +!_TAG_ROLE_DESCRIPTION!Sh!script loaded /loaded/ +HOME src/public/home.html /^

HOME<\/h1>$/;" h +a src/public/style.css /^a {$/;" s +blog 1 src/public/blog/blog_1.html /^

blog 1<\/h3>$/;" j +blog 2 src/public/blog/blog_2.html /^

blog 2<\/h3>$/;" j +blog title src/public/blog/blog_template.html /^

blog title<\/h3>$/;" j +body src/public/style.css /^ body {$/;" s +content src/public/index.html /^
$/;" I +css src/public/index.html /^ $/;" I +h1 src/public/style.css /^h1 {$/;" s +html src/public/style.css /^html {$/;" s +img src/public/style.css /^img { $/;" s +mskor src/public/index.html /^

mskor<\/h1>$/;" h +mskor src/public/index.html /^ mskor<\/title>$/;" j +td src/public/style.css /^td {$/;" s +tr:hover src/public/style.css /^ tr:hover {$/;" s diff --git a/zig-out/bin/jungle_zig b/zig-out/bin/jungle_zig new file mode 100755 index 0000000..694ea4b Binary files /dev/null and b/zig-out/bin/jungle_zig differ diff --git a/zig-out/lib/libjungle_zig.a b/zig-out/lib/libjungle_zig.a new file mode 100644 index 0000000..af92f85 Binary files /dev/null and b/zig-out/lib/libjungle_zig.a differ