fedilogue/fedilogger/fedilogger.go

119 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
2022-01-01 02:26:39 +00:00
"context"
"net/http"
2020-12-17 04:23:25 +00:00
_ "net/http/pprof"
2021-12-11 00:32:32 -05:00
"regexp"
2022-01-01 02:26:39 +00:00
"runtime"
2020-12-17 04:23:25 +00:00
"sync"
"time"
2021-08-07 19:46:35 +00:00
2021-02-13 02:48:11 +00:00
"github.com/microcosm-cc/bluemonday"
2022-01-01 02:26:39 +00:00
"gitlab.com/khanzf/fedilogue/shared"
)
// Current instances
2022-01-01 02:26:39 +00:00
var runninginstances map[string]shared.RunningInstance
var ri_mutex = &sync.Mutex{}
2020-11-24 20:36:47 -05:00
func startpprof() {
2021-02-11 21:05:01 +00:00
logInfo("Starting http/pprof on :7777")
logFatal(http.ListenAndServe("127.0.0.1:7777", nil))
2020-11-24 20:36:47 -05:00
}
2021-08-07 19:46:35 +00:00
func statusReportHandler() {
for {
2021-08-07 19:46:35 +00:00
StatusReport()
time.Sleep(time.Second * 60)
}
}
2021-09-29 06:43:06 +00:00
/* Tests:
- TestStatusReport_empty_run
- TestStatusReport_full_content
*/
2021-08-07 19:46:35 +00:00
func StatusReport() {
running := 0
keepalive := 0
unsupported := 0
mastodon := 0
pleroma := 0
2023-07-14 02:42:07 +00:00
misskey := 0
2021-08-07 19:46:35 +00:00
other := 0
ri_mutex.Lock()
2023-07-14 02:42:07 +00:00
for i, o := range runninginstances {
logDebug("Software ", o.Software, " Status: ", o.Status, " instance ", i)
2021-08-07 19:46:35 +00:00
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
}
2021-08-07 19:46:35 +00:00
if o.Software == "mastodon" && o.Status == 200 {
mastodon = mastodon + 1
} else if o.Software == "pleroma" && o.Status == 200 {
pleroma = pleroma + 1
2023-07-14 02:42:07 +00:00
} else if o.Software == "misskey" && o.Status == 200 {
misskey = misskey + 1
2021-08-07 19:46:35 +00:00
} else if o.Status == 200 {
other = other + 1
}
}
2021-08-07 19:46:35 +00:00
ri_mutex.Unlock()
2023-07-14 02:42:07 +00:00
logInfo("Running:", running, " Keepalive:", keepalive, " Unsupported:", unsupported, " Ma:", mastodon, ",P:", pleroma, ",Mi:", misskey, ",O:", other)
}
2020-11-24 20:36:47 -05:00
func main() {
// Initial Setup
logInit()
2022-01-01 02:26:39 +00:00
runninginstances = make(map[string]shared.RunningInstance)
getSettings()
2020-11-24 20:36:47 -05:00
go startpprof()
2020-11-16 00:10:33 -05:00
2020-12-18 06:06:32 +00:00
pool = getDbPool()
2020-12-06 18:05:17 -05:00
p = bluemonday.NewPolicy()
spaceReg = regexp.MustCompile(`[\s\t\.]+`)
2021-01-14 20:43:20 +00:00
removeHTMLReg = regexp.MustCompile(`<\/?\s*br\s*>`)
re = regexp.MustCompile("^https?://([^/]*)/(.*)$")
2021-01-25 20:28:13 -05:00
matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*")
staggeredStartChan = make(chan bool)
2021-12-11 00:32:32 -05:00
// 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()
2022-01-01 02:26:39 +00:00
go staggeredStart()
go statusReportHandler()
2021-12-11 00:32:32 -05:00
for rows.Next() {
var endpoint string
err = rows.Scan(&endpoint)
if err != nil {
2021-12-11 00:53:43 -05:00
logErr("Unable to iterate database, exiting.")
2021-12-11 00:32:32 -05:00
return
}
2022-01-01 02:26:39 +00:00
o, exists := GetRunner(endpoint)
if o.Banned == true {
continue // Banned instance
}
2021-12-11 00:32:32 -05:00
if exists == false {
go StartInstance(endpoint)
}
}
go startctl()
go webmain()
runtime.Goexit()
}