feta/process/feta.go

141 lines
3.0 KiB
Go
Raw Normal View History

2019-12-19 14:24:26 +00:00
package process
2019-11-05 23:32:09 +00:00
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"
2019-11-05 23:32:09 +00:00
import "git.eeqj.de/sneak/feta/ingester"
import "git.eeqj.de/sneak/feta/storage"
import "git.eeqj.de/sneak/feta/locator"
import "git.eeqj.de/sneak/feta/manager"
import "git.eeqj.de/sneak/feta/instance"
2019-11-06 01:15:31 +00:00
// CLIEntry is the main entrypoint for the feta process from the cli
func CLIEntry(version string, buildarch string) int {
2019-12-19 14:24:26 +00:00
f := new(Feta)
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
}
2019-12-19 14:24:26 +00:00
// Feta is the main structure/process of this app
type Feta struct {
2019-11-05 23:32:09 +00:00
version string
buildarch string
2019-12-19 14:24:26 +00:00
locator *locator.InstanceLocator
manager *manager.InstanceManager
ingester *ingester.TootIngester
2019-12-19 14:24:26 +00:00
api *Server
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
}
2019-12-19 14:24:26 +00:00
func (f *Feta) identify() {
2019-11-05 23:32:09 +00:00
log.Info().
Str("version", f.version).
Str("buildarch", f.buildarch).
Msg("starting")
}
2019-12-19 14:24:26 +00:00
func (f *Feta) 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()
}
2019-12-19 14:24:26 +00:00
func (f *Feta) uptime() time.Duration {
2019-11-06 07:03:42 +00:00
return time.Since(f.startup)
2019-11-06 01:15:31 +00:00
}
2019-12-19 14:24:26 +00:00
/*
func (f *Feta) 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
2019-12-19 14:24:26 +00:00
//f.databaseMigrations()
2019-11-06 07:03:42 +00:00
}
2019-12-19 14:24:26 +00:00
*/
2019-11-06 07:03:42 +00:00
2019-12-19 14:24:26 +00:00
func (f *Feta) runForever() int {
2019-11-06 07:03:42 +00:00
f.startup = time.Now()
2019-12-19 14:24:26 +00:00
//f.setupDatabase()
2019-11-06 07:03:42 +00:00
2019-12-19 14:24:26 +00:00
// FIXME move this channel creation into the manager's constructor
// and add getters/setters on the manager/locator
newInstanceHostnameNotifications := make(chan instance.Hostname)
2019-11-05 23:32:09 +00:00
2019-12-19 14:24:26 +00:00
f.locator = locator.New()
f.manager = manager.New()
f.ingester = ingester.NewTootIngester()
2019-12-19 14:24:26 +00:00
home := os.Getenv("HOME")
if home == "" {
panic("can't find home directory")
}
diskBackend := storage.NewTootFSStorage(home + "/.local/feta")
f.ingester.SetStorageBackend(diskBackend)
f.api = new(Server)
f.api.SetFeta(f) // api needs to get to us to access data
2019-11-05 23:32:09 +00:00
2019-12-19 14:24:26 +00:00
f.locator.SetInstanceNotificationChannel(newInstanceHostnameNotifications)
f.manager.SetInstanceNotificationChannel(newInstanceHostnameNotifications)
2019-11-05 23:32:09 +00:00
2019-12-19 14:24:26 +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
}