2021-05-30 12:12:00 +01:00
|
|
|
package server
|
2021-03-04 13:38:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-08 13:25:55 +01:00
|
|
|
"net/http"
|
2021-03-04 13:38:18 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2021-09-11 20:12:47 +01:00
|
|
|
"git.iim.gay/grufwub/go-store/kv"
|
2021-03-04 13:38:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/account"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/app"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/auth"
|
2021-07-11 15:22:21 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/blocks"
|
2021-05-31 16:36:35 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/emoji"
|
2021-07-09 17:32:48 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/favourites"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/fileserver"
|
2021-05-31 16:36:35 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/filter"
|
2021-05-15 10:58:11 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequest"
|
2021-05-09 13:06:06 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
|
2021-05-31 16:36:35 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/list"
|
2021-05-08 13:25:55 +01:00
|
|
|
mediaModule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
|
2021-05-27 15:06:24 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/notification"
|
2021-05-29 18:36:54 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/search"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/status"
|
2021-06-19 10:18:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/streaming"
|
2021-05-21 22:04:59 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/timeline"
|
2021-06-24 13:26:08 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/nodeinfo"
|
2021-05-09 19:34:27 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/user"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/webfinger"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/security"
|
2021-05-30 12:12:00 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/cliactions"
|
2021-04-01 19:46:45 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-25 14:34:33 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-04-01 19:46:45 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
2021-05-27 15:06:24 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
|
2021-05-30 12:12:00 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2021-04-01 19:46:45 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-07-23 09:36:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2021-05-30 12:12:00 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
2021-04-01 19:46:45 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/router"
|
2021-06-13 17:42:28 +01:00
|
|
|
timelineprocessing "github.com/superseriousbusiness/gotosocial/internal/timeline"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-06-21 18:46:10 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/web"
|
2021-03-04 13:38:18 +00:00
|
|
|
)
|
|
|
|
|
2021-05-30 12:12:00 +01:00
|
|
|
// Start creates and starts a gotosocial server
|
|
|
|
var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 14:34:33 +01:00
|
|
|
dbService, err := bundb.NewBunDBService(ctx, c, log)
|
2021-03-04 13:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-09 15:15:25 +01:00
|
|
|
if err := dbService.CreateAllTables(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error creating database tables: %s", err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := dbService.CreateInstanceAccount(ctx); err != nil {
|
2021-06-13 17:42:28 +01:00
|
|
|
return fmt.Errorf("error creating instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := dbService.CreateInstanceInstance(ctx); err != nil {
|
2021-06-13 17:42:28 +01:00
|
|
|
return fmt.Errorf("error creating instance instance: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:06:24 +01:00
|
|
|
federatingDB := federatingdb.New(dbService, c, log)
|
2021-05-21 14:48:26 +01:00
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
router, err := router.New(ctx, c, dbService, log)
|
2021-04-01 19:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:18:06 +01:00
|
|
|
// Open the storage backend
|
|
|
|
storage, err := kv.OpenFile(c.StorageConfig.BasePath, nil)
|
2021-04-01 19:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating storage backend: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:25:55 +01:00
|
|
|
// build converters and util
|
2021-08-29 11:03:08 +01:00
|
|
|
typeConverter := typeutils.NewConverter(c, dbService, log)
|
2021-06-13 17:42:28 +01:00
|
|
|
timelineManager := timelineprocessing.NewManager(dbService, typeConverter, c, log)
|
2021-05-08 13:25:55 +01:00
|
|
|
|
2021-04-01 19:46:45 +01:00
|
|
|
// build backend handlers
|
2021-09-11 20:18:06 +01:00
|
|
|
mediaHandler := media.New(c, dbService, storage, log)
|
2021-04-01 19:46:45 +01:00
|
|
|
oauthServer := oauth.New(dbService, log)
|
2021-08-10 12:32:39 +01:00
|
|
|
transportController := transport.NewController(c, dbService, &federation.Clock{}, http.DefaultClient, log)
|
2021-07-05 12:23:03 +01:00
|
|
|
federator := federation.NewFederator(dbService, federatingDB, transportController, c, log, typeConverter, mediaHandler)
|
2021-09-11 20:18:06 +01:00
|
|
|
processor := processing.NewProcessor(c, typeConverter, federator, oauthServer, mediaHandler, storage, timelineManager, dbService, log)
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := processor.Start(ctx); err != nil {
|
2021-05-08 13:25:55 +01:00
|
|
|
return fmt.Errorf("error starting processor: %s", err)
|
2021-04-19 18:42:19 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 09:36:28 +01:00
|
|
|
idp, err := oidc.NewIDP(c, log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating oidc idp: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:46:45 +01:00
|
|
|
// build client api modules
|
2021-07-23 09:36:28 +01:00
|
|
|
authModule := auth.New(c, dbService, oauthServer, idp, log)
|
2021-05-08 13:25:55 +01:00
|
|
|
accountModule := account.New(c, processor, log)
|
2021-05-09 13:06:06 +01:00
|
|
|
instanceModule := instance.New(c, processor, log)
|
2021-05-08 13:25:55 +01:00
|
|
|
appsModule := app.New(c, processor, log)
|
2021-05-15 10:58:11 +01:00
|
|
|
followRequestsModule := followrequest.New(c, processor, log)
|
2021-05-09 19:34:27 +01:00
|
|
|
webfingerModule := webfinger.New(c, processor, log)
|
2021-06-24 13:26:08 +01:00
|
|
|
nodeInfoModule := nodeinfo.New(c, processor, log)
|
2021-06-21 18:46:10 +01:00
|
|
|
webBaseModule := web.New(c, processor, log)
|
2021-05-09 19:34:27 +01:00
|
|
|
usersModule := user.New(c, processor, log)
|
2021-05-21 22:04:59 +01:00
|
|
|
timelineModule := timeline.New(c, processor, log)
|
2021-05-27 15:06:24 +01:00
|
|
|
notificationModule := notification.New(c, processor, log)
|
2021-05-29 18:36:54 +01:00
|
|
|
searchModule := search.New(c, processor, log)
|
2021-05-31 16:36:35 +01:00
|
|
|
filtersModule := filter.New(c, processor, log)
|
|
|
|
emojiModule := emoji.New(c, processor, log)
|
|
|
|
listsModule := list.New(c, processor, log)
|
2021-05-08 13:25:55 +01:00
|
|
|
mm := mediaModule.New(c, processor, log)
|
|
|
|
fileServerModule := fileserver.New(c, processor, log)
|
|
|
|
adminModule := admin.New(c, processor, log)
|
|
|
|
statusModule := status.New(c, processor, log)
|
2021-07-05 12:23:03 +01:00
|
|
|
securityModule := security.New(c, dbService, log)
|
2021-06-19 10:18:55 +01:00
|
|
|
streamingModule := streaming.New(c, processor, log)
|
2021-07-09 17:32:48 +01:00
|
|
|
favouritesModule := favourites.New(c, processor, log)
|
2021-07-11 15:22:21 +01:00
|
|
|
blocksModule := blocks.New(c, processor, log)
|
2021-04-01 19:46:45 +01:00
|
|
|
|
2021-05-08 13:25:55 +01:00
|
|
|
apis := []api.ClientModule{
|
2021-04-19 18:42:19 +01:00
|
|
|
// modules with middleware go first
|
|
|
|
securityModule,
|
|
|
|
authModule,
|
|
|
|
|
|
|
|
// now everything else
|
2021-06-21 18:46:10 +01:00
|
|
|
webBaseModule,
|
2021-04-01 19:46:45 +01:00
|
|
|
accountModule,
|
2021-05-09 13:06:06 +01:00
|
|
|
instanceModule,
|
2021-04-01 19:46:45 +01:00
|
|
|
appsModule,
|
2021-05-15 10:58:11 +01:00
|
|
|
followRequestsModule,
|
2021-04-19 18:42:19 +01:00
|
|
|
mm,
|
|
|
|
fileServerModule,
|
|
|
|
adminModule,
|
|
|
|
statusModule,
|
2021-05-09 19:34:27 +01:00
|
|
|
webfingerModule,
|
2021-06-24 13:26:08 +01:00
|
|
|
nodeInfoModule,
|
2021-05-09 19:34:27 +01:00
|
|
|
usersModule,
|
2021-05-21 22:04:59 +01:00
|
|
|
timelineModule,
|
2021-05-27 15:06:24 +01:00
|
|
|
notificationModule,
|
2021-05-29 18:36:54 +01:00
|
|
|
searchModule,
|
2021-05-31 16:36:35 +01:00
|
|
|
filtersModule,
|
|
|
|
emojiModule,
|
|
|
|
listsModule,
|
2021-06-19 10:18:55 +01:00
|
|
|
streamingModule,
|
2021-07-09 17:32:48 +01:00
|
|
|
favouritesModule,
|
2021-07-11 15:22:21 +01:00
|
|
|
blocksModule,
|
2021-04-01 19:46:45 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 13:25:55 +01:00
|
|
|
for _, m := range apis {
|
2021-04-01 19:46:45 +01:00
|
|
|
if err := m.Route(router); err != nil {
|
|
|
|
return fmt.Errorf("routing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 12:12:00 +01:00
|
|
|
gts, err := gotosocial.NewServer(dbService, router, federator, c)
|
2021-04-01 19:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating gotosocial service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gts.Start(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error starting gotosocial service: %s", err)
|
|
|
|
}
|
2021-03-05 17:31:12 +00:00
|
|
|
|
2021-03-04 13:38:18 +00:00
|
|
|
// catch shutdown signals from the operating system
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Infof("received signal %s, shutting down", sig)
|
|
|
|
|
|
|
|
// close down all running services in order
|
2021-04-01 19:46:45 +01:00
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 13:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("done! exiting...")
|
|
|
|
return nil
|
|
|
|
}
|