From abff4760fe4aede34b60fbefc8f398ad2370304e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 7 Dec 2015 17:22:51 +0100 Subject: renamed RDDB to RdDb and added code to fetch show infos based on ID diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 5eea054..2b512ed 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -35,10 +35,11 @@ import ( type ImportContext struct { *Config - *RDDB + *RdDb UserName string Password string Trusted bool + ShowId int GroupName string Cart int Cut int @@ -51,13 +52,14 @@ type ImportContext struct { DeleteSourceFile bool } -func NewImportContext(conf *Config, rddb *RDDB, user string, group string) *ImportContext { +func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *ImportContext { ctx := new(ImportContext) ctx.Config = conf - ctx.RDDB = rddb + ctx.RdDb = rddb ctx.UserName = user ctx.Password = "" ctx.Trusted = false + ctx.ShowId = 0 ctx.GroupName = group ctx.Cart = 0 ctx.Cut = 0 @@ -76,7 +78,7 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { req.username = ctx.UserName req.cached = cached req.response = make(chan getPasswordResult) - ctx.RDDB.getPasswordChan <- req + ctx.RdDb.getPasswordChan <- req res := <-req.response if res.err != nil { @@ -87,6 +89,21 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { return } +func (ctx *ImportContext) getShowInfo() (err error) { + req := getShowInfoRequest{} + req.showid = ctx.ShowId + req.response = make(chan getShowInfoResult) + ctx.RdDb.getShowInfoChan <- req + + res := <-req.response + if res.err != nil { + err = res.err + return + } + rhdl.Printf("Title of show %d is '%s'", ctx.ShowId, res.title) + return +} + // func import_audio(url, file string) (err error) { // var b bytes.Buffer @@ -142,6 +159,11 @@ func ImportFile(ctx *ImportContext) (err error) { return } } + + if err = ctx.getShowInfo(); err != nil { + return + } + rhdl.Printf("%+v", ctx) return diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index 2a87388..dd1dfec 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -41,35 +41,50 @@ type getPasswordRequest struct { response chan getPasswordResult } -type RDDB struct { - dbh *sql.DB - password_cache map[string]string - getPasswordChan chan getPasswordRequest - dbGetPasswordStmt *sql.Stmt - quit chan bool - done chan bool +type getShowInfoResult struct { + title string + carts map[int]int + err error } -func (self *RDDB) init(conf *Config) (err error) { +type getShowInfoRequest struct { + showid int + response chan getShowInfoResult +} + +type RdDb struct { + dbh *sql.DB + password_cache map[string]string + getPasswordChan chan getPasswordRequest + getPasswordStmt *sql.Stmt + getShowInfoChan chan getShowInfoRequest + getShowInfoStmt *sql.Stmt + quit chan bool + done chan bool +} + +func (self *RdDb) init(conf *Config) (err error) { dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", conf.db_user, conf.db_passwd, conf.db_host, conf.db_db) if self.dbh, err = sql.Open("mysql", dsn); err != nil { return } - if self.dbGetPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?"); err != nil { + if self.getPasswordStmt, err = self.dbh.Prepare("select PASSWORD from USERS where LOGIN_NAME = ?;"); err != nil { + return + } + if self.getShowInfoStmt, err = self.dbh.Prepare("select TITLE,MACROS from CART where NUMBER = ?;"); err != nil { return } - return } -func (self *RDDB) getPassword(username string, cached bool) (pwd string, err error) { +func (self *RdDb) getPassword(username string, cached bool) (pwd string, err error) { if cached { pwd = self.password_cache[username] } if pwd == "" { - err = self.dbGetPasswordStmt.QueryRow(username).Scan(&pwd) + err = self.getPasswordStmt.QueryRow(username).Scan(&pwd) if err != nil { if err == sql.ErrNoRows { err = fmt.Errorf("user '%s' not known by rivendell", username) @@ -82,7 +97,23 @@ func (self *RDDB) getPassword(username string, cached bool) (pwd string, err err return } -func (self *RDDB) dispatchRequests() { +func (self *RdDb) getShowInfo(showid int) (title string, carts map[int]int, err error) { + var macros string + err = self.getShowInfoStmt.QueryRow(showid).Scan(&title, ¯os) + if err != nil { + if err == sql.ErrNoRows { + err = fmt.Errorf("show '%d' not found", showid) + } + return + } + + rhdl.Printf("RdDb: macros for showid '%d' are set to '%s'", showid, macros) + // TODO: also fetch cart list + carts = make(map[int]int) + return +} + +func (self *RdDb) dispatchRequests() { defer func() { self.done <- true }() for { select { @@ -90,17 +121,21 @@ func (self *RDDB) dispatchRequests() { return case req := <-self.getPasswordChan: if req.cached { - rhdl.Println("RDDB: got getPassword request for", req.username, "(cached)") + rhdl.Println("RdDb: got getPassword request for", req.username, "(cached)") } else { - rhdl.Println("RDDB: got getPassword request for", req.username, "(not cached)") + rhdl.Println("RdDb: got getPassword request for", req.username, "(not cached)") } pwd, err := self.getPassword(req.username, req.cached) req.response <- getPasswordResult{pwd, err} + case req := <-self.getShowInfoChan: + rhdl.Println("RdDb: got getShowInfo request for", req.showid) + title, carts, err := self.getShowInfo(req.showid) + req.response <- getShowInfoResult{title, carts, err} } } } -func (self *RDDB) Cleanup() { +func (self *RdDb) Cleanup() { self.quit <- true <-self.done close(self.quit) @@ -109,18 +144,22 @@ func (self *RDDB) Cleanup() { if self.dbh != nil { self.dbh.Close() } - if self.dbGetPasswordStmt != nil { - self.dbGetPasswordStmt.Close() + if self.getPasswordStmt != nil { + self.getPasswordStmt.Close() + } + if self.getShowInfoStmt != nil { + self.getPasswordStmt.Close() } } -func NewRDDB(conf *Config) (db *RDDB, err error) { - db = new(RDDB) +func NewRdDb(conf *Config) (db *RdDb, err error) { + db = new(RdDb) db.quit = make(chan bool) db.done = make(chan bool) db.password_cache = make(map[string]string) db.getPasswordChan = make(chan getPasswordRequest) + db.getShowInfoChan = make(chan getShowInfoRequest) if err = db.init(conf); err != nil { return diff --git a/src/helsinki.at/rhimportd/ctrlWeb.go b/src/helsinki.at/rhimportd/ctrlWeb.go index e5f74b7..6475a96 100644 --- a/src/helsinki.at/rhimportd/ctrlWeb.go +++ b/src/helsinki.at/rhimportd/ctrlWeb.go @@ -32,16 +32,16 @@ import ( type webHandler struct { *rhimport.Config - *rhimport.RDDB + *rhimport.RdDb trusted bool - H func(*rhimport.Config, *rhimport.RDDB, bool, http.ResponseWriter, *http.Request) + H func(*rhimport.Config, *rhimport.RdDb, bool, http.ResponseWriter, *http.Request) } func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - self.H(self.Config, self.RDDB, self.trusted, w, r) + self.H(self.Config, self.RdDb, self.trusted, w, r) } -func StartControlWeb(addr_s string, conf *rhimport.Config, rddb *rhimport.RDDB) { +func StartControlWeb(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDb) { http.Handle("/public/simple", webHandler{conf, rddb, false, webSimpleHandler}) http.Handle("/trusted/simple", webHandler{conf, rddb, true, webSimpleHandler}) diff --git a/src/helsinki.at/rhimportd/ctrlWebSimple.go b/src/helsinki.at/rhimportd/ctrlWebSimple.go index f4c64ee..7b7c70d 100644 --- a/src/helsinki.at/rhimportd/ctrlWebSimple.go +++ b/src/helsinki.at/rhimportd/ctrlWebSimple.go @@ -67,7 +67,7 @@ func webSimpleResponse(w http.ResponseWriter) { encoder.Encode(respdata) } -func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RDDB, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) { +func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) { decoder := json.NewDecoder(r.Body) var reqdata webSimpleRequestData @@ -93,7 +93,7 @@ func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RDDB, trusted b return } -func webSimpleHandler(conf *rhimport.Config, rddb *rhimport.RDDB, trusted bool, w http.ResponseWriter, r *http.Request) { +func webSimpleHandler(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, w http.ResponseWriter, r *http.Request) { rhdl.Printf("SimpleHandler: request for '%s'", html.EscapeString(r.URL.Path)) var ctx *rhimport.ImportContext diff --git a/src/helsinki.at/rhimportd/rhimportd.go b/src/helsinki.at/rhimportd/rhimportd.go index 7cdc67b..b8c5e3b 100644 --- a/src/helsinki.at/rhimportd/rhimportd.go +++ b/src/helsinki.at/rhimportd/rhimportd.go @@ -51,7 +51,7 @@ func main() { return } - rddb, err := rhimport.NewRDDB(conf) + rddb, err := rhimport.NewRdDb(conf) if err != nil { rhl.Println("Error initializing Rivdenll DB:", err) return -- cgit v0.10.2