feta/feta.go

128 lines
2.7 KiB
Go
Raw Normal View History

2019-11-05 23:32:09 +00:00
package feta
import "os"
import "time"
2019-11-06 01:15:31 +00:00
import "github.com/jinzhu/gorm"
2019-12-14 15:49:35 +00:00
import _ "github.com/jinzhu/gorm/dialects/sqlite" // required for orm
2019-11-06 01:15:31 +00:00
2019-11-05 23:32:09 +00:00
import "github.com/rs/zerolog"
import "github.com/rs/zerolog/log"
import "github.com/mattn/go-isatty"
import "github.com/sneak/feta/ingester"
2019-11-05 23:32:09 +00:00
// InstanceHostname is a special type for holding the hostname of an
// instance (string)
2019-11-06 01:15:31 +00:00
type InstanceHostname string
// CLIEntry is the main entrypoint for the feta process from the cli
func CLIEntry(version string, buildarch string) int {
f := new(Process)
2019-11-05 23:32:09 +00:00
f.version = version
f.buildarch = buildarch
f.setupLogging()
return f.runForever()
2019-11-05 23:32:09 +00:00
}
// Process is the main structure/process of this app
type Process struct {
2019-11-05 23:32:09 +00:00
version string
buildarch string
2019-11-06 01:15:31 +00:00
locator *InstanceLocator
manager *InstanceManager
ingester *ingester.TootIngester
api *fetaAPIServer
2019-11-06 01:15:31 +00:00
db *gorm.DB
2019-11-06 07:03:42 +00:00
startup time.Time
2019-11-05 23:32:09 +00:00
}
func (f *Process) identify() {
2019-11-05 23:32:09 +00:00
log.Info().
Str("version", f.version).
Str("buildarch", f.buildarch).
Msg("starting")
}
func (f *Process) setupLogging() {
2019-11-06 01:15:31 +00:00
2019-11-05 23:32:09 +00:00
log.Logger = log.With().Caller().Logger()
2019-11-06 01:15:31 +00:00
tty := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
if tty {
2019-11-06 01:15:31 +00:00
out := zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) {
// Customize time format
w.TimeFormat = time.RFC3339
},
)
log.Logger = log.Output(out)
2019-11-05 23:32:09 +00:00
}
// always log in UTC
zerolog.TimestampFunc = func() time.Time {
return time.Now().UTC()
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if os.Getenv("DEBUG") != "" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
f.identify()
}
func (f *Process) uptime() time.Duration {
2019-11-06 07:03:42 +00:00
return time.Since(f.startup)
2019-11-06 01:15:31 +00:00
}
func (f *Process) setupDatabase() {
2019-11-06 01:15:31 +00:00
var err error
2019-11-06 07:03:42 +00:00
f.db, err = gorm.Open("sqlite3", "feta.sqlite")
2019-11-06 01:15:31 +00:00
if err != nil {
2019-11-06 07:03:42 +00:00
panic(err)
2019-11-06 01:15:31 +00:00
}
2019-11-06 07:03:42 +00:00
f.databaseMigrations()
}
func (f *Process) runForever() int {
2019-11-06 07:03:42 +00:00
f.startup = time.Now()
f.setupDatabase()
2019-11-06 01:15:31 +00:00
newInstanceHostnameNotifications := make(chan InstanceHostname)
2019-11-05 23:32:09 +00:00
f.locator = newInstanceLocator()
f.manager = newInstanceManager()
f.ingester = ingester.NewTootIngester()
f.api = new(fetaAPIServer)
f.api.setFeta(f) // api needs to get to us to access data
2019-11-05 23:32:09 +00:00
2019-12-14 16:34:13 +00:00
f.locator.setInstanceNotificationChannel(newInstanceHostnameNotifications)
f.manager.setInstanceNotificationChannel(newInstanceHostnameNotifications)
2019-11-05 23:32:09 +00:00
f.manager.setTootDestination(f.ingester.GetDeliveryChannel())
// ingester goroutine:
go f.ingester.Ingest()
2019-11-06 01:15:31 +00:00
// locator goroutine:
go f.locator.Locate()
2019-11-05 23:32:09 +00:00
2019-11-06 01:15:31 +00:00
// manager goroutine:
go f.manager.Manage()
2019-11-06 01:15:31 +00:00
go f.api.Serve()
2019-11-06 01:15:31 +00:00
// this goroutine (main) does nothing until we handle signals
// FIXME(sneak)
for {
time.Sleep(1 * time.Second)
}
2019-11-05 23:32:09 +00:00
return 0
}