-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract the server-side mini-framework to its own repo
- Loading branch information
Showing
17 changed files
with
132 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,68 @@ | ||
const db = @import("../db.zig"); | ||
const server = @import("../server.zig"); | ||
const tk = @import("tokamak"); | ||
|
||
pub fn @"GET /chat"(ctx: *server.Context) !void { | ||
pub fn @"GET /chat"(r: *tk.Responder) !void { | ||
var stmt = try db.query( | ||
\\SELECT id, name, | ||
\\(SELECT content FROM ChatMessage WHERE chat_id = Chat.id ORDER BY id DESC LIMIT 1) as last_message | ||
\\FROM Chat ORDER BY id DESC | ||
, .{}); | ||
defer stmt.deinit(); | ||
|
||
return ctx.sendJson(stmt.iterator(struct { id: u32, name: []const u8, last_message: ?[]const u8 })); | ||
return r.sendJson(stmt.iterator(struct { id: u32, name: []const u8, last_message: ?[]const u8 })); | ||
} | ||
|
||
pub fn @"POST /chat"(ctx: *server.Context) !void { | ||
const data = try ctx.readJson(struct { | ||
name: []const u8, | ||
prompt: ?[]const u8, | ||
}); | ||
|
||
pub fn @"POST /chat"(r: *tk.Responder, data: db.Chat) !void { | ||
var stmt = try db.query("INSERT INTO Chat (name, prompt) VALUES (?, ?) RETURNING *", .{ data.name, data.prompt }); | ||
defer stmt.deinit(); | ||
|
||
try ctx.sendJson(try stmt.read(db.Chat)); | ||
try r.sendJson(try stmt.read(db.Chat)); | ||
} | ||
|
||
pub fn @"GET /chat/:id"(ctx: *server.Context, id: u32) !void { | ||
pub fn @"GET /chat/:id"(r: *tk.Responder, id: u32) !void { | ||
var stmt = try db.query("SELECT * FROM Chat WHERE id = ?", .{id}); | ||
defer stmt.deinit(); | ||
|
||
return ctx.sendJson(try stmt.read(db.Chat)); | ||
return r.sendJson(try stmt.read(db.Chat)); | ||
} | ||
|
||
pub fn @"PUT /chat/:id"(ctx: *server.Context, id: u32, data: db.Chat) !void { | ||
pub fn @"PUT /chat/:id"(r: *tk.Responder, id: u32, data: db.Chat) !void { | ||
try db.exec("UPDATE Chat SET name = ?, prompt = ? WHERE id = ?", .{ data.name, data.prompt, id }); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} | ||
|
||
pub fn @"GET /chat/:id/messages"(ctx: *server.Context, id: u32) !void { | ||
pub fn @"GET /chat/:id/messages"(r: *tk.Responder, id: u32) !void { | ||
var stmt = try db.query("SELECT * FROM ChatMessage WHERE chat_id = ? ORDER BY id", .{id}); | ||
defer stmt.deinit(); | ||
|
||
return ctx.sendJson(stmt.iterator(db.ChatMessage)); | ||
return r.sendJson(stmt.iterator(db.ChatMessage)); | ||
} | ||
|
||
pub fn @"POST /chat/:id/messages"(ctx: *server.Context, id: u32) !void { | ||
const data = try ctx.readJson(struct { | ||
role: []const u8, | ||
content: []const u8, | ||
}); | ||
|
||
pub fn @"POST /chat/:id/messages"(r: *tk.Responder, id: u32, data: db.ChatMessage) !void { | ||
var stmt = try db.query("INSERT INTO ChatMessage (chat_id, role, content) VALUES (?, ?, ?) RETURNING *", .{ id, data.role, data.content }); | ||
defer stmt.deinit(); | ||
|
||
try ctx.sendJson(try stmt.read(db.ChatMessage)); | ||
try r.sendJson(try stmt.read(db.ChatMessage)); | ||
} | ||
|
||
pub fn @"GET /chat/:id/messages/:message_id"(ctx: *server.Context, id: u32, message_id: u32) !void { | ||
pub fn @"GET /chat/:id/messages/:message_id"(r: *tk.Responder, id: u32, message_id: u32) !void { | ||
var stmt = try db.query("SELECT * FROM ChatMessage WHERE id = ? AND chat_id = ?", .{ message_id, id }); | ||
defer stmt.deinit(); | ||
|
||
return ctx.sendJson(try stmt.read(db.ChatMessage)); | ||
return r.sendJson(try stmt.read(db.ChatMessage)); | ||
} | ||
|
||
pub fn @"PUT /chat/:id/messages/:message_id"(ctx: *server.Context, id: u32, message_id: u32) !void { | ||
const data = try ctx.readJson(struct { | ||
role: []const u8, | ||
content: []const u8, | ||
}); | ||
|
||
pub fn @"PUT /chat/:id/messages/:message_id"(r: *tk.Responder, id: u32, message_id: u32, data: db.ChatMessage) !void { | ||
try db.exec("UPDATE ChatMessage SET role = ?, content = ? WHERE id = ? AND chat_id = ?", .{ data.role, data.content, message_id, id }); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} | ||
|
||
pub fn @"DELETE /chat/:id/messages/:message_id"(ctx: *server.Context, id: u32, message_id: u32) !void { | ||
pub fn @"DELETE /chat/:id/messages/:message_id"(r: *tk.Responder, id: u32, message_id: u32) !void { | ||
try db.exec("DELETE FROM ChatMessage WHERE id = ? AND chat_id = ?", .{ message_id, id }); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} | ||
|
||
pub fn @"DELETE /chat/:id"(ctx: *server.Context, id: u32) !void { | ||
pub fn @"DELETE /chat/:id"(r: *tk.Responder, id: u32) !void { | ||
try db.exec("DELETE FROM Chat WHERE id = ?", .{id}); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
const server = @import("../server.zig"); | ||
const tk = @import("tokamak"); | ||
const util = @import("../util.zig"); | ||
|
||
pub fn @"GET /log"(ctx: *server.Context) !void { | ||
try ctx.sendChunk(try util.Logger.dump(ctx.arena)); | ||
} | ||
pub const @"GET /log" = util.Logger.dump; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,43 @@ | ||
const std = @import("std"); | ||
const db = @import("../db.zig"); | ||
const server = @import("../server.zig"); | ||
const tk = @import("tokamak"); | ||
const util = @import("../util.zig"); | ||
|
||
pub fn @"GET /models"(ctx: *server.Context) !void { | ||
pub fn @"GET /models"(allocator: std.mem.Allocator, r: *tk.Responder) !void { | ||
var stmt = try db.query("SELECT * FROM Model ORDER BY id", .{}); | ||
defer stmt.deinit(); | ||
|
||
var rows = std.ArrayList(struct { id: u32, name: []const u8, path: []const u8, imported: bool, size: ?u64 }).init(ctx.arena); | ||
var rows = std.ArrayList(struct { id: u32, name: []const u8, path: []const u8, imported: bool, size: ?u64 }).init(allocator); | ||
var it = stmt.iterator(db.Model); | ||
while (try it.next()) |m| { | ||
try rows.append(.{ | ||
.id = m.id, | ||
.name = try ctx.arena.dupe(u8, m.name), | ||
.path = try ctx.arena.dupe(u8, m.path), | ||
.id = m.id.?, | ||
.name = try allocator.dupe(u8, m.name), | ||
.path = try allocator.dupe(u8, m.path), | ||
.imported = m.imported, | ||
.size = util.getFileSize(m.path) catch null, | ||
}); | ||
} | ||
|
||
return ctx.sendJson(rows.items); | ||
return r.sendJson(rows.items); | ||
} | ||
|
||
pub fn @"POST /models"(ctx: *server.Context) !void { | ||
const data = try ctx.readJson(struct { | ||
name: []const u8, | ||
path: []const u8, | ||
imported: bool = false, | ||
}); | ||
|
||
pub fn @"POST /models"(r: *tk.Responder, data: db.Model) !void { | ||
var stmt = try db.query("INSERT INTO Model (name, path, imported) VALUES (?, ?, ?) RETURNING *", .{ data.name, data.path, data.imported }); | ||
defer stmt.deinit(); | ||
|
||
try ctx.sendJson(try stmt.read(db.Model)); | ||
try r.sendJson(try stmt.read(db.Model)); | ||
} | ||
|
||
pub fn @"PUT /models/:id"(ctx: *server.Context, id: u32, data: db.Model) !void { | ||
pub fn @"PUT /models/:id"(r: *tk.Responder, id: u32, data: db.Model) !void { | ||
try db.exec("UPDATE Model SET name = ?, path = ? WHERE id = ?", .{ data.name, data.path, id }); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} | ||
|
||
pub fn @"DELETE /models/:id"(ctx: *server.Context, id: []const u8) !void { | ||
const path = try db.getString(ctx.arena, "SELECT path FROM Model WHERE id = ?", .{id}); | ||
pub fn @"DELETE /models/:id"(allocator: std.mem.Allocator, r: *tk.Responder, id: []const u8) !void { | ||
const path = try db.getString(allocator, "SELECT path FROM Model WHERE id = ?", .{id}); | ||
const imported = try db.get(bool, "SELECT imported FROM Model WHERE id = ?", .{id}); | ||
try db.exec("DELETE FROM Model WHERE id = ?", .{id}); | ||
if (!imported) std.fs.deleteFileAbsolute(path) catch {}; | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,21 @@ | ||
const db = @import("../db.zig"); | ||
const server = @import("../server.zig"); | ||
const tk = @import("tokamak"); | ||
|
||
pub fn @"GET /prompts"(ctx: *server.Context) !void { | ||
pub fn @"GET /prompts"(r: *tk.Responder) !void { | ||
var stmt = try db.query("SELECT * FROM Prompt ORDER BY id", .{}); | ||
defer stmt.deinit(); | ||
|
||
return ctx.sendJson(stmt.iterator(db.Prompt)); | ||
return r.sendJson(stmt.iterator(db.Prompt)); | ||
} | ||
|
||
pub fn @"POST /prompts"(ctx: *server.Context) !void { | ||
const data = try ctx.readJson(struct { | ||
name: []const u8, | ||
prompt: []const u8, | ||
}); | ||
|
||
pub fn @"POST /prompts"(r: *tk.Responder, data: db.Prompt) !void { | ||
var stmt = try db.query("INSERT INTO Prompt (name, prompt) VALUES (?, ?) RETURNING *", .{ data.name, data.prompt }); | ||
defer stmt.deinit(); | ||
|
||
try ctx.sendJson(try stmt.read(db.Prompt)); | ||
try r.sendJson(try stmt.read(db.Prompt)); | ||
} | ||
|
||
pub fn @"DELETE /prompts/:id"(ctx: *server.Context, id: u32) !void { | ||
pub fn @"DELETE /prompts/:id"(r: *tk.Responder, id: u32) !void { | ||
try db.exec("DELETE FROM Prompt WHERE id = ?", .{id}); | ||
return ctx.noContent(); | ||
return r.noContent(); | ||
} |
Oops, something went wrong.