From 0357e5985857199c5c6df92400909eef017e09d1 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 22 Feb 2018 17:41:48 -0500 Subject: [PATCH] db: Add plugin to access DB Can now download the database at /db/catbase.db --- main.go | 2 ++ plugins/db/db.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 plugins/db/db.go diff --git a/main.go b/main.go index 4ab7e1b..c64dd3d 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/velour/catbase/plugins/babbler" "github.com/velour/catbase/plugins/beers" "github.com/velour/catbase/plugins/counter" + "github.com/velour/catbase/plugins/db" "github.com/velour/catbase/plugins/dice" "github.com/velour/catbase/plugins/emojifyme" "github.com/velour/catbase/plugins/fact" @@ -79,6 +80,7 @@ func main() { b.AddHandler("tell", tell.New(b)) // catches anything left, will always return true b.AddHandler("factoid", fact.New(b)) + b.AddHandler("db", db.New(b)) for { err := client.Serve() diff --git a/plugins/db/db.go b/plugins/db/db.go new file mode 100644 index 0000000..bff767b --- /dev/null +++ b/plugins/db/db.go @@ -0,0 +1,45 @@ +package db + +import ( + "fmt" + "log" + "net/http" + "os" + "time" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/config" +) + +type DBPlugin struct { + bot bot.Bot + config *config.Config +} + +func New(b bot.Bot) *DBPlugin { + return &DBPlugin{b, b.Config()} +} + +func (p *DBPlugin) Message(message msg.Message) bool { return false } +func (p *DBPlugin) Event(kind string, message msg.Message) bool { return false } +func (p *DBPlugin) ReplyMessage(msg.Message, string) bool { return false } +func (p *DBPlugin) BotMessage(message msg.Message) bool { return false } +func (p *DBPlugin) Help(channel string, parts []string) {} + +func (p *DBPlugin) RegisterWeb() *string { + http.HandleFunc("/db/catbase.db", p.serveQuery) + tmp := "/db/catbase.db" + return &tmp +} + +func (p *DBPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { + f, err := os.Open(p.bot.Config().DB.File) + defer f.Close() + if err != nil { + log.Printf("Error opening DB for web service: %s", err) + fmt.Fprintf(w, "Error opening DB") + return + } + http.ServeContent(w, r, "catbase.db", time.Now(), f) +}