Initial commit/theft of web

This commit is contained in:
mskorczak 2024-09-13 17:48:02 +01:00
commit a2d683ec75
15 changed files with 431 additions and 0 deletions

2
build.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/bash
zig build

99
build.zig Normal file
View file

@ -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);
}

71
build.zig.zon Normal file
View file

@ -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 <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,
//},
.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",
},
}

3
run.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
./build.sh
./zig-out/bin/jungle_zig

98
src/main.zig Normal file
View file

@ -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>> (<<name>>)
// \\ <</files>>
// ;
// 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
});
}

View file

@ -0,0 +1,3 @@
<h3>blog 1</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text for blog 1

View file

@ -0,0 +1,3 @@
<h3>blog 2</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text for blog 2

View file

@ -0,0 +1,3 @@
<h3>blog title</h3>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">back</a>
heres some test text

1
src/public/home.html Normal file
View file

@ -0,0 +1 @@
<h1>HOME</h1>

29
src/public/index.html Normal file
View file

@ -0,0 +1,29 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, inital-scale=1">
<meta http-equiv="X-UR-Compatible" content='ie=edge'>
<title>mskor</title>
<meta name="keywords" content="mskor, rust, c, c++, developer, software engineer, back-end, full-stack, portfolio, blog">
<link rel="stylesheet" id="css" href="style.css">
<script src="https://unpkg.com/htmx.org@1.9.2" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"></script>
</head>
<header>
<h1>mskor</h1>
<hr>
<nav>
<a href="" hx-get="/home" hx-target="#content" hx-swap="innerHTML" hx-trigger="load">home</a>
<a href="" hx-get="/git" hx-target="#content" hx-swap="innerHTML">git</a>
<a href="" hx-get="/blog" hx-target="#content" hx-swap="innerHTML">blog</a>
<a href="" hx-get="/resume" hx-target="#content" hx-swap="innerHTML">resume</a>
<a href="" hx-get="/contact" hx-target="#content" hx-swap="innerHTML">contact</a>
</nav>
<hr>
</header>
<body>
<div id="content">
</div>
</body>
</html>

48
src/public/style.css Normal file
View file

@ -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;
}
}

10
src/root.zig Normal file
View file

@ -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);
}

61
tags Normal file
View file

@ -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 /^<h1>HOME<\/h1>$/;" h
a src/public/style.css /^a {$/;" s
blog 1 src/public/blog/blog_1.html /^<h3>blog 1<\/h3>$/;" j
blog 2 src/public/blog/blog_2.html /^<h3>blog 2<\/h3>$/;" j
blog title src/public/blog/blog_template.html /^<h3>blog title<\/h3>$/;" j
body src/public/style.css /^ body {$/;" s
content src/public/index.html /^<div id="content">$/;" I
css src/public/index.html /^ <link rel="stylesheet" id="css" href="style.css">$/;" 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 /^ <h1>mskor<\/h1>$/;" h
mskor src/public/index.html /^ <title>mskor<\/title>$/;" j
td src/public/style.css /^td {$/;" s
tr:hover src/public/style.css /^ tr:hover {$/;" s

BIN
zig-out/bin/jungle_zig Executable file

Binary file not shown.

BIN
zig-out/lib/libjungle_zig.a Normal file

Binary file not shown.