Farhan Khan
ab8e0182bb
restructured how the engine works some communication is through a shared object, perhaps I will revisit this problem in the future. Until then, this is stable and works...
158 lines
4.3 KiB
Go
158 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"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 imdone() {
|
|
// fmt.Println("I am done")
|
|
}
|
|
|
|
func writePost(pool *pgxpool.Pool, reportpost ReportPost) {
|
|
defer imdone()
|
|
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() {
|
|
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 <- nil
|
|
return
|
|
}
|
|
commandClient <- c
|
|
}
|
|
}(l)
|
|
|
|
go func() {
|
|
for{
|
|
v := <-reportPostChan // New Post
|
|
// fmt.Println(v)
|
|
go writePost(pool, v)
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
for {
|
|
c := <-commandClient // New client connection
|
|
go handleClient(c, reportPostChan)
|
|
}
|
|
}()
|
|
|
|
/*
|
|
for {
|
|
select {
|
|
case w := <-instanceReportChan: // Start or suspend instance
|
|
if w.status == NEW_INSTANCE {
|
|
x = x + 1
|
|
NewInstance(w.endpoint, instanceReportChan, reportPostChan)
|
|
} else if w.status == RUNNING || w.status == TOOMANYREQUESTS {
|
|
for i, runninginstance := range runninginstances {
|
|
if runninginstance.Endpoint == w.endpoint {
|
|
runninginstances[i].Min_id = w.min_id
|
|
runninginstances[i].Status = w.status
|
|
runninginstances[i].LastRun = time.Now().Format("2006.01.02-15:04:05")
|
|
}
|
|
}
|
|
go DeferPollRun(w, instanceReportChan, reportPostChan)
|
|
} else {
|
|
SuspendInstance(w)
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
}
|
|
|
|
func main() {
|
|
go engine()
|
|
log.Println("serving on port 8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|