Merge branch 'testing' of https://github.com/mitteneer/zuletzt into testing
This commit is contained in:
commit
ee855dde18
8 changed files with 120 additions and 8 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,4 +2,4 @@ zig-out/
|
||||||
zig-cache/
|
zig-cache/
|
||||||
*.core
|
*.core
|
||||||
static/
|
static/
|
||||||
.jetzig
|
.jetzig
|
||||||
12
build.zig
12
build.zig
|
|
@ -15,10 +15,20 @@ pub fn build(b: *std.Build) !void {
|
||||||
// Example dependency:
|
// Example dependency:
|
||||||
const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
||||||
exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas"));
|
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.
|
// 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);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,17 @@
|
||||||
// internet connectivity.
|
// internet connectivity.
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.jetzig = .{
|
.jetzig = .{
|
||||||
.url = "https://github.com/jetzig-framework/jetzig/archive/31e8ae36e1d7f68166114eefb867c23df352d537.tar.gz",
|
.url = "https://github.com/jetzig-framework/jetzig/archive/68ca4d3bbad05880e6e38fc6d10e7a24e772081e.tar.gz",
|
||||||
.hash = "1220d368ce21549ccd721d5f303888986fff5714835b1f266a62e329f85eabec8f19",
|
.hash = "122047ebabab1dfe1bb7f96ffdac98410ffb4850b06dc6b1670ce74aaeaa49bdc08e",
|
||||||
},
|
},
|
||||||
.iguanas = .{
|
.iguanas = .{
|
||||||
.url = "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz",
|
.url = "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz",
|
||||||
.hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c",
|
.hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c",
|
||||||
},
|
},
|
||||||
|
.sqlite = .{
|
||||||
|
.url = "https://github.com/vrischmann/zig-sqlite/archive/6af21bb.tar.gz",
|
||||||
|
.hash = "12202799861d92c7880d1a74f95be099752d7f0420a0c9628dc923ca2b1a22b7bda8",
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
// This makes *all* files, recursively, included in this package. It is generally
|
// This makes *all* files, recursively, included in this package. It is generally
|
||||||
|
|
|
||||||
BIN
src/app/database/data.db
Normal file
BIN
src/app/database/data.db
Normal file
Binary file not shown.
|
|
@ -1,3 +1,4 @@
|
||||||
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
/// `src/app/views/root.zig` represents the root URL `/`
|
/// `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();
|
var root = try data.object();
|
||||||
|
|
||||||
// Add a string to the root 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
|
// 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
|
// 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.
|
// present.
|
||||||
const params = try request.params();
|
const params = try request.params();
|
||||||
|
|
||||||
if (params.get("message")) |value| {
|
try root.put("message_param", params.get("message"));
|
||||||
try root.put("message_param", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set arbitrary response headers as required. `content-type` is automatically assigned for
|
// Set arbitrary response headers as required. `content-type` is automatically assigned for
|
||||||
// HTML, JSON responses.
|
// HTML, JSON responses.
|
||||||
|
|
|
||||||
12
src/app/views/search.zig
Normal file
12
src/app/views/search.zig
Normal 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
54
src/db.zig
Normal 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});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
33
src/main.zig
33
src/main.zig
|
|
@ -4,6 +4,8 @@ pub const jetzig = @import("jetzig");
|
||||||
|
|
||||||
pub const routes = @import("routes");
|
pub const routes = @import("routes");
|
||||||
|
|
||||||
|
pub const sqlite = @import("sqlite");
|
||||||
|
|
||||||
// Override default settings in `jetzig.config` here:
|
// Override default settings in `jetzig.config` here:
|
||||||
pub const jetzig_options = struct {
|
pub const jetzig_options = struct {
|
||||||
/// Middleware chain. Add any custom middleware here, or use middleware provided in
|
/// 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 {
|
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(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer std.debug.assert(gpa.deinit() == .ok);
|
defer std.debug.assert(gpa.deinit() == .ok);
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue