func New(config *config.Config) *Broker { broker := new(Broker) broker.cfg = config // Connect to the message queue server address := fmt.Sprintf("amqp://%s:%s@%s:%d/", config.GetQueueUsername(), config.GetQueuePassword(), config.GetQueueHost(), config.GetQueuePort()) conn, err := amqp.Dial(address) if err != nil { fmt.Printf("There was an error connecting to the broker") panic(err) } broker.conn = conn // Create a new channel channel, err := conn.Channel() if err != nil { panic(err) } broker.channel = channel broker.initializeQueues() return broker }
/////////////////////////////////////////////////////////////// // Database housekeeping func New(cfg *config.Config) *Db { db := new(Db) db.cfg = cfg connection, err := sql.Open("sqlite3", cfg.GetDbFilename()) if err != nil { fmt.Printf("There was an error opening the database\n") return nil } db.connection = connection return db }
func newOAuthClient(ctx context.Context, config *oauth2.Config, cfg *config.Config) *http.Client { cacheFile := tokenCacheFile(config) token, err := tokenFromFile(cacheFile) if err != nil { token = tokenFromWeb(ctx, config, cfg) token.RefreshToken = cfg.GetYoutubeRefreshToken() saveToken(cacheFile, token) } else { fmt.Printf("Using cached token from %q\n", cacheFile) } return config.Client(ctx, token) }
func tokenFromWeb(ctx context.Context, config *oauth2.Config, cfg *config.Config) *oauth2.Token { ch := make(chan string) randState := fmt.Sprintf("st%d", time.Now().UnixNano()) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GetYoutubeOAuthPort())) if err != nil { panic(err) } // make our own handler that puts all requests in a wait group. h := http.NewServeMux() // add a close handlefunc to that handler h.HandleFunc("/youtube_callback/", func(rw http.ResponseWriter, req *http.Request) { if req.URL.Path == "/favicon.ico" { http.Error(rw, "", 404) return } if req.FormValue("state") != randState { fmt.Printf("State doesn't match: req = %#v", req) http.Error(rw, "", 500) return } if code := req.FormValue("code"); code != "" { fmt.Fprintf(rw, "<h1>Success</h1>Authorized.") rw.(http.Flusher).Flush() ch <- code lis.Close() return } fmt.Printf("no code") http.Error(rw, "", 500) lis.Close() }) //listen and serve until listner is closed go http.Serve(lis, h) // TODO fix this hard-coded localhost thingy config.RedirectURL = fmt.Sprintf("http://127.0.0.1:%d/youtube_callback/", cfg.GetYoutubeOAuthPort()) authURL := config.AuthCodeURL(randState) go openURL(authURL) fmt.Printf("Authorize this app at: %s", authURL) code := <-ch token, err := config.Exchange(ctx, code) if err != nil { fmt.Printf("Token exchange error: %v", err) } return token }
func send_email(email string, title string, html string, config *config.Config) { gun := mailgun.NewMailgun("munch.utkarshsinha.com", config.GetMailPrivateKey(), config.GetMailPublicKey()) msg := mailgun.NewMessage("Broccoli Munch <*****@*****.**>", title, html, "*****@*****.**") msg.SetHtml(html) response, id, err := gun.Send(msg) fmt.Printf("%s\n", response) fmt.Printf("%s\n", id) fmt.Printf("%s\n", err) }