Merge branch 'testing' of https://github.com/mitteneer/zuletzt into testing

This commit is contained in:
mitteneer 2024-04-08 20:25:00 -04:00
commit ee855dde18
8 changed files with 120 additions and 8 deletions

2
.gitignore vendored
View file

@ -2,4 +2,4 @@ zig-out/
zig-cache/
*.core
static/
.jetzig
.jetzig

View file

@ -15,10 +15,20 @@ pub fn build(b: *std.Build) !void {
// Example dependency:
const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas"));
const sqlite = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("sqlite", sqlite.module("sqlite"));
// links the bundled sqlite3, so leave this out if you link the system one
exe.linkLibrary(sqlite.artifact("sqlite"));
// All dependencies **must** be added to imports above this line.
try jetzig.jetzigInit(b, exe, .{});
try jetzig.jetzigInit(b, exe, .{.zmpl_version = .v2});
b.installArtifact(exe);

View file

@ -16,13 +16,17 @@
// internet connectivity.
.dependencies = .{
.jetzig = .{
.url = "https://github.com/jetzig-framework/jetzig/archive/31e8ae36e1d7f68166114eefb867c23df352d537.tar.gz",
.hash = "1220d368ce21549ccd721d5f303888986fff5714835b1f266a62e329f85eabec8f19",
.url = "https://github.com/jetzig-framework/jetzig/archive/68ca4d3bbad05880e6e38fc6d10e7a24e772081e.tar.gz",
.hash = "122047ebabab1dfe1bb7f96ffdac98410ffb4850b06dc6b1670ce74aaeaa49bdc08e",
},
.iguanas = .{
.url = "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz",
.hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c",
},
.sqlite = .{
.url = "https://github.com/vrischmann/zig-sqlite/archive/6af21bb.tar.gz",
.hash = "12202799861d92c7880d1a74f95be099752d7f0420a0c9628dc923ca2b1a22b7bda8",
}
},
.paths = .{
// This makes *all* files, recursively, included in this package. It is generally

BIN
src/app/database/data.db Normal file

Binary file not shown.

View file

@ -1,3 +1,4 @@
const std = @import("std");
const jetzig = @import("jetzig");
/// `src/app/views/root.zig` represents the root URL `/`
@ -18,7 +19,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
// Add a string to the root object.
try root.put("message", data.string("Welcome to Jetzig!"));
try root.put("welcome_message", data.string("Welcome to Jetzig!"));
// Request params have the same type as a `data.object()` so they can be inserted them
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
@ -26,9 +27,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
// present.
const params = try request.params();
if (params.get("message")) |value| {
try root.put("message_param", value);
}
try root.put("message_param", params.get("message"));
// Set arbitrary response headers as required. `content-type` is automatically assigned for
// HTML, JSON responses.

12
src/app/views/search.zig Normal file
View file

@ -0,0 +1,12 @@
const std = @import("std");
const jetzig = @import("jetzig");
const search = @import("../../db.zig");
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
const params = try request.params();
const query = params.get("q");
try root.put("q",query);
//try root.put("q", data.string("Welcome"));
return request.render(.ok);
}

54
src/db.zig Normal file
View file

@ -0,0 +1,54 @@
const std = @import("std");
const sql = @import("sqlite");
pub fn loadDb(path: []u8) !sql.sqlite.Db{
try sql.sqlite.Db.init(.{
.mode = sql.sqlite.Db.Mode{ .File = path},
.open_flags = .{
.write = true,
.create = true,
},
.threading_mode = .MultiThread,
});
}
const addArtist = \\INSERT INTO artists ('artist', 'plays', 'url') VALUES (?,?)
;
const addTrack = \\INSERT INTO tracks ('artist', 'track', 'album', 'plays', 'url') VALUES (?,?,?,?)
;
const getArtist = \\SELECT artist, plays FROM artists WHERE artist == ?
;
const getTrack = \\SELECT artist, track, album, plays FROM tracks WHERE track == ?
;
const getTrackSearch = \\SELECT url FROM artists WHERE artist == ?
;
const getArtistSearch = \\SELECT url FROM artists WHERE artist == ?
;
pub var db = loadDb("/home/swebb/Source/zuletzt/src/app/database/data.db");
pub fn search(query: []const u8) !void{
var artistSearch = try db.prepare(getArtistSearch);
defer artistSearch.deinit();
var trackSearch = try db.prepare(getTrackSearch);
defer trackSearch.deinit();
const artistResults = try artistSearch.one(
struct {
artist: [128:0]u8,
plays: usize,
},
.{},
.{ .artist = query},
);
if (artistResults) |r|{
std.log.debug("Artist: {}, Plays: {}", .{r.name, r.plays});
}
}

View file

@ -4,6 +4,8 @@ pub const jetzig = @import("jetzig");
pub const routes = @import("routes");
pub const sqlite = @import("sqlite");
// Override default settings in `jetzig.config` here:
pub const jetzig_options = struct {
/// Middleware chain. Add any custom middleware here, or use middleware provided in
@ -85,6 +87,37 @@ pub const jetzig_options = struct {
};
pub fn main() !void {
//var db = try sqlite.Db.init(.{
// .mode = sqlite.Db.Mode{ .File = "/home/swebb/Source/zuletzt/src/app/database/data.db" },
// .open_flags = .{
// .write = true,
// .create = true,
// },
// .threading_mode = .MultiThread,
//});
//const create =
// \\CREATE TABLE artists ('artist', 'plays')
//;
//const query =
// \\INSERT INTO artists ('artist', 'plays') VALUES (?,?)
//;
//var build = try db.prepare(create);
//defer build.deinit();
//try build.exec(.{},.{});
//var stmt = try db.prepare(query);
//defer stmt.deinit();
//try stmt.exec(.{}, .{
// .artist = "Wilco",
// .plays = 2500,
//});
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();