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"
|
2019-11-06 16:02:55 +00:00
|
|
|
import "github.com/mattn/go-isatty"
|
2019-11-05 23:32:09 +00:00
|
|
|
|
2019-12-19 14:24:26 +00:00
|
|
|
import "github.com/sneak/feta/ingester"
|
|
|
|
import "github.com/sneak/feta/storage"
|
|
|
|
import "github.com/sneak/feta/locator"
|
|
|
|
import "github.com/sneak/feta/manager"
|
|
|
|
import "github.com/sneak/feta/instance"
|
2019-11-06 01:15:31 +00:00
|
|
|
|
2019-12-14 15:24:42 +00:00
|
|
|
// CLIEntry is the main entrypoint for the feta process from the cli
|
2019-12-19 12:39:42 +00:00
|
|
|
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()
|
2019-12-14 15:24:42 +00:00
|
|
|
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
|
2019-12-19 12:39:42 +00:00
|
|
|
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
|
|
|
|
2019-11-06 16:02:55 +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()
|
2019-12-19 12:39:42 +00:00
|
|
|
f.ingester = ingester.NewTootIngester()
|
2019-12-14 15:39:58 +00:00
|
|
|
|
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())
|
2019-12-14 15:39:58 +00:00
|
|
|
|
|
|
|
// ingester goroutine:
|
2019-12-19 12:39:42 +00:00
|
|
|
go f.ingester.Ingest()
|
2019-12-14 15:39:58 +00:00
|
|
|
|
2019-11-06 01:15:31 +00:00
|
|
|
// locator goroutine:
|
2019-12-19 12:39:42 +00:00
|
|
|
go f.locator.Locate()
|
2019-11-05 23:32:09 +00:00
|
|
|
|
2019-11-06 01:15:31 +00:00
|
|
|
// manager goroutine:
|
2019-12-19 12:39:42 +00:00
|
|
|
go f.manager.Manage()
|
2019-11-06 01:15:31 +00:00
|
|
|
|
2019-12-19 12:39:42 +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
|
|
|
|
}
|