Farhan Khan
e9178b1d09
middle layer commit database middle layer might be redundant screwed up tables.sql isn't re-useing connections
94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"github.com/jackc/pgx/pgxpool"
|
|
// "github.com/davecgh/go-spew/spew"
|
|
// "time"
|
|
)
|
|
|
|
func postHandler(reportPostChan chan ReportPost) {
|
|
for { // Write posts
|
|
v := <-reportPostChan
|
|
go writePost(v)
|
|
}
|
|
}
|
|
|
|
/*
|
|
func check_user(uri string) (AccountType, error) {
|
|
conn, _:= pool.Acquire(context.Background())
|
|
defer conn.Release()
|
|
|
|
var accountData AccountType
|
|
|
|
//var q string
|
|
//var Acct string
|
|
//err := conn.QueryRow(context.Background(), "SELECT acct, avatar, bot, created_at, display_name FROM accounts WHERE uri = $1", uri).Scan(&accountData.Acct, &(accountData.Avatar), &(q), &accountData.Created_at, &accountData.Display_name)
|
|
var timez time.Time
|
|
row := conn.QueryRow(context.Background(), "SELECT acct, avatar, bot, created_at, display_name, FROM accounts WHERE uri = $1", uri)
|
|
err := row.Scan(&accountData.Acct, &accountData.Avatar, &accountData.Bot, &timez, &accountData.Display_name)
|
|
if err != nil {
|
|
return accountData, err
|
|
}
|
|
accountData.Url = uri
|
|
accountData.Created_at = timez.Format(time.RFC3339)
|
|
spew.Dump(accountData)
|
|
return accountData, err
|
|
}
|
|
*/
|
|
|
|
func writePost(reportpost ReportPost) {
|
|
conn, err := pool.Acquire(context.Background())
|
|
if err != nil {
|
|
log.Fatal("Error connecting to database:", err)
|
|
}
|
|
defer conn.Release()
|
|
|
|
// Insert new account if new
|
|
var accountid int
|
|
err = conn.QueryRow(context.Background(), "INSERT INTO accounts (acct, avatar, bot, created_at, display_name, uri) VALUES($1, $2, $3, $4, $5, $6) ON CONFLICT(acct) DO UPDATE SET acct=EXCLUDED.acct RETURNING id", reportpost.Account.Acct, reportpost.Account.Avatar, reportpost.Account.Bot, reportpost.Account.Created_at, reportpost.Account.Display_name, reportpost.Account.Url).Scan(&accountid)
|
|
if err != nil {
|
|
log.Print("First ", err)
|
|
log.Print("--------------------------")
|
|
log.Print("Reported error: ", err)
|
|
log.Print("Account Acct: ", reportpost.Account.Acct)
|
|
log.Print("Account Avatar: ", reportpost.Account.Avatar)
|
|
log.Print("Account Avatar len: ", len(reportpost.Account.Avatar))
|
|
log.Print("Account Bot: ", reportpost.Account.Bot)
|
|
log.Print("Account Created_at: ", reportpost.Account.Created_at)
|
|
log.Print("Account Display: ", reportpost.Account.Display_name)
|
|
log.Print("Account URL: ", reportpost.Account.Url)
|
|
log.Print(reportpost)
|
|
log.Print("--------------------------")
|
|
log.Fatal("Unable to write record to database")
|
|
}
|
|
|
|
// Insert new post if new
|
|
_, err = conn.Exec(context.Background(), "INSERT INTO posts (uri, content, created_at, normalized, account_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Uri, reportpost.Content, reportpost.Created_at, reportpost.normalized, accountid, reportpost.posthash)
|
|
if err != nil { // For now I want to know why this failed.
|
|
log.Print("Second ", err)
|
|
log.Print("--------------------------")
|
|
log.Print("Reported error: ", err)
|
|
log.Print("Uri: ", reportpost.Uri)
|
|
log.Print("Content: ", reportpost.Content)
|
|
log.Print("Created_at: ", reportpost.Created_at)
|
|
log.Print("normalized: ", reportpost.normalized)
|
|
log.Print("account_id", accountid)
|
|
log.Print("posthash: ", reportpost.posthash)
|
|
log.Print("--------------------------")
|
|
log.Fatal("Unable to write record to database")
|
|
}
|
|
}
|
|
|
|
func getDbPool() *pgxpool.Pool {
|
|
// Setup Database
|
|
dbURI := fmt.Sprintf("postgres://%s:%s@%s:%d/fedilogue", settings.Database.Username, settings.Database.Password, settings.Database.Host, settings.Database.Port)
|
|
pool, err := pgxpool.Connect(context.Background(), dbURI)
|
|
if err != nil {
|
|
log.Fatal("Unable to connect to database:", err)
|
|
}
|
|
return pool
|
|
}
|