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 4e00f0d..e1908b8 100755 --- a/db/item.go +++ b/db/item.go @@ -44,6 +44,12 @@ 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) } @@ -53,6 +59,13 @@ func (i Item) PrettyName() string { } 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, COALESCE(p.uploaded, f.modified), s.downloads, s.views FROM packages p JOIN stats s ON p.id = s.pid @@ -63,6 +76,7 @@ func GetItemList(ctx context.Context, tag string, query string) ([]Item, error) AND modified < TIMESTAMP'2015-01-01 00:00:00' GROUP BY pid) f ON p.id = f.pid` + var args []any if tag != "" { @@ -116,6 +130,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)} q := `SELECT p.name, p.filename, p.description, p.size, p.uploader, COALESCE(p.uploaded, f.modified) 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 {