db: Add plugin to access DB

Can now download the database at /db/catbase.db
This commit is contained in:
Chris Sexton 2018-02-22 17:41:48 -05:00
parent 13c00394f7
commit 0357e59858
2 changed files with 47 additions and 0 deletions

View File

@ -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()

45
plugins/db/db.go Normal file
View File

@ -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)
}