fedilogue/poll/main.go
Farhan Khan 58d63dea93 added web receiving scaffolding
changed pprof port to 7777
added received_at post column
2020-11-24 00:00:04 -05:00

128 lines
3.6 KiB
Go

package main
import (
"github.com/microcosm-cc/bluemonday"
"github.com/jackc/pgx/pgxpool"
_ "net/http/pprof"
"net/http"
"context"
"sync"
"fmt"
"net"
"log"
"os"
)
func AppendIfMissing(hay []string, needle string) []string {
for _, ele := range hay {
if ele == needle {
return hay
}
}
return append(hay, needle)
}
func writePost(pool *pgxpool.Pool, reportpost ReportPost) {
conn, err := pool.Acquire(context.Background())
if err != nil {
log.Fatal("Error connecting to database:", err)
os.Exit(1)
}
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, url) 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 {
fmt.Println("First ", err)
fmt.Println("--------------------------")
fmt.Println("Reported error: ", err)
fmt.Println("Account Acct: ", reportpost.Account.Acct)
fmt.Println("Account Avatar: ", reportpost.Account.Avatar)
fmt.Println("Account Avatar len: ", len(reportpost.Account.Avatar))
fmt.Println("Account Bot: ", reportpost.Account.Bot)
fmt.Println("Account Created_at: ", reportpost.Account.Created_at)
fmt.Println("Account Display: ", reportpost.Account.Display_name)
fmt.Println("Account URL: ", reportpost.Account.Url)
fmt.Println(reportpost)
fmt.Println("--------------------------")
os.Exit(1) // For now I want this to die and learn why it failed
return
}
// Insert new post if new
_, err = conn.Exec(context.Background(), "INSERT INTO posts (url, content, created_at, normalized, account_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Url, reportpost.Content, reportpost.Created_at, reportpost.normalized, accountid, reportpost.posthash)
if err != nil { // For now I want to know why this failed.
fmt.Println("Second ", err)
fmt.Println("--------------------------")
fmt.Println("Reported error: ", err)
fmt.Println("Url: ", reportpost.Url)
fmt.Println("Content: ", reportpost.Content)
fmt.Println("Created_at: ", reportpost.Created_at)
fmt.Println("normalized: ", reportpost.normalized)
fmt.Println("account_id", accountid)
fmt.Println("posthash: ", reportpost.posthash)
fmt.Println("--------------------------")
os.Exit(1) // For now I want this to die and learn why it failed
return
}
}
// Current instances
var runninginstances map[string]RunningInstance
var ri_mutex = &sync.Mutex{}
func engine() {
p = bluemonday.NewPolicy()
runninginstances = make(map[string]RunningInstance)
// Initial Setup
reportPostChan := make(chan ReportPost)
// Setup Database
pool, err := pgxpool.Connect(context.Background(), "postgres://postgres@127.0.0.1/fedilogue")
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
os.Exit(1)
}
l, err := net.Listen("tcp", "127.0.0.1:5555")
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
commandClient := make(chan net.Conn)
go func(l net.Listener) {
for {
c, err := l.Accept()
if err != nil {
fmt.Println("Error on accept", err)
os.Exit(0)
}
commandClient <- c
}
}(l)
go func() {
for {
c := <-commandClient // New client connection
go handleClient(c, reportPostChan)
}
}()
for {
v := <-reportPostChan // New Post
go writePost(pool, v)
}
}
func main() {
go engine()
go webmain()
log.Println("serving on port 8080")
log.Fatal(http.ListenAndServe(":7777", nil))
}