From c034076956f732606d21075c6b80539d1dde41e8 Mon Sep 17 00:00:00 2001 From: AllMeatball <181806857+AllMeatball@users.noreply.github.com> Date: Thu, 11 Jun 2026 18:14:03 -0500 Subject: [PATCH] Add offline test database protocol Makes it easier to debug and add features without the whole addon database --- db/init.go | 12 +++++++++++- db/item.go | 15 +++++++++++++++ db/tag.go | 10 ++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/db/init.go b/db/init.go index a4cc587..a220819 100755 --- a/db/init.go +++ b/db/init.go @@ -25,11 +25,21 @@ import ( _ "github.com/go-sql-driver/mysql" ) -var conn *sql.DB +var conn *sql.DB = nil +var protocol_type = "" + +func GetProtocol() string { + return protocol_type +} func Init(username string, password string, protocol string, address string, database string) error { var err error + protocol_type = protocol + if protocol == "none" { + return nil + } + conn, err = sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s)/%s?parseTime=true", username, password, protocol, address, database)) if err != nil { return err diff --git a/db/item.go b/db/item.go index 8ac0c44..7743f42 100755 --- a/db/item.go +++ b/db/item.go @@ -43,11 +43,24 @@ type Item struct { var ErrInvalidID = errors.New("download id not found") +var TEST_ITEM = Item{ + Name: "missingname", + Filename: "missingname.zip", + Size: 1337 * (1024 * 1024), +} + func (i Item) PrettySize() string { return fmt.Sprintf("%.02f MB", float64(i.Size)/1024/1024) } func GetItemList(ctx context.Context, tag string, query string) ([]Item, error) { + if (GetProtocol() == "test") { return []Item{ + TEST_ITEM, + TEST_ITEM, + TEST_ITEM, + TEST_ITEM, + }, nil } + q := "SELECT p.id, p.name, p.filename, p.description, p.size, p.uploader, p.uploaded, s.downloads, s.views FROM packages p JOIN stats s ON p.id = s.pid" var args []any @@ -86,6 +99,8 @@ func GetItemList(ctx context.Context, tag string, query string) ([]Item, error) } func GetItem(ctx context.Context, id int) (Item, error) { + if (GetProtocol() == "test") { return TEST_ITEM, nil } + item := Item{ID: id, Images: make(map[int]string)} err := conn.QueryRowContext(ctx, "SELECT name, filename, description, size, uploader, uploaded FROM packages WHERE id = ?", id).Scan(&item.Name, &item.Filename, &item.Description, &item.Size, &item.Uploader, &item.Uploaded) diff --git a/db/tag.go b/db/tag.go index 4263900..3f6ebf4 100644 --- a/db/tag.go +++ b/db/tag.go @@ -21,6 +21,16 @@ package db import "context" func GetPopularTags(ctx context.Context) ([]string, error) { + if (GetProtocol() == "test") { return []string{ + "garrysmod", + "1337", + "hax", + "lol", + "lua", + "is", + "awesome", + }, nil } + var tags []string rows, err := conn.QueryContext(ctx, "SELECT tag FROM tags GROUP BY tag ORDER BY COUNT(*) DESC, tag ASC LIMIT 100") if err != nil {