fedilogue/fedilogger/fedilogger.go
Farhan Khan a427b27a80 Restructuring of go modules and Dockerfiles
Moving to new git repository location
2025-01-24 04:36:22 +00:00

119 lines
2.5 KiB
Go

package main
import (
"context"
"net/http"
_ "net/http/pprof"
"regexp"
"runtime"
"sync"
"time"
"github.com/microcosm-cc/bluemonday"
"git.farhan.codes/farhan/fedilogue/shared"
)
// Current instances
var runninginstances map[string]shared.RunningInstance
var ri_mutex = &sync.Mutex{}
func startpprof() {
logInfo("Starting http/pprof on :7777")
logFatal(http.ListenAndServe("127.0.0.1:7777", nil))
}
func statusReportHandler() {
for {
StatusReport()
time.Sleep(time.Second * 60)
}
}
/* Tests:
- TestStatusReport_empty_run
- TestStatusReport_full_content
*/
func StatusReport() {
running := 0
keepalive := 0
unsupported := 0
mastodon := 0
pleroma := 0
misskey := 0
other := 0
ri_mutex.Lock()
for i, o := range runninginstances {
logDebug("Software ", o.Software, " Status: ", o.Status, " instance ", i)
if o.Status == 200 {
running = running + 1
} else if o.Status == 607 { // Keepalive
keepalive = keepalive + 1
} else if o.Status == 605 { // Unsupported instance
unsupported = unsupported + 1
}
if o.Software == "mastodon" && o.Status == 200 {
mastodon = mastodon + 1
} else if o.Software == "pleroma" && o.Status == 200 {
pleroma = pleroma + 1
} else if o.Software == "misskey" && o.Status == 200 {
misskey = misskey + 1
} else if o.Status == 200 {
other = other + 1
}
}
ri_mutex.Unlock()
logInfo("Running:", running, " Keepalive:", keepalive, " Unsupported:", unsupported, " Ma:", mastodon, ",P:", pleroma, ",Mi:", misskey, ",O:", other)
}
func main() {
// Initial Setup
logInit()
runninginstances = make(map[string]shared.RunningInstance)
getSettings()
go startpprof()
pool = getDbPool()
p = bluemonday.NewPolicy()
spaceReg = regexp.MustCompile(`[\s\t\.]+`)
removeHTMLReg = regexp.MustCompile(`<\/?\s*br\s*>`)
re = regexp.MustCompile("^https?://([^/]*)/(.*)$")
matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*")
staggeredStartChan = make(chan bool)
// Start instances located in database
rows, err := pool.Query(context.Background(), "SELECT endpoint FROM instances")
if err != nil {
logErr("Unable to select from instances")
return
}
defer rows.Close()
go staggeredStart()
go statusReportHandler()
for rows.Next() {
var endpoint string
err = rows.Scan(&endpoint)
if err != nil {
logErr("Unable to iterate database, exiting.")
return
}
o, exists := GetRunner(endpoint)
if o.Banned == true {
continue // Banned instance
}
if exists == false {
go StartInstance(endpoint)
}
}
go startctl()
go webmain()
runtime.Goexit()
}